[英]copy files from server to another with t-sql
I'm in trouble. 我有麻烦了。 I need copy a file from one server to another using T-sql.
我需要使用T-sql将文件从一台服务器复制到另一台服务器。 The format is variable (pdf, xlsx, doc, jpg), but generally excel .xlsx.
格式是可变的(pdf,xlsx,doc,jpg),但通常为excel .xlsx。
For now my solution is to convert on the source server the file in a varbinary data and pass it as a parameter to a stored procedure located on the target server. 现在,我的解决方案是在源服务器上将文件转换为varbinary数据,并将其作为参数传递给位于目标服务器上的存储过程。
The code in the source server: 源服务器中的代码:
DECLARE @temp table(data varbinary (max))
DECLARE @out varbinary(max)
INSERT INTO @temp(data)
SELECT * FROM OPENROWSET(BULK N'C:\original.xlsx', SINGLE_BLOB) as data
SELECT @out=data FROM @temp
EXEC DEST_SERVER.copy @out
(I am aware that the code is ugly and redundant, but so far going well) (我知道代码很丑陋和多余,但到目前为止进展顺利)
The code on the destination server is: 目标服务器上的代码是:
CREATE PROCEDURE copy @in varbinary(max)
AS
BEGIN
CREATE TABLE ##temp(data varbinary(max))
insert into ##temp(data) values(@in)
declare @SQLcommand varchar(1000)
set @SQLcommand = 'bcp "SELECT data FROM ##temp" queryout "C:\copied.xlsx" -T -S ' + @@SERVERNAME + ' -n'
exec xp_cmdshell @SQLcommand
DROP TABLE ##temp
END
By the time the procedure works to create the new file on the destination server, but both files are slightly different. 到该过程可以在目标服务器上创建新文件时,但是两个文件都略有不同。
Specifically, the new file has a few more bits at the beginning, everything else is identical: 具体来说,新文件在开始时还有更多位,其他所有内容都相同:
original.xlsx
original.xlsx
50 4B 03 04 14 00 06 00
50 4B 03 04 14 00 06 00
08 00 00 00 21 00 A7 95
08 00 00 00 21 00 A7 95
F9 99 84 01 00 00 14 06
F9 99 84 01 00 00 14 06
00 00 13 00 DD 01 5B 43
00 00 13 00 DD 01 5B 43
6F 6E 74 65 6E 74 5F 54
6F 6E 74 65 6E 74 5F 54
etc...
等等...
copied.xlsx
copied.xlsx
4F 32 00 00 00 00 00 00 -> this bytes are new
4F 32 00 00 00 00 00 00->此字节是新的
50 4B 03 04 14 00 06 00
50 4B 03 04 14 00 06 00
08 00 00 00 21 00 A7 95
08 00 00 00 21 00 A7 95
F9 99 84 01 00 00 14 06
F9 99 84 01 00 00 14 06
00 00 13 00 DD 01 5B 43
00 00 13 00 DD 01 5B 43
6F 6E 74 65 6E 74 5F 54
6F 6E 74 65 6E 74 5F 54
etc...
等等...
What am I doing wrong ?, 我究竟做错了什么 ?,
The guy in this post had the same problem. 这篇文章中的人有同样的问题。 He fix it by specifying a format file with the bcp command.
他通过使用bcp命令指定格式文件来修复它。 I think this might be your problem too.
我认为这也可能是您的问题。 It looks like it might have something to do with bcp and a varbinary column.
看起来可能与bcp和varbinary列有关。
SQL Server BCP export corrupted file? SQL Server BCP导出损坏的文件?
Hope that helps 希望能有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.