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

相关文章:

  • 2022-02-05
  • 2022-12-23
  • 2021-12-11
  • 2022-12-23
  • 2022-01-18
猜你喜欢
  • 2021-11-13
  • 2022-12-23
  • 2021-10-06
  • 2021-11-16
  • 2021-11-11
  • 2022-12-23
相关资源
相似解决方案