很多时候开发人员都会问,是否可以在单个语句中将多行插入到一个表中。 目前,开发人员在表中插入值时必须编写多个insert
语句。 它不仅枯燥乏味,而且耗时。 要摆脱这一点,应该尝试这种语法。 实际上,有三种不同的方法可以将多个值插入到单个表中。
在sql server中插入多个值 -
create table student (id int value varchar (100));
sql insert :(传统插入)
insert into student (id, name)
values (1, 'armaan');
insert into student (id, name)
values (2, 'billy');
insert into student (id, name)
values (3, 'charlie');
清理表:
truncate table student;
insert select :( select union insert)
insert into student (id, name)
select 1, 'armaan'
union all
select 2, 'billy'
union all
select 3, 'charlie';
清理表:
truncate table student;
sql server 2008+行构建
insert into student (id, name)
values (1, 'armaan'), (2, 'billy'), (3, 'charlie');