SQL:在一个语句中插入多组值?

时间:2021-04-08 10:26:12

Is it possible to insert multiple sets of values to a SQLite table in one statement?

是否可以在一个语句中向SQLite表插入多组值?

I was trying:

我在努力:

INSERT INTO the_table VALUES (1,2,'hi'),(2,0,'foo');

插入the_table VALUES(1,2,'hi'),(2,0,'foo');

with the different ()s representing different insert sets, but I get an error.

使用不同的()代表不同的插入集,但是我得到一个错误。

3 个解决方案

#1


Are there only three columns in your table? If not, you could try defining the column names you are setting like so:

表中只有三列吗?如果没有,您可以尝试定义您设置的列名称,如下所示:

INSERT INTO the_table 
       (column1  ,column2  ,column3) 
VALUES (1        ,2        ,'hi'   )
      ,(2        ,0        ,'foo'  )

This convention was introduced in SQL Server 2008 known as the Table Value Constructor. See MSDN's INSERT page for a look at the overall syntax. Also, the INSERT statement can be easily formatted for better readability.

此约定是在SQL Server 2008中引入的,称为表值构造函数。有关整体语法的信息,请参阅MSDN的INSERT页面。此外,INSERT语句可以轻松格式化,以提高可读性。

#2


You can do

你可以做

INSERT INTO the_table 
SELECT 1,2,'hi'
UNION
SELECT 2,0,'foo';

#3


I was found that syntax in MSDN but after trying I can't do that too, than I note that in the bottom of the page was written that there is an error in the page :) where is link http://msdn.microsoft.com/en-us/library/ms174335.aspx see the bottom How to insert multiple rows

我在MSDN中发现了语法,但在尝试之后我也做不到,比我注意到在页面底部写的是页面中有错误:)其中是链接http://msdn.microsoft .com / zh-cn / library / ms174335.aspx请参阅底部如何插入多行

#1


Are there only three columns in your table? If not, you could try defining the column names you are setting like so:

表中只有三列吗?如果没有,您可以尝试定义您设置的列名称,如下所示:

INSERT INTO the_table 
       (column1  ,column2  ,column3) 
VALUES (1        ,2        ,'hi'   )
      ,(2        ,0        ,'foo'  )

This convention was introduced in SQL Server 2008 known as the Table Value Constructor. See MSDN's INSERT page for a look at the overall syntax. Also, the INSERT statement can be easily formatted for better readability.

此约定是在SQL Server 2008中引入的,称为表值构造函数。有关整体语法的信息,请参阅MSDN的INSERT页面。此外,INSERT语句可以轻松格式化,以提高可读性。

#2


You can do

你可以做

INSERT INTO the_table 
SELECT 1,2,'hi'
UNION
SELECT 2,0,'foo';

#3


I was found that syntax in MSDN but after trying I can't do that too, than I note that in the bottom of the page was written that there is an error in the page :) where is link http://msdn.microsoft.com/en-us/library/ms174335.aspx see the bottom How to insert multiple rows

我在MSDN中发现了语法,但在尝试之后我也做不到,比我注意到在页面底部写的是页面中有错误:)其中是链接http://msdn.microsoft .com / zh-cn / library / ms174335.aspx请参阅底部如何插入多行