简体   繁体   English

使用t-sql将文件从服务器复制到另一个

[英]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.

相关问题 使用T-SQL脚本将数据从一台服务器复制到另一台服务器吗? - T-SQL Script to copy data from one server to another? 从服务器复制表并插入另一台服务器:此T-SQL查询出了什么问题? - Copy Table from a Server and Insert into another Server: What is wrong with this T-SQL query? T-SQL / SQL-根据条件将值从一列复制到视图中的另一列 - T-SQL/SQL- Copy Value From One Column to Another in View Depending on Condition 当 SQL 服务器中另一个表的两个值之间时,T-SQL 使用 case 语句更新列 - T-SQL update column with case statement when between two values from another table in SQL Server 如何在另一个数据库服务器中运行T-SQL? - How to run T-SQL in another DB server? 使用 T-SQL 从 SQL 服务器获取最旧的记录 - Getting oldest record from SQL Server using T-SQL 自动提取日期来自SQL Server - T-SQL - Auto pickup dates from SQL Server - T-SQL 从 SQL 服务器到 PostgreSQL 的 T-SQL 存储过程 - T-SQL stored procedure from SQL server to PostgreSQL 使用T-Sql,如何从远程服务器上的一个表插入本地服务器上的另一个表? - Using T-Sql, how can I insert from one table on a remote server into another table on my local server? T-SQL查找其他数据库中缺少的项目 - T-SQL Finding Missing item from another database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM