简体   繁体   English

将具有Blob和utf8字符串字段的表从MySql导出到MS Sql Server 2014

[英]exporting table with blob and utf8 string fields from MySql to MS Sql server 2014

I have a table with binary(32), blob and varchar utf-8 fields. 我有一个带有binary(32),blob和varchar utf-8字段的表。 from one mysql server to another I export data via csv: 从一台mysql服务器到另一台我通过csv导出数据:

select * INTO OUTFILE '$tmp_fname'
                  FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
                  LINES TERMINATED BY '\\r\\n'
                    from mytable

and then 接着

load data local infile '" . $mysqli->real_escape_string($glb) . "' ignore into table mytable_temp
        CHARACTER SET 'utf8'
          FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
                  LINES TERMINATED BY '\\n' 

I tried the same with bulk insert in MSSQL, and for simple types it works (I get another table with int and char(44) in it). 我在MSSQL中对大容量插入进行了相同的尝试,对于简单的类型,它也可以工作(我得到了另一个带有int和char(44)的表)。 But in this case I get import errors. 但是在这种情况下,我得到导入错误。 Some detials: I need to make automated export-import - that's why I use csv, both servers can communicate only via http (php scripts). 一些细节:我需要进行自动导出-导入-这就是为什么我使用csv的原因,两个服务器只能通过http(php脚本)进行通信。 Tables have millions of rows. 表有数百万行。

So here are questions. 所以这是问题。

  1. How blob field data should be formated in csv so that MS SQL can import it? 应如何在csv中格​​式化blob字段数据,以便MS SQL可以导入它?

  2. How can I export utf8 string for MS SQL? 如何为MS SQL导出utf8字符串? I tried convert(myfield using utf16), is it what I need? 我尝试了convert(使用utf16的myfield),这是我需要的吗?

Also I tried to export data in utf16 and specify DATAFILETYPE ='widechar' in bulk insert, but it throws an error on first int value. 我也尝试导出utf16中的数据并在批量插入中指定DATAFILETYPE ='widechar',但是它在第一个int值上引发了错误。 It can't actually read widechar? 它实际上无法读取widechar吗?

It's strange nobody from professionals knows an answer. 奇怪的是,没有专业人士知道答案。

  1. blob and binary fields should be exported as HEX( field_name ) and then imported to mssql as is. Blob和二进制字段应导出为HEX( field_name ),然后按原样导入到mssql。

By the way, the most flexible way is using format file, as having exact csv you see where quotes appear and where do not. 顺便说一句,最灵活的方法是使用格式文件,因为有了确切的csv,您可以看到引号出现在哪里和不出现在哪里。 format file description 格式文件说明

  1. to export utf8 and other non-ansi strings from mysql you should use HEX( (convert( str_field_name using utf16le) )) - you get all bytes as they are - then bulk import to intermediate mssql table and then merge or insert to the target table converting to nvarchar: cast(source.str_field_name AS nvarchar( any-length-you-need )). 要从mysql导出utf8和其他非ansi字符串,您应该使用HEX((convert( str_field_name using str_field_name )))-您str_field_name获得所有字节-然后批量导入到中间mssql表,然后合并或插入到目标表转换为nvarchar:cast(source.str_field_name AS nvarchar( any-length-you-need ))。 I spend about an hour before realized that mssql needs exactly litle endian. 我花了大约一个小时才意识到mssql确实需要一点点锂。

Don't try to 'select ... into outfile' with encoding utf16le, just leave it default, as everything we've got casting all strings to hex binary is pure ansi output. 请勿尝试使用utf16le编码将其“选择...选入outfile”,而应将其保留为默认值,因为我们将所有字符串强制转换为十六进制二进制文件的一切都是纯ansi输出。 Bulk insert somehow refused to import widechar (utf16le) csv as well as utf16be. 批量插入以某种方式拒绝导入Widechar(utf16le)csv以及utf16be。 So maybe hex-bin solution is not that fast but it is universal. 因此,也许十六进制的解决方案不是那么快,但它是通用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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