用UTF-8中的xp_cmdshell写入文件

问题描述 投票:2回答:3

我正在使用xp_cmdshell创建文件,如下所示:

SELECT @command = 'echo ' + @fileContent + ' > e:\out\' + @fileName + '.csv'
exec master.dbo.xp_cmdshell 'mkdir "e:\out\"'
exec master..xp_cmdshell @command 

问题是文件内容不在UTF-8中,因此某些特殊字符是错误的。

我可以用UTF-8编码创建文件吗?

file sql-server-2005 utf-8 xp-cmdshell
3个回答
1
投票

您可以使用SQLCMD代替旧的技术作为DOS输出重定向

sqlcmd -S ServerName -d DataBaseName -E -o "CSV File Path & Location" -Q "Your Query" -W -w 4000 -s ";" -f 65001

开关-f 65001指示使用UTF8作为外档文件

SELECT @command = 'sqlcmd -S ServerName -d DataBaseName -E -o "'+ @fileName +'" -Q "Your  Query" -W -w 4000 -s ";" -f 65001'
exec master.dbo.xp_cmdshell 'mkdir "e:\out\"'
exec master..xp_cmdshell @command

ps.s。我无法对其进行测试,因此请查看SQLCMD文档

希望帮助


4
投票

我最终通过将输出写入temp.txt文件并添加下一个PowerShell命令将其转换为UTF-8来成功完成了此操作:

-- Change encoding to UTF-8 with PowerShell 
SET @command = 'powershell -Command "Get-Content '+@Path+'\temp.txt -Encoding Unicode | Set-Content -Encoding UTF8 '+@path+'\'+@filename+'"';
EXEC xp_cmdshell @command;

0
投票

Jan贷款人。

我选择了您的方式。

但是,读取时间未使用该编码类型设置。

SET @COMMAND ='powershell -Command“获取内容'+ @PATH + @V_FILE_NAME +'|设置内容-编码UTF8'+ @PATH + @V_FILE_NAME +'2”']]]

这是成功。

谢谢^^

© www.soinside.com 2019 - 2024. All rights reserved.