开发者社区> 问答> 正文

使用BCP命令在SQL数据库中上传文件

我想将文件从文件所在的本地计算机保存到远程数据库中。我想使用BCP命令来实现。如何做到这一点?

我的查询类似于以下内容:

INSERT INTO ExcelFileUploadedData(
    Project, 
    SubProject, 
    LogicalName,FileName, 
    FileData) 

SELECT 
    'Sample',
    'Sample',
    'BP',
    '1.xls',
    * 
FROM OPENROWSET(BULK N'D:\File\File1.xls.zip', SINGLE_BLOB) rs

展开
收起
祖安文状元 2020-01-05 14:41:52 757 0
1 条回答
写回答
取消 提交回答
  • 你可以试试这个

    SET @TableQuery='CREATE TABLE [temp1](
               [Id] [int] IDENTITY(1,1) NOT NULL,
               Project [NVARCHAR(50)], 
               SubProject [NVARCHAR(50)], 
               LogicalName [NVARCHAR(50)],
               FileName [NVARCHAR(50)], 
               FileData [NVARCHAR(50)],
               CONSTRAINT [PK_temp1] PRIMARY KEY CLUSTERED 
               (
                  [Id] ASC
                )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
                ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
                                                  '
               EXEC(@TableQuery)  
    
               DECLARE @params NVARCHAR(max)
               SET @params = N'@uploadData SINGLE_BLOB readonly'
    
               SET @BulkInsertQuery =N'INSERT INTO [temp1] ( Project,            SubProject,LogicalName,FileName,FileData) 
    select * from @uploadData';          
               print @BulkInsertQuery
               EXEC sp_executesql @BulkInsertQuery, @params, @uploadData
    
      // Enter Records from temp table into real table
    
               INSERT INTO ExcelFileUploadedData(
                  Project, 
                  SubProject, 
                  LogicalName,FileName, 
                  FileData) 
               SELECT 
                  'Sample',
                  'Sample',
                  'BP',
                  '1.xls',
                  * 
           FROM [temp1]
    
    2020-01-05 14:42:01
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server 2017 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载