使用 BCP 将数据导入 SQL Server,同时保留重音、亚洲字符等
我正在尝试使用 bcp 将 PostgreSQL 转储数据导入到 SQL Server 中。我编写了一个 Python 脚本来将分隔符切换为“^”并消除其他错误的格式,但在导入 SQL Server 时找不到正确的开关来保留字符串的 unicode 格式。
在Python中,如果我打印出给我带来麻烦的行,则该行在csv模块中看起来像这样:
['12', '\xe4\xb8\x89\xe5\x8e\x9f \xe3\x81\x95\xe3\x81\xa8\xe5\xbf\x97']
数据库表只有2列:一个integer
,一个varchar
代码>.
我用于创建表的语句(简化)仅为:
CREATE TABLE [dbo].[example](
[ID] [int] NOT NULL,
[Comment] [nvarchar](max)
)
为了运行 bcp,我使用这一行:
c:\>bcp dbo.example in fileinput -S servername -T -t^^ -c
它成功导入了大约一百万行,但我所有的重音字符都被破坏了。
例如,“Böhm,Rüdiger”会变成“B+μhm,R++diger”。有谁有如何使用 bcp 正确设置开关或其他提示的经验吗?
编辑:varchar
切换为 nvarchar
,但这并不能解决问题。 Python 中的此输出(使用 CSV 模块读取):
['62', 'B\xc3\xb6hm, R\xc3\xbcdiger']
在目标数据库的 SSMS 中显示为这样(分隔符匹配一致性):
select * from dbo.example where id = 62
62;"B├╢hm, R├╝diger"
在 pgAdmin 中,使用原始数据库,我有以下内容:
62;"Böhm, Rüdiger"
I'm trying to import a PostgreSQL dump of data into SQL Server using bcp. I've written a Python script to switches delimiters into '^' and eliminate other bad formatting, but I cannot find the correct switches to preserve unicode formatting for the strings when importing into SQL Server.
In Python, if I print out the lines that are causing me trouble, the row looks like this with the csv module:
['12', '\xe4\xb8\x89\xe5\x8e\x9f \xe3\x81\x95\xe3\x81\xa8\xe5\xbf\x97']
The database table only has 2 columns: one integer
, one varchar
.
My statement (simplified) for creating the table is only:
CREATE TABLE [dbo].[example](
[ID] [int] NOT NULL,
[Comment] [nvarchar](max)
)
And to run bcp, I'm using this line:
c:\>bcp dbo.example in fileinput -S servername -T -t^^ -c
It successfully imports about a million rows, but all of my accented characters are broken.
For example, "Böhm, Rüdiger" is turned into "B+¦hm, R++diger". Does anyone have experience with how to properly set switches or other hints with bcp?
Edit: varchar
switched to nvarchar
, but this does not fix the issue. This output in Python (reading with CSV module):
['62', 'B\xc3\xb6hm, R\xc3\xbcdiger']
is displayed as this in SSMS from the destination DB (delimiters matched for consistency):
select * from dbo.example where id = 62
62;"B├╢hm, R├╝diger"
where in pgAdmin, using the original DB, I have this:
62;"Böhm, Rüdiger"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要修改 BCP 命令以支持宽字符集(请注意使用 -w 而不是 -c 开关)
BCP 文档参考
另请参阅 http://msdn.microsoft.com/en-us/library/ms188289.aspx
You may need to modify your BCP command to support wide character sets (note the use of -w instead of -c switch)
BCP documentation reference
See also http://msdn.microsoft.com/en-us/library/ms188289.aspx
如果您需要保留 unicode,请将 varchar 更改为 nvarchar...
If you need to preserve unicode change varchar to nvarchar...