1:SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
转至http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html
Insert是T-sql中常用语句,Insert INTO table(field1,field2,...) values(value1,value2,...)这种形式的在应用程序开发中必不可少。但我们在开发、测试过程中,经常会遇到需要表复制的情况,如将一个table1的数据的部分字段复制到table2中,或者将整个table1复制到table2中,这时候我们就要使用SELECT INTO 和 INSERT INTO SELECT 表复制语句了。
1.INSERT INTO SELECT语句
语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> --1.创建测试表
create TABLE Table1
(
a varchar(10),
b varchar(10),
c varchar(10),
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
a ASC
)
) ON [PRIMARY]
create TABLE Table2
(
a varchar(10),
c varchar(10),
d int,
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
a ASC
)
) ON [PRIMARY]
GO
--2.创建测试数据
Insert into Table1 values('赵','asds','90')
Insert into Table1 values('钱','asds','100')
Insert into Table1 values('孙','asds','80')
Insert into Table1 values('李','asds',null)
GO
select * from Table2
--3.INSERT INTO SELECT语句复制表数据
Insert into Table2(a, c, d) select a,c,5 from Table1
GO
--4.显示更新后的结果
select * from Table2
GO
--5.删除测试表
drop TABLE Table1
drop TABLE Table2
2.SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> --1.创建测试表
create TABLE Table1
(
a varchar(10),
b varchar(10),
c varchar(10),
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
a ASC
)
) ON [PRIMARY]
GO
--2.创建测试数据
Insert into Table1 values('赵','asds','90')
Insert into Table1 values('钱','asds','100')
Insert into Table1 values('孙','asds','80')
Insert into Table1 values('李','asds',null)
GO
--3.SELECT INTO FROM语句创建表Table2并复制数据
select a,c INTO Table2 from Table1
GO
--4.显示更新后的结果
select * from Table2
GO
--5.删除测试表
drop TABLE Table1
drop TABLE Table2
--author:陈超
--查询是否有template_id重复的模板(comframe里)
select template_id,count(template_id) from vm_template vt group by template_id having count(8)>1;
--查出所有重复的数据
SELECT ROWID,NCA.CLASS_NAME FROM NG_CFG_AUDIT NCA WHERE NCA.CLASS_NAME IN
(SELECT NCA2.CLASS_NAME FROM NG_CFG_AUDIT NCA2 GROUP BY NCA2.CLASS_NAME HAVING COUNT(*)>1);
--查询多余的数据的rowid最大的一条
SELECT MAX(NCA3.ROWID),NCA3.CLASS_NAME FROM NG_CFG_AUDIT NCA3 GROUP BY NCA3.CLASS_NAME HAVING COUNT(*)>1;
--查询多出来的数据
(
SELECT NCA.ROWID,NCA.CLASS_NAME FROM NG_CFG_AUDIT NCA WHERE NCA.CLASS_NAME IN
(SELECT NCA2.CLASS_NAME FROM NG_CFG_AUDIT NCA2 GROUP BY NCA2.CLASS_NAME HAVING COUNT(*)>1)
)
MINUS
(
SELECT MAX(NCA3.ROWID),NCA3.CLASS_NAME FROM NG_CFG_AUDIT NCA3 GROUP BY NCA3.CLASS_NAME HAVING COUNT(*)>1
);
--删除多余的数据(对于多的只保留一条)
DELETE FROM NG_CFG_AUDIT NCA4 WHERE NCA4.ROWID IN(
(
SELECT NCA.ROWID FROM NG_CFG_AUDIT NCA WHERE NCA.CLASS_NAME IN
(SELECT NCA2.CLASS_NAME FROM NG_CFG_AUDIT NCA2 GROUP BY NCA2.CLASS_NAME HAVING COUNT(*)>1)
)
MINUS
(
SELECT MAX(NCA3.ROWID) FROM NG_CFG_AUDIT NCA3 GROUP BY NCA3.CLASS_NAME HAVING COUNT(*)>1
)
);
SELECT ROWID,NCA.* FROM NG_CFG_AUDIT NCA WHERE NCA.CLASS_NAME='com.asiainfo.boss.channel.ng.channelinfo.service.impl.PopularizeCostDrawInfoSVImpl';
注:如果多个字段联合唯一去重,则可以用||将它们连接起来in操作