您的位置:首页 > 数据库

SQL Server复制表结构和数据到另一表方法及问题解决方案

2016-05-31 13:58 477 查看
对已经存在的一张表,要把该表的表结构和数据复制到另一张新表中,可以采用的方法有两种。

方法一

select * into test01_02  from test01_01;

把test01_01的表结构和数据(如果有数据)导入到test01_02表中。

注:使用这种方法的前提是test01_02表是不存在的,如果存在执行SQL语句时会报错。

方法二

set  identity_insert  test01_03  on

insert  into  test01_03(id,p_name,p_age,p_address)  select * from test01_01

set  identity_insert  test01_03  off

把test01_01的数据导入到test01_03表中,其中id为主键,整型,自动增长。

注:

a.方法二要求test01_03表存在,不存在会报错。

b.如果把方法二改写为:insert into test01_03  select * from test01_01,会报“仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'test01_03'中的标识列指定显式值”错误。

c.如果把方法二改写为:

set  identity_insert  test01_03  on

insert  into  test01_03   select * from test01_01

set  identity_insert  test01_03  off

会报“仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'test01_03'中的标识列指定显式值”错误。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息