濮阳杆衣贸易有限公司

主頁 > 知識庫 > SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解

SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解

熱門標簽:在電子版地圖標注要收費嗎 實體店地圖標注怎么標 外呼系統(tǒng)會封嗎 電銷機器人 深圳 南京電銷外呼系統(tǒng)哪家好 萬利達綜合醫(yī)院地圖標注點 地圖標注如何弄全套標 股票配資電銷機器人 武漢AI電銷機器人

做數據庫開發(fā)的過程中難免會遇到有表數據備份的,而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是事先創(chuàng)建好的

例:

--1.創(chuàng)建表
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.創(chuàng)建測試數據
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的表結構自動創(chuàng)建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.創(chuàng)建表
create TABLE Table1
(
    a varchar(10),
    b varchar(10),
    c varchar(10)
) ;

commit;
--2.創(chuàng)建測試數據
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.創(chuàng)建表
create TABLE Table1
(
    a varchar(10),
    b varchar(10),
    c varchar(10)
) ;

commit;
--2.創(chuàng)建測試數據
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;

到此這篇關于SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解的文章就介紹到這了,更多相關SQL Server之SELECT INTO 和 INSERT INTO SELECT內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • sqlserver存儲過程中SELECT 與 SET 對變量賦值的區(qū)別
  • SQLServer中SELECT語句的執(zhí)行順序
  • SQLServer之常用函數總結詳解
  • SqlServer數據庫遠程連接案例教程

標簽:汕頭 武威 廣東 泰安 濟源 安徽 濟寧 臺州

巨人網絡通訊聲明:本文標題《SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解》,本文關鍵詞  SQL,Server,之,SELECT,INTO,和,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解》相關的同類信息!
  • 本頁收集關于SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解的相關信息資訊供網民參考!
  • 推薦文章
    临城县| 方山县| 临夏县| 巴楚县| 平度市| 调兵山市| 水富县| 大洼县| 梁河县| 建始县| 五峰| 天镇县| 宿迁市| 依安县| 漠河县| 大安市| 拉萨市| 习水县| 福泉市| 沂南县| 高青县| 漠河县| 突泉县| 当阳市| 潼关县| 府谷县| 本溪| 井研县| 绥中县| 垫江县| 竹溪县| 西林县| 重庆市| 安福县| 济阳县| 恩施市| 贡嘎县| 鹤岗市| 比如县| 佳木斯市| 万山特区|