INSERT INTO 与 SELECT INTO 的区别和使用

SQL Server提供了两种快速复制表的方法,正好最近又用到了,记下:

(1)INSERT INTO TableName1 SELECT columnNames FROM TableName2 WHERE TableName2.Column1 ='' and TableName2.Column2 =''

     这种方法适用于TableName1表已经存在的情况下,把表TableName2中满足条件的记录追加到TableName1表中。

    

create table A
(
   Name nvarchar(10),
   Birthday date
)
create table B
(
   Name nvarchar(10),
   Birthday date
)

insert into A values('Lucy', '1988-01-01')
insert into A values('Luli', '1989-05-01')

insert into B(Name,Birthday)
select Name, birthday from A where A.Name = 'Lucy'

 

(2)SELECT TableName2ColumnNames INTO TableName1 FROM TableName2 WHERE TableName2.Column1 ='' and TableName2.Column2 =''

     这种方法适用于TableName1表不存在的情况下,把表TableName2中满足条件的记录复制到TableName1表中。

create table A
(
   Name nvarchar(10),
   Birthday date
)

insert into A values('Lucy', '1988-01-01')
insert into A values('Luli', '1989-05-01')

select Name, birthday into B from A where A.Name = 'Lucy'

 

     此种方法还用于快速复制表结构,用法如下

      SELECT TableName2ColumnNames INTO TableName1 FROM TableName2 WHERE 1=2

posted @ 2013-05-30 15:13  行与止  阅读(812)  评论(0编辑  收藏  举报