简体   繁体   中英

BCP Imported File in UTF-8 Encoded File

I have generated a CSV file using BULK Copy (BCP) utility in SQL Server 2008. I want a file that will be in UTF-8 Encoding. I am using below code to generate File. Any one can help me to resolve this problem.

DECLARE @Path VARCHAR(100) ,
@sql VARCHAR(8000) ,
@DBName VARCHAR(100) ,
@exePath VARCHAR(100),

SELECT @sql = 'bcp "SELECT * FROM '+@DBName+'..ABC" queryout  '+@Path+'\'+'ABC'.csv -c -k     -t, -U sa -P abc -T -S ' + @@servername

exec @exePath @sql;

Sql Server 2008 don't support utf-8 but you can use "-w" instead "-c" that will use UTF-16 as mentionned by DiMach, the probleme is that the csv file not will be well formed.

If you trying to export csv file for specific language, for example french (french contains spécial caracters : à é â ç ...) you will need to know that there is an other solution :

  • First : try to search in the internet the encoding that support all charaters of the target language, for example for french we can found ISO-8859-1

  • Second : search in the internet a page code related to encoding that you find, for ISO-8859-1 is 1252

  • Third : execute to following sql request in your sql server to see if your sql server support this encoding

    select name, COLLATIONPROPERTY(name, 'CodePage') as Code_Page, description from sys.fn_HelpCollations() where COLLATIONPROPERTY(name, 'CodePage')=1252 order by Code_Page

  • fourth : in BCP command use -C 1252 only (delete -c and -w)

I hope that's help

只需将“ -c”参数替换为“ -w”

SELECT @sql = 'bcp "SELECT * FROM '+@DBName+'..ABC" queryout  '+@Path+'\'+'ABC'.csv -w -k     -t, -U sa -P abc -T -S ' + @@servername

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM