Microsoft SQL xp_cmdshell 不喜欢带空格的文件名。我可以用其他东西替换该空间吗?
我有这个 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 recognize...
使用双引号,它会给出相同的错误错误。使用双双引号 "" ""
时,它表示 文件名、目录名或卷标语法不正确。
@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 ? &&
在“路径”前面并且它有效(用双引号引起来。)....为什么?
I have this TSQL code that dumps data from tables using BCP. It looks complicated, but it simply creates a @command string to be executed once for each table, then BCP dump the table records to disk. It's a nice way to backup all the table data quickly. Below I show the resolved version which is a little easier to read.
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
the @bcppath
is C:\Program Files\Microsoft SQL Server\90\Tools\Binn\bcp.exe
which has a space.
Without using double quotes around the path ""
, it gives an error of 'C:\Program' is not recognized...
With using double quotes, it gives the same error. With using double double quotes "" ""
, it says The filename, directory name, or volume label syntax is incorrect.
@command resolves to this when printed:
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;
EDIT:
Oddly, I put an ECHO ? &&
in front of the "path" and it worked (surrounded by double quotes.) .... Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须在引用路径之前放置一些内容以避免错误
C:\Program' is not recognize...
所以我使用了 CALL 语句,它对我有用...You have to put something before quoted path to avoid error
C:\Program' is not recognized...
so I used CALL statement and it worked for me ...尝试为包含空格的路径部分指定短名称,例如 PROGRA~1 而不是 Program Files。因此,您的第一个路径类似于 C:\PROGRA~1\MI6841~1\90\Tools\Binn\bcp.exe。如果没有任何空格,您应该可以删除引号。
如果您在包含长目录/文件名的目录中执行
dir /x
,您可以获得短8.3名称。Try specifying the short name for the parts of the path containing spaces For example, PROGRA~1 rather than Program Files. So, your first path would be something like C:\PROGRA~1\MI6841~1\90\Tools\Binn\bcp.exe. If you don't have any spaces, you should be able to drop the quotes.
If you perform a
dir /x
in the directory containing the long directory/file names you can obtain the short 8.3 name.作为解决方法,您可以使用
subst
。所以你不再需要间隔路径了。
或者你尝试找出失败的原因。
cmdcmdline 应该向您显示完整的命令,如果引号已经存在,那么这应该可以工作
As workaround, you could use
subst
.so you didn't need the spaced path anymore.
Or you try to find out why it fails.
The cmdcmdline should show you the complete command, if the quotes are already there this should work
这个奇怪的结构也有效:
This weird construction also working: