SQL SERVER 2005 复制表

来源:互联网 发布:知远战略与防务研究所 编辑:程序博客网 时间:2024/06/10 02:32

1、SELECT * INTO FROM:要求新表不存在,将数据和表结构复制到新表,主外键等约束不会复制

SELECT * INTO 新表 FROM 旧表 WHERE 1=2  -- 仅仅复制表结构

SELECT * INTO 新表 FROM 旧表  

2、INSERT INTO:要求新表存在,将数据复制到新表

INSERT INTO 新表 SELECT * FROM 旧表 

INSERT INTO 新表(字段1,字段2,.......) SELECT 字段1,字段2,...... FROM 旧表 

例子:

--insert into select 和 select intocreate table p_table (id int primary key not null,name nvarchar(50),age int)insert into p_table values(1,'xiao八',18)insert into p_table values(2,'qiqi',17)insert into p_table values(3,'老十',20)select * from p_tablecreate table c_table (id int primary key not null,name nvarchar(50),age int)select * from c_tableinsert into c_table select * from p_tableselect * from c_table-- select intodrop table c_tableselect * into c_table from p_tableselect * from c_tabledrop table p_table,c_table 

查看表结构:

-- 查看表结构 select * from information_schema.columns where table_name = 'c_table'

查看存储过程:

exec sp_help 'c_table'