H2 database, insert by selecting results from CSVREAD

别说谁变了你拦得住时间么 提交于 2019-12-08 19:13:52

问题


I have a CSV file like

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

I'm trying to use CSVREAD function to read this file into database, like this

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

For some reason I keep on getting SQL error stating that the column count does not match.

The table is created with Hibernate/GORM and contains the fields I try to insert into.

The select itself seems to work, or at least it does not cause any errors when executed alone. What's wrong with my statement?


回答1:


You have used

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

but you should use, as documented in the SQL railroad diagrams,

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

Actually, for H2, it is a bit faster if you create the table as follows, but I understand it is not always possible:

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


来源:https://stackoverflow.com/questions/19299589/h2-database-insert-by-selecting-results-from-csvread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!