Write to file with xp_cmdshell in UTF-8

岁酱吖の 提交于 2019-12-04 16:38:55

You can use the SQLCMD instead the old tecnique as DOS outupt redirect

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

the switch -f 65001 indicate to use UTF8 as outfile file

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

p.s. i cant test it so please check the SQLCMD documentation

Hope it help

I finally succeeded doing this by writing the output to a temp.txt file and adding the next PowerShell command to convert it to 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;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!