我有这个 TSQL 代码,它使用 BCP 从表中转储数据。它看起来很复杂,但它只是创建一个 @command 字符串,为每个表执行一次,然后 BCP 将表记录转储到磁盘。这是快速备份所有表数据的好方法。下面我展示了更容易阅读的已解决版本。
set @command =
'if (''?'' <> ''[dbo].[sysdiagrams]'')
BEGIN;
create table #result (result nvarchar(2048) null );
declare @temp nvarchar(1000);
set @temp = ''' + @bcpPath + ' ' + @database + '.dbo.'' +
substring( ''?'', 8, len(''?'')- 8) +
'' out "' + @driveLetter + @drivePath +
'\'' + substring( ''?'', 8, len(''?'')- 8) +
''.out" -c -x -t"|" -Uuser -Ppassword'';
insert into #result (result)
exec xp_cmdshell @temp;
drop table #result;
END;'
exec sp_msforeachtable @command
@bcppath
是带有空格的 C:\Program Files\Microsoft SQL Server\90\Tools\Binn\bcp.exe
。如果在路径
""
周围不使用双引号,则会给出 'C:\Program' is not recognized...
的错误使用双引号,它会给出相同的错误。使用双双引号 "" ""
,它说 The filename, directory name, or volume label syntax is incorrect.
@command 在打印时解析为:
if ('?' <> '[dbo].[sysdiagrams]')
BEGIN;
create table #result (result nvarchar(2048) null );
declare @temp nvarchar(1000);
set @temp = '"C:\Program Files\Microsoft SQL Server\90\Tools\Binn\bcp.exe"
myDB.dbo.' +
substring( '?', 8, len('?')- 8) +
' out "E:\DataExports\' +
substring( '?', 8, len('?')- 8) + '.out" -c -x -t"|" -Uuser -Ppassword';
insert into #result (result)
exec xp_cmdshell @temp;
drop table #result;
END;
编辑:
奇怪的是,我在“路径”前面放了一个
ECHO ? &&
并且它起作用了(用双引号包围。)......为什么?
最佳答案
你必须在引用路径之前放一些东西以避免错误 C:\Program' is not recognized...
所以我使用了 CALL 语句并且它对我有用......
declare @cmd nvarchar(1000)
set @cmd = 'call "C:\Program Files\Microsoft SQL Server\90\Tools\Binn\bcp.exe" myDB.dbo.'
exec xp_cmdshell @cmd
关于sql-server-2005 - Microsoft SQL xp_cmdshell 不喜欢带有空格的文件名。我可以用别的东西代替空格吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5569051/