INSERT INTO SELECT語句與SELECT INTO FROM語句的一些差別。本站提示廣大學習愛好者:(INSERT INTO SELECT語句與SELECT INTO FROM語句的一些差別)文章只能為提供參考,不一定能成為您想要的結果。以下是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