select into from 和 insert into select 的用法和区别(转)

转自:http://www.studyofnet.com/news/182.html

select into from 和 insert into select都是用来复制表,两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建。insert into select from 要求目标表存在

INSERT INTO SELECT语句:

语句形式为
 
Insert into Table2(field1,field2,...) select value1,value2,... from Table1
    --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','')
Insert into Table1 values('钱','asds','')
Insert into Table1 values('孙','asds','')
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

正确的形式:

INSERT INTO AC02_WYL
(AAZ159, BAE001, AAB001, AAC001, AAC049)
SELECT A.AAZ159, A.BAE001, A.AAB001, A.AAC001, 201411
FROM AC02 A
WHERE AAB001 = 511500000296;

如图:select into from 和 insert into select 的用法和区别(转)

 

上一篇:【Linux命令与工具】ps命令


下一篇:js简单弹出层、遮罩层