select into from 和 insert into select 的用法和区别
2014-07-03 17:57
447 查看
--查看同一张表中 吗,某一个是否字段重复
SELECT a.name,c.collegeName FROM tb_person a,tb_student c WHERE a.uuid = c.uuid and a.socialId in (
SELECT b.socialId
FROM tb_person b
GROUP BY b.socialId
HAVING COUNT(b.socialId) > 1 )
--insert into select
insert into relamanagedaccountandrole(managedAccountId,roleId) select b.accountId,'4028814842a8246d0142a8275a510002' from tb_student a,tb_person b where a.uuid = b.uuid and a.collegeId='402881884370b5bd014370bf8bad0002'
and accountId not in (select managedAccountId from relamanagedaccountandrole)
--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
SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中 。
--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
相关文章推荐
- select into from 和 insert into select 的用法和区别复制表数据
- select into from 和 insert into select 的用法和区别
- insert into select 和select into from的用法和区别(转)
- select into from 和 insert into select 的用法和区别
- SELECT INTO FROM 和 INSERT INTO SELECT 的用法和区别
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别
- insert into select 和select into from的用法和区别
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的区别和用法及 SQL SELECT INTO 中Undeclared variable错误解决办法
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别
- insert into select 和select into from的用法和区别
- select into from 和 insert into select 的用法和区别
- SELECT INTO FROM 和 INSERT INTO SELECT 的用法和区别
- select into from 和 insert into select 的用法和区别
- select into from 和 insert into select 的用法和区别(转)
- select into from 和 insert into select 的用法和区别