INSERT INTO SELECT语句与SELECT INTO FROM语句的一些区别
王虎高芳,长春微信公众平台开发,武平二手房出售
1.insert into select语句
语句形式为:insert into table2(field1,field2,...) select value1,value2,... from table1
要求目标表table2必须存在,由于目标表table2已经存在,所以我们除了插入源表table1的字段外,还可以插入常量。示例如下:
insert into select语句复制表数据
--1.创建测试表
create table table1
(
a varchar(10),
b varchar(10),
c varchar(10),
constraint [pk_table1] primary key clustered
(
a asc
)
) on [primary]
create table table2
(
a varchar(10),
c varchar(10),
d int,
constraint [pk_table2] primary key clustered
(
a asc
)
) on [primary]
go
--2.创建测试数据
insert into table1 values('赵','asds','90')
insert into table1 values('钱','asds','100')
insert into table1 values('孙','asds','80')
insert into table1 values('李','asds',null)
go
select * from table2
--3.insert into select语句复制表数据
insert into table2(a, c, d) select a,c,5 from table1
go
--4.显示更新后的结果
select * from table2
go
--5.删除测试表
drop table table1
drop table table2
2.select into from语句 语句形式为:select vale1, value2 into table2 from table1
要求目标表table2不存在,因为在插入时会自动创建表table2,并将table1中指定字段数据复制到table2中。示例如下:
select into from创建表并复制表数据
--1.创建测试表
create table table1
(
a varchar(10),
b varchar(10),
c varchar(10),
constraint [pk_table1] primary key clustered
(
a asc
)
) on [primary]
go
--2.创建测试数据
insert into table1 values('赵','asds','90')
insert into table1 values('钱','asds','100')
insert into table1 values('孙','asds','80')
insert into table1 values('李','asds',null)
go
--3.select into from语句创建表table2并复制数据
select a,c into table2 from table1
go
--4.显示更新后的结果
select * from table2
go
--5.删除测试表
drop table table1
drop table table2
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
相关文章:
-
-
sql某个日期是当年的第几周
/* *周一作为一周的开始 *当年的1月1号所在的周算作第一周 */ CREATE function GetWeekIndexFirstDate ( @...
[阅读全文]
-
-
数据库SQL---范式
1、数据冗余导致的问题:冗余存储、更新异常、插入异常、删除异常。 2、函数依赖:一种完整性约束。 在关系模式r(R)中,α属于R,β属于R。 1)α函数...
[阅读全文]
-
-
数据库SQL---查询
1、查询所有列 select *from emp;--*表示所有的,from emp表示从emp表中查询。 2、查询指定列 select empno,e...
[阅读全文]
-
-
-
-
-
网友评论