查询XML到文件需要了解一点字符编码...
尝试这个:
USE master;
GO
CREATE DATABASE TestDB;
GO
CREATE TABLE TestDB.dbo.TestTbl(TestString1 VARCHAR(100),TestString2 NVARCHAR(100));
INSERT INTO TestDB.dbo.TestTbl VALUES('abc', N'abc')
,('<&> Russian: "слов в тексте"', N'<&> Russian: "слов в тексте"');
GO
SELECT * FROM TestDB.dbo.TestTbl FOR XML PATH('Test'),ROOT('String');
/* The result of the simple SELECT shows, that VARCHAR cannot deal with russian letters
<String>
<Test>
<TestString1>abc</TestString1>
<TestString2>abc</TestString2>
</Test>
<Test>
<TestString1><&> Russian: "???? ? ??????"</TestString1>
<TestString2><&> Russian: "слов в тексте"</TestString2>
</Test>
</String>
*/
GO
--Two times the same command, only difference is -w instead of -c:
--And: YOU HAVE TO FULLY QUALIFY THE OBJECT's NAME!
DECLARE @cmd1 VARCHAR(4000)='bcp "SELECT * FROM TestDB.dbo.TestTbl FOR XML PATH(''object''),ROOT(''objects'');" queryout "c:\dvp\test1.xml" -T -c -S' + @@SERVERNAME;
DECLARE @cmd2 VARCHAR(4000)='bcp "SELECT * FROM TestDB.dbo.TestTbl FOR XML PATH(''object''),ROOT(''objects'');" queryout "c:\dvp\test2.xml" -T -w -S' + @@SERVERNAME;
EXECUTE master..xp_cmdshell @cmd1;
EXECUTE master..xp_cmdshell @cmd2;
GO
DROP DATABASE TestDb;
GO
第一个命令 with 的结果-c
现在将破坏俄语字母,即使使用NVARCHAR
<objects>
<object>
<TestString1>abc</TestString1>
<TestString2>abc</TestString2>
</object>
<object>
<TestString1><&> Russian: "???? ? ??????"</TestString1>
<TestString2><&> Russian: "???? ? ??????"</TestString2>
</object>
</objects>
第二个命令的结果与-w
预期的一样:
<objects>
<object>
<TestString1>abc</TestString1>
<TestString2>abc</TestString2>
</object>
<object>
<TestString1><&> Russian: "???? ? ??????"</TestString1>
<TestString2><&> Russian: "слов в тексте"</TestString2>
</object>
</objects>