在SQL中添加多行

时间:2013-01-25 16:54:46

标签: sql sqlite insert

我被要求在表格中添加8行。

insert into Rating ( rID, mID, stars, ratingDate ) 
values ('207', '101', '5', null), ('207', '102', '5', null),
       ('207', '103', '5', null), ('207', '104', '5', null),
       ('207', '105', '5', null), ('207', '106', '5', null),
       ('207', '107', '5', null), ('207', '108', '5', null)

此操作适用于添加一个值,但添加多个时会产生错误

  

查询无法执行:接近“,”:语法错误

缺少什么?

4 个答案:

答案 0 :(得分:3)

迟到的回答 如果您使用的是SQLITE版本3.7.11或更高版本,则可以通过此语法插入多行

简单

INSERT INTO Rating (rID, mID, stars, ratingDate) VALUES ('207', '102', '5', null) , ('207', '102', '5', null) , ('207', '102', '5', null)

如果使用新的SQLITE版本,上述条款确实有效。

SELECT CLAUSE

insert into Rating 
        SELECT '207' AS rID, '101' AS mID, '5' AS stars, null AS ratingDate   
  UNION SELECT '207', '102', '5', null
  UNION SELECT '207', '103', '5', null
  UNION SELECT '207', '104', '5', null
  UNION SELECT '207', '105', '5', null
  UNION SELECT '207', '106', '5', null            
  UNION SELECT '207', '107', '5', null
  UNION SELECT '207', '108', '5', null

或SQL

insert into Rating (rID, mID, stars, ratingDate)
        SELECT '207', '101', '5', null 
  UNION SELECT '207', '102', '5', null
  UNION SELECT '207', '103', '5', null
  UNION SELECT '207', '104', '5', null
  UNION SELECT '207', '105', '5', null
  UNION SELECT '207', '106', '5', null            
  UNION SELECT '207', '107', '5', null
  UNION SELECT '207', '108', '5', null

请记住我不想在上面插入的值集中检查重复,然后使用UNION ALL代替UNION,因为它会快一点。

答案 1 :(得分:0)

我认为您的RDBMS不支持此类构造。

insert into Rating ( rID, mID, stars, ratingDate ) 
values ('207', '101', '5', null);
insert into Rating ( rID, mID, stars, ratingDate ) 
values ('207', '102', '5', null);
.....

答案 2 :(得分:0)

我建议:

insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '101', '5', null);
insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '102', '5', null);
...
insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '108', '5', null);

答案 3 :(得分:0)

我在sql lite中创建了表。表创建脚本如下

 create table Rating (rID varchar(10),mID varchar(10),stars varchar(10),ratingDate date);

我使用以下查询插入上表,它对我来说很好。

insert into Rating ( rID, mID, stars, ratingDate ) 
values ('207', '101', '5', null), ('207', '102', '5', null),
       ('207', '103', '5', null), ('207', '104', '5', null),
       ('207', '105', '5', null), ('207', '106', '5', null),
       ('207', '107', '5', null), ('207', '108', '5', null);