BCP を使用してテーブルからデータをダンプするこの TSQL コードがあります。複雑に見えますが、テーブルごとに 1 回実行される @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...
と、同じエラーが発生します。二重二重引用符を使用"" ""
すると、それは言う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 ? &&
、「パス」の前に を置いたところ、うまくいきました (二重引用符で囲みました) ....なぜですか?