2

次のように 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 エンコーディングで作成できますか?

4

3 に答える 3

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;
于 2012-11-28T14:13:01.930 に答える
1

DOS出力リダイレクトとして古い手法の代わりにSQLCMDを使用できます

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テストできないので、SQLCMDのドキュメントを確認してください

それが役立つことを願っています

于 2012-05-29T11:06:34.883 に答える