2017-04-26 66 views
0
declare @cmd nvarchar(255) 

SET @cmd = 'bcp "select * from Testdb.dbo.mytable WHERE nr LIKE ''%102065336''' + '" queryout C:\temp\sample.xml -c -t, -S' + @@servername + ' -T' 

exec xp_cmdshell @cmd 

代码返回与正确格式的正确的.xml文件,但是当我使用这个@cmd所以用的“=”,而不是“喜欢”的XML文件如下破(只神秘的字符在它):奇怪的XML-出口行为

SET @cmd = 'bcp "select * from Testdb.dbo.mytable WHERE nr = ''102065336''' + '" queryout C:\temp\sample.xml -c -t, -S' + @@servername + ' -T' 

那么这怎么可能?查询返回相同的数据,如果我执行sql语句...

回答

1

我不能重现这一点。检查出来:

USE master; 
GO 
CREATE DATABASE tstDB; 
GO 
USE tstDB; 

CREATE TABLE mytable(nr VARCHAR(100)); 
GO 
INSERT INTO mytable VALUES('11'),('12'),('22'); 
GO 

declare @cmd nvarchar(255); 

SET @cmd = 'bcp "select * from tstDB.dbo.mytable WHERE nr LIKE ''%11'' FOR XML AUTO' + '" queryout C:\temp\sample1.xml -c -t, -S' + @@servername + ' -T'; 

exec xp_cmdshell @cmd; 

SET @cmd = 'bcp "select * from tstDB.dbo.mytable WHERE nr = ''11'' FOR XML AUTO' + '" queryout C:\temp\sample2.xml -c -t, -S' + @@servername + ' -T'; 

exec xp_cmdshell @cmd; 
GO 
USE master; 
GO 
--careful with real data! 
DROP DATABASE tstDB; 
GO 

一些想法:

  • 您谈谈XML export,但你看不产生任何XML代码?所以,也许问题是,我们不能看到的区域...
  • 你申报你的命令的大小为255,这是非常小的...也许,是一些被截断
  • generall暗示:使用当您导出XML时,请使用-w而不是-c。查找details here
+0

-w做到了,谢谢先生! –