2013-10-10 79 views
3

選擇結果插入我有一個CSV文件像H2數據庫,通過CSVREAD

1,hello,13 
2,world,14 
3,ciao,26 

我試圖用CSVREAD函數讀取這個文件放到數據庫中,這樣

insert into my_table(id, message, code) values (
    select convert("id",bigint), "message", convert("code", bigint) 
    from CSVREAD('myfile.csv', 'id,message,code', null) 
); 

對於我繼續得到的某些原因SQL error stating that the column count does not match.

該表是使用Hibernate/GORM創建的,並且包含我嘗試插入的字段。

選擇本身似乎工作,或者至少它不單獨執行時不會導致任何錯誤。 我的聲明有什麼問題?

回答

8

您已經使用

insert into my_table(...) values (select ...) 

,但你應該使用,如SQL railroad diagrams記載,

insert into my_table(...) select ... 

實際上,對於H2,it is a bit faster if you create the table as follows,但我知道它並不總是可能的:

create table my_table(...) as select ... 
+0

謝謝@Thomas。在這個開發階段,GORM/Hibernate爲我創建表格,我只從CSV文件中初始化數據。當應用程序成熟時,使用create table的選項也包含數據。 – kaskelotti