我有这个 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
空间的。
如果在 path 周围不使用双引号""
,则会出现错误'C:\Program' is not recognized...
With using double quotes,它会给出相同的错误。使用双双引号"" ""
,它说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 ? &&
在“路径”前面放了一个,它起作用了(用双引号括起来。)....为什么?