select into from 和 insert into select 的用法和区别复制表数据
来源:互联网 发布:java内嵌webkit浏览器 编辑:程序博客网 时间:2024/04/30 20:44
select into from 和 insert into select都是用来复制表,两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建。insert into select from 要求目标表存在
下面分别介绍两者语法
INSERT INTO SELECT语句
语句形式为:
Insert into Table2(field1,field2,...) select value1,value2,... from Table1
注意地方:
(1)要求目标表Table2必须存在,并且字段field,field2...也必须存在
(2)注意Table2的主键约束,如果Table2有主键而且不为空,则 field1, field2...中必须包括主键
(3)注意语法,不要加values,和插入一条数据的sql混了,不要写成:
Insert into Table2(field1,field2,...) values (select value1,value2,... from Table1)
(4)由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。
完整实例:
SQL 代码 复制
--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中 。
完整实例:
SQL 代码 复制
--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
0 0
- select into from 和 insert into select 的用法和区别复制表数据
- insert into select 和 select into from 用法和区别
- select into from 和 insert into from 的区别
- insert into from和select from into的区别
- 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 的用法和区别
- 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 的用法和区别
- python编程常用模板总结
- WebService上传和下载文件
- python基础学习二 list和tuple(列表和元组)
- Hbernate一对多和多对一关系详解
- 一路PN码串行捕获设计--基于《通信收发信机的verilog实现与仿真》实例
- select into from 和 insert into select 的用法和区别复制表数据
- Android横竖屏切换小结
- Cocos2d-x shader学习2: 模糊(Blur)
- 项目管理-常用工具推荐
- echarts报错ReferenceError: require is not defined
- python 基础 特殊符号的使用
- Mac用iTerm2连接到Linux上,不能输入中文
- 任务调度的 Java 实现方法二:quartz
- 《用户体验要素》读后感