ORACLE匯出文字到MYSQL 報錯 Incorrect integer value: ''
今天從ORACLE匯出資料文字格式的到MYSQL 5.6 發現
load data infile Incorrect date value ''
這種報錯,從5.6開始在嚴格模式下插入資料如果是數字和日期
mysql> insert into testnull1 values('','test','');
ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1
mysql> insert into testnull1 values(1,'test','');
ERROR 1292 (22007): Incorrect datetime value: '' for column 'dt' at row 1
會報錯,實際上我們希望''為NULL;
5.6如果表結構是如果表是int,varchar,datetime
後如果透過 load data infile 如果記錄如下
,test,
同樣會把空值轉換為''會報如上的錯,這個時候就要用\N來代替。
那麼如果從ORACLE中匯出
select nvl(to_char(id),'\N'),nvl(name,'\N'),to_char(dt,'yyyy-mm-dd hh24:mi:ss') from testnulli;
將NULL值轉換為\N如下:
create table testnulli
(id int,name varchar2(20),dt date);
insert into testnulli
values(1,'gaopeng',to_date('2010-10-10 12:02:01','YYYY-MM-DD HH24:MI:SS'));
insert into testnulli
values(null,'gaopeng',null);
DECLARE
row_result varchar2(4000);
selectsql varchar2(4000);
qrycursor SYS_REFCURSOR;
txt_handle UTL_FILE.file_type;
BEGIN
--selectsql := 'select id || '','' || name || '','' || dti from testdump where name = ''gaopeng'' and dti > to_date(''2015-03-17 00:00:00'', ''yyyy-mm-dd hh24:mi:ss'')';
selectsql := 'select nvl(to_char(id),''\N'')||'',''||nvl(name,''\N'')||'',''||nvl(to_char(dt,''yyyy-mm-dd hh24:mi:ss''),''\N'') from testnulli';
txt_handle := UTL_FILE.FOPEN('DATA_PUMP_DIR', 'testnull.txt', 'w', 32767);
open qrycursor for selectsql;
loop
fetch qrycursor
into row_result;
exit when qrycursor%notfound;
UTL_FILE.PUT_LINE(txt_handle, row_result);
end loop;
--關閉遊標
close qrycursor;
UTL_FILE.FCLOSE(txt_handle);
end;
匯出的文字如下:
1,gaopeng,2010-10-10 12:02:01
\N,gaopeng,\N
然後再次 load data infile 就不會出問題
mysql> load data infile '/tmp/testnull.txt' into table testnull1 fields terminated by ',' ;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from testnull1;
+------+---------+---------------------+
| id | name | dt |
+------+---------+---------------------+
| 1 | gaopeng | 2010-10-10 12:02:01 |
| NULL | gaopeng | NULL |
+------+---------+---------------------+
2 rows in set (0.02 sec)
實際上
mysql> insert into testnull1 values(\N,'test',\N);
Query OK, 1 row affected (0.00 sec)
等價於
mysql> insert into testnull1 values(null,'test',null);
Query OK, 1 row affected (0.01 sec)
load data infile Incorrect date value ''
這種報錯,從5.6開始在嚴格模式下插入資料如果是數字和日期
mysql> insert into testnull1 values('','test','');
ERROR 1366 (HY000): Incorrect integer value: '' for column 'id' at row 1
mysql> insert into testnull1 values(1,'test','');
ERROR 1292 (22007): Incorrect datetime value: '' for column 'dt' at row 1
會報錯,實際上我們希望''為NULL;
5.6如果表結構是如果表是int,varchar,datetime
後如果透過 load data infile 如果記錄如下
,test,
同樣會把空值轉換為''會報如上的錯,這個時候就要用\N來代替。
那麼如果從ORACLE中匯出
select nvl(to_char(id),'\N'),nvl(name,'\N'),to_char(dt,'yyyy-mm-dd hh24:mi:ss') from testnulli;
將NULL值轉換為\N如下:
create table testnulli
(id int,name varchar2(20),dt date);
insert into testnulli
values(1,'gaopeng',to_date('2010-10-10 12:02:01','YYYY-MM-DD HH24:MI:SS'));
insert into testnulli
values(null,'gaopeng',null);
DECLARE
row_result varchar2(4000);
selectsql varchar2(4000);
qrycursor SYS_REFCURSOR;
txt_handle UTL_FILE.file_type;
BEGIN
--selectsql := 'select id || '','' || name || '','' || dti from testdump where name = ''gaopeng'' and dti > to_date(''2015-03-17 00:00:00'', ''yyyy-mm-dd hh24:mi:ss'')';
selectsql := 'select nvl(to_char(id),''\N'')||'',''||nvl(name,''\N'')||'',''||nvl(to_char(dt,''yyyy-mm-dd hh24:mi:ss''),''\N'') from testnulli';
txt_handle := UTL_FILE.FOPEN('DATA_PUMP_DIR', 'testnull.txt', 'w', 32767);
open qrycursor for selectsql;
loop
fetch qrycursor
into row_result;
exit when qrycursor%notfound;
UTL_FILE.PUT_LINE(txt_handle, row_result);
end loop;
--關閉遊標
close qrycursor;
UTL_FILE.FCLOSE(txt_handle);
end;
匯出的文字如下:
1,gaopeng,2010-10-10 12:02:01
\N,gaopeng,\N
然後再次 load data infile 就不會出問題
mysql> load data infile '/tmp/testnull.txt' into table testnull1 fields terminated by ',' ;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from testnull1;
+------+---------+---------------------+
| id | name | dt |
+------+---------+---------------------+
| 1 | gaopeng | 2010-10-10 12:02:01 |
| NULL | gaopeng | NULL |
+------+---------+---------------------+
2 rows in set (0.02 sec)
實際上
mysql> insert into testnull1 values(\N,'test',\N);
Query OK, 1 row affected (0.00 sec)
等價於
mysql> insert into testnull1 values(null,'test',null);
Query OK, 1 row affected (0.01 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-1815013/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- kettle建立資源庫Incorrect integer value
- 【BUG記錄】MySQL插入Emoji表情報錯"Incorrect string value"MySql
- 【MySQL】Incorrect string value 問題一則MySql
- 解決 Incorrect datetime value: '0000-00-00 00:00:00' 報錯
- mysql匯入報錯Variable 'sql_notes' can't be set to the value of 'NULL'MySqlNull
- 使用TOAD(8.5.3)開啟ORACLE10.2中儲存過程報錯:'IN' is not a valid integer valueOracle儲存過程
- 【ORACLE 匯入匯出】exp 錯誤Oracle
- oracle 9.2.0.7 + hp_unix exp匯出報錯處理Oracle
- Mybatis Data truncation: Truncated incorrect DOUBLE value: '*'MyBatis
- 將資料從文字匯入到mysql(轉)MySql
- mysql 報錯:ERROR 1366 (HY000): Incorrect string value: ‘\xD5\xC5\xC8\xFD‘ for column ‘name‘ at row 1MySqlError
- mysql 5.5.9 匯出匯入到5.1.41 的問題MySql
- MySQL 5.5 報錯"ERROR 1075 (42000): Incorrect table definition"MySqlError
- java.sql.SQLException: Incorrect string valueJavaSQLException
- 【MySQL報錯】1366 - Incorrect string value: ‘\xE6\x80\xBB\xE7\xBB\x8F...‘ for column ‘name‘ at row 1MySql
- SQLSERVER匯出TXT文字檔案,ORACLE SQL LOADER匯入TXT文字檔案SQLServerOracle
- mysql匯入報錯怎麼解決?MySql
- mysql出現Unknown or incorrect time zone: 'NULL'MySqlNull
- MySQL錯誤Incorrect file format解決方案薦MySqlORM
- 將informix匯出的文字資料匯入oracle資料庫ORMOracle資料庫
- MySQL: Incorrect string value: '\xF0\xA4\xBD\x82'分析MySql
- MySQL ERROR 1366(HY000):Incorrect string value:''for column''at row 1解決方案MySqlError
- MySQL 亂碼實戰解決ERROR 1366 (HY000): Incorrect string value: 'MySqlError
- 【MySQL】ERROR 1290 (HY000): --secure-file-priv--匯出報錯MySqlError
- 9i匯出11gR2庫報錯ORA-01455: converting column overflows integer datatype
- Mysql匯入&匯出MySql
- Mysql匯入匯出MySql
- mysql匯出select結果到檔案MySql
- 轉oracle資料泵匯出時報錯Oracle
- Oracle Spool的用法小結以及兩種方法的比較----------匯出記錄到文字Oracle
- mysql 命令匯入匯出MySql
- mysql匯入匯出慢MySql
- mysql 匯入、匯出命令MySql
- Mysql資料庫使用Navicat Mysql匯入sql檔案報錯MySql資料庫
- MySQL 5.7 Invalid default value for 'CREATE_TIME'報錯的解決方法MySql
- 記一次 oracle expdp 匯出錯誤Oracle
- oracle 10g expdp匯出報錯ora-4031的解決方法Oracle 10g
- 記一次 打包報錯:Keystore was tampered with, or password was incorrect