sql-server-2005 - Microsoft SQL xp_cmdshell 不喜欢带有空格的文件名。我可以用别的东西代替空格吗?

标签 sql-server-2005 tsql batch-file 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 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/

相关文章:

查看中的 SQL 计数作为列

sql-server - 指定多个排序规则

sql - 如何在标签中生成带有非法字符的xml

java - 从java代码运行批处理文件代码

asp.net - 什么是 SQL Server 报告服务?

sql-server - 如何在 SQL Server 2005 中从 IndexId 获取索引名称

sql - 选择存储过程返回的列

sql-server - SQL 服务器 : birthdays on Leap Year

batch-file - 将每个分隔符的值拆分为单独的行 - 批处理

windows - 用于静默安装多个程序的批处理文件