我有这个使用BCP从表转储数据的TSQL代码。它看起来很复杂,但它只是创建一个@command字符串,为每个表执行一次,然后BCP将表记录转储到磁盘。这是快速备份所有表格数据的好方法。下面我展示了一个更容易阅读的解决版本。Microsoft SQL xp_cmdshell不喜欢带空格的文件名。我可以用其他东西替换空间吗?
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 ? &&
和它工作(由双引号包围)....为什么?
是不是路径中的.. \ binn目录? – DaveE 2011-04-06 15:40:17
即使是这样,第二条路也会杀死它。 – 2011-04-06 16:24:27
可能。如果xp_cmdshell在没有路径组件的情况下调用bcp“bcp myDB.dbo ...”,会发生什么情况?我问b/c我们使用bcp进行批量加载,它不关心文件路径在我们的本地或客户端安装中是否有空格。 – DaveE 2011-04-06 16:43:22