当前位置:Gxlcms > 数据库问题 > SQL中一次插入多条数据

SQL中一次插入多条数据

时间:2021-07-01 10:21:17 帮助过:21人阅读

top 0 * into newstudent from student insert into newstudent select * from student

这里会发生这样的报错:

技术分享

因为NewClass表中ClassId为标识列,所以我们不能插入值。

我们的解决办法如下:

select top 0 * into newstudent from  student
set identity_insert newstudent on
insert into newstudent (classid,classname) select * from student

我们把newstudent 的标识列插入写为显示的,并且添加了列名字段便可以插入了。

之后我们再创建一个新的NewClass2:

select top 0 *into NewClass2 from MyClass
set identity_insert NewClass2 on
insert into NewClass2(ClassId,ClassName) select* from MyClass

此时还会报错,这是因为我们之前设置了newclass的标识列插入为on,我们必须先关闭后才可以往newclass2插入值,代码如下:

select top 0 *into NewClass2 from MyClass
set identity_insert newclass off
set identity_insert NewClass2 on
insert into NewClass2(ClassId,ClassName) select* from MyClass

至此我们解决了使用第二种方法一次插入多条数据。

3.

语法:insert into 表(字段列名) select 值 union select值

 

SQL中一次插入多条数据

标签:name   span   new   blog   sel   显示   style   identity   标识   

人气教程排行