SQL Server之SELECT INTO 和 INSERT INTO SELECT案例详解
作者:壊壊—娚孩 时间:2024-01-22 01:11:56
做数据库开发的过程中难免会遇到有表数据备份的,而SELECT INTO……和INSERT INTO SELECT…… 这两种语句就是用来进行表数据复制,下面简单的介绍下:
1、INSERT INTO SELECT
语句格式:Insert Into Table2(column1,column2……) Select value1,value2,value3,value4 From Table1 或 Insert Into Table2 Select * From Table1
说明:这种方式的表复制必须要求Table2是事先创建好的
例:
--1.创建表
create TABLE Table1
(
a varchar(10),
b varchar(10),
c varchar(10)
) ;
create TABLE Table2
(
a varchar(10),
c varchar(10),
d varchar(10)
);
commit;
--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);
commit;
--3.复制table1数据到table2中
Insert into Table2(a, c, d) select a,b,c from Table1;
commit;
--或,此种方式必须要求table2和table1的列数相等,而且类型兼容
Insert into Table2 select * from table1;
commit;
以上这些sql在oracle和MS SqlServer中的语法是一样的,可以通用.
2、SELECT INTO……
这种方式的语句可以在Table2不存在的时候进行表数据复制,编译器会根据Table1的表结构自动创建Table2,Table2和Table1的结构基本上是一致的,但是如果已经存在Table2,则编译器会报错.
这种方式的语句在Oracle中和MS SqlServer中是有点差别的,,如下:
语句格式:
Oracle:Create Table2 as Select column1,column2……From Table1 或 Create Table2 as Select * From Table1
MS SqlServer:Select column1,column2…… into Table2 From Table1 或 Select * into Table2 From Table1
例:
--Oracle
--1.创建表
create TABLE Table1
(
a varchar(10),
b varchar(10),
c varchar(10)
) ;
commit;
--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);
commit;
--3.复制table1数据到table2中
Create Table Table2 as select a,b,c From table1;
Commit;
--或(这两种方式的sql只能应用一次)
Create table table2 as select * From Table1;
Commit;
--删除表
drop table table1;
drop table table2;
commit;
--MS SqlServer
--1.创建表
create TABLE Table1
(
a varchar(10),
b varchar(10),
c varchar(10)
) ;
commit;
--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);
commit;
--3.复制table1数据到table2中
Select a,b,c into Table2 From table1;
Commit;
--或(这两种方式的sql只能应用一次)
Select * into table2 From Table1;
Commit;
--删除表
drop table table1;
drop table table2;
commit;
来源:https://www.cnblogs.com/NaughtyBoy/p/3153082.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Go 语言的 :=的具体使用
![](https://img.aspxhome.com/file/2023/9/127329_0s.png)
mysql 5.7.19 winx64免安装版配置教程
![](https://img.aspxhome.com/file/2023/8/129908_0s.jpg)
PS中执行N遍选定动作的脚本
![](https://img.aspxhome.com/file/UploadPic/20082/22/2008222213917983s.gif)
Python通过requests模块实现抓取王者荣耀全套皮肤
![](https://img.aspxhome.com/file/2023/7/131167_0s.png)
Python使用Mediapipe对图像进行手部地标检测
![](https://img.aspxhome.com/file/2023/5/114745_0s.jpg)
七十六个网站用户体验要点
MySQL中datetime时间字段的四舍五入操作
![](https://img.aspxhome.com/file/2023/6/107716_0s.png)
TensorFlow实现模型断点训练,checkpoint模型载入方式
![](https://img.aspxhome.com/file/2023/4/80824_0s.jpg)
JavaScript实现秒杀时钟倒计时
![](https://img.aspxhome.com/file/2023/6/126176_0s.jpg)
Python使用ElementTree美化XML格式的操作
![](https://img.aspxhome.com/file/2023/7/135017_0s.jpg)
php中fgetcsv()函数用法实例
asp用正则解析远程图片地址,用XMLHTTP将其保存
Pyqt5 实现跳转界面并关闭当前界面的方法
php和asp利用Shell.Application来执行程序的代码
python交互式图形编程实例(一)
python使用正则表达式匹配反斜杠\\遇到的问题
![](https://img.aspxhome.com/file/2023/6/131416_0s.png)
详解Python中 sys.argv[]的用法简明解释
![](https://img.aspxhome.com/file/2023/6/131706_0s.png)