如何通过一个查询在同一个表中插入多个行?(复制)

时间:2022-08-24 21:34:19

This question already has an answer here:

这个问题已经有了答案:

I have two insert queries like these:

我有两个这样的插入查询:

INSERT INTO mytable VALUES('NULL',name1)('NULL',name2)

Those two INSERT queries above, add two separated rows to mytable. Well can I do that via single query?

上面的两个插入查询,向mytable添加两个独立的行。我可以通过单一查询来实现吗?

2 个解决方案

#1


3  

Use:

使用:

INSERT INTO mytable (id, name) VALUES 
(NULL, :name1),
(NULL, :name2)

#2


1  

Just sample example how it works

举个例子

    DECLARE @mytable TABLE 
        (Id varchar(1), name VARCHAR(10))
    ;

    INSERT INTO @mytable (id, name) VALUES (NULL, 'name1'), (NULL, 'name2')
Select * from @mytable

#1


3  

Use:

使用:

INSERT INTO mytable (id, name) VALUES 
(NULL, :name1),
(NULL, :name2)

#2


1  

Just sample example how it works

举个例子

    DECLARE @mytable TABLE 
        (Id varchar(1), name VARCHAR(10))
    ;

    INSERT INTO @mytable (id, name) VALUES (NULL, 'name1'), (NULL, 'name2')
Select * from @mytable