posts - 27,comments - 1,views - 33万

做数据库开发的过程中难免会遇到有表数据备份的,而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;
View Code
复制代码

 以上这些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;
View Code
复制代码
复制代码
--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;
View Code
复制代码

 

  说明:由于比较懒,文章中的代码引自http://www.cnblogs.com/freshman0216/archive/2008/08/15/1268316.html,稍作修改

posted on   壊壊—娚孩  阅读(19278)  评论(1编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示