1 1.INSERT INTO SELECT语句 2 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1 3 4 要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下: 5 6 INSERT INTO SELECT语句复制表数据 7 --1.创建测试表 8 create TABLE Table1 9 ( 10 a varchar(10), 11 b varchar(10), 12 c varchar(10), 13 CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 14 ( 15 a ASC 16 ) 17 ) ON [PRIMARY] 18 19 create TABLE Table2 20 ( 21 a varchar(10), 22 c varchar(10), 23 d int, 24 CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED 25 ( 26 a ASC 27 ) 28 ) ON [PRIMARY] 29 GO 30 --2.创建测试数据 31 Insert into Table1 values('赵','asds','90') 32 Insert into Table1 values('钱','asds','100') 33 Insert into Table1 values('孙','asds','80') 34 Insert into Table1 values('李','asds',null) 35 GO 36 select * from Table2 37 38 --3.INSERT INTO SELECT语句复制表数据 39 Insert into Table2(a, c, d) select a,c,5 from Table1 40 GO 41 42 --4.显示更新后的结果 43 select * from Table2 44 GO 45 --5.删除测试表 46 drop TABLE Table1 47 drop TABLE Table2 48 49 2.SELECT INTO FROM语句 50 51 语句形式为:SELECT vale1, value2 into Table2 from Table1 52 53 要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下: 54 55 SELECT INTO FROM创建表并复制表数据 56 --1.创建测试表 57 create TABLE Table1 58 ( 59 a varchar(10), 60 b varchar(10), 61 c varchar(10), 62 CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 63 ( 64 a ASC 65 ) 66 ) ON [PRIMARY] 67 GO 68 69 --2.创建测试数据 70 Insert into Table1 values('赵','asds','90') 71 Insert into Table1 values('钱','asds','100') 72 Insert into Table1 values('孙','asds','80') 73 Insert into Table1 values('李','asds',null) 74 GO 75 76 --3.SELECT INTO FROM语句创建表Table2并复制数据 77 select a,c INTO Table2 from Table1 78 GO 79 80 --4.显示更新后的结果 81 select * from Table2 82 GO 83 --5.删除测试表 84 drop TABLE Table1 85 drop TABLE Table2
转:http://www.cnblogs.com/netsa/archive/2011/10/26/2225625.html