sql过程将文件复制到本地计算机错误

时间:2013-11-13 02:05:25

标签: sql synchronization

我的目的是:

当sql过程备份数据库完成后,将bak文件复制到本地计算机。当复制文件,我的计算机和服务器在loacl网络中。

这是我的程序(bak DB已完成):

create procedure CopyFile
as
begin
    declare @DynamicSQL varchar(1000)
    set @DynamicSQL='net use \\Dolphin-PC\ipc$ 123 /user:Administrator&&xcopy E:\"Database backup"\%Date:~0,4%%Date:~5,2%%Date:~8,2%\*.bak  \\Dolphin-PC\D$\邮电\LocalDB\%Date:~0,4%%Date:~5,2%%Date:~8,2%\ /E /Y /H /K&&net use \\Dolphin-PC\ipc$ /delete'   
    --Configure extension procedure the cmd_shell enable
    exec sp_configure 'show advanced options',1;
    reconfigure;
    exec sp_configure 'xp_cmdshell',1;
    reconfigure;    
    exec xp_cmdshell @DynamicSQL
    --Configure extended procedure cmd_shell disable
    exec sp_configure 'show advanced options',1;
    reconfigure;
    exec sp_configure 'xp_cmdshell',0;
    reconfigure;
end

当我运行proc时,它提示:

occour system error 1312。
NULL
the session have not been complete.Maybe have been finished.
NULL
NULL

我在DOS中运行没有问题。DOS命令是:

net use \\Dolphin-PC\ipc$ 123 /user:Administrator
xcopy E:\"Database backup"\%Date:~0,4%%Date:~5,2%%Date:~8,2%\*.bak  \\Dolphin-PC\D$\邮电\LocalDB\%Date:~0,4%%Date:~5,2%%Date:~8,2%\ /E /Y /H /K
net use \\Dolphin-PC\ipc$ /delete

用另一种方式用sql执行批处理:

exec sp_configure 'show advanced options',1;
    reconfigure;
    exec sp_configure 'xp_cmdshell',1;
    reconfigure;    
    exec xp_cmdshell 'E:\"Database backup"\bakcpy.bat'
    --Configure extended procedure cmd_shell disable
    exec sp_configure 'show advanced options',1;
    reconfigure;
    exec sp_configure 'xp_cmdshell',0;
    reconfigure;

错误是:

NULL
C:\Windows\system32>net use \\Dolphin-PC\ipc$ 123 /user:Administrator 
occur system error 1312.
NULL
The login session does not exits.Maybe finished.
NULL
NULL
C:\Windows\system32>xcopy E:\"Database backup"\20131115\*.bak  \\Dolphin-PC\D$\js\LocalDB\20131115\ /E /Y /H /K 
invalid disk format
 0 file have been copied
NULL
C:\Windows\system32>net use \\Dolphin-PC\ipc$ /delete 
Can't find network connection.
NULL
input NET HELPMSG 2250 to get more help
NULL
NULL

1 个答案:

答案 0 :(得分:0)

将DOS命令放入批处理文件中。而是从存储过程运行批处理文件。这将删除单引号和空格周围的任何问题。