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以及“=”而不是“LIKE”时,xml文件看起来已损坏(其中只有神秘的字符):
SET @cmd = 'bcp "select * from Testdb.dbo.mytable WHERE nr = ''102065336''' + '" queryout C:\temp\sample.xml -c -t, -S' + @@servername + ' -T'
那怎么可能呢?如果我执行sql语句,查询将返回相同的数据...
答案 0 :(得分: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?所以也许这个问题出现在我们看不到的地方...... -w
代替-c
。查找details here