濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解

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

熱門標(biāo)簽:在電子版地圖標(biāo)注要收費(fèi)嗎 實(shí)體店地圖標(biāo)注怎么標(biāo) 外呼系統(tǒng)會(huì)封嗎 電銷機(jī)器人 深圳 南京電銷外呼系統(tǒng)哪家好 萬(wàn)利達(dá)綜合醫(yī)院地圖標(biāo)注點(diǎn) 地圖標(biāo)注如何弄全套標(biāo) 股票配資電銷機(jī)器人 武漢AI電銷機(jī)器人

做數(shù)據(jù)庫(kù)開發(fā)的過(guò)程中難免會(huì)遇到有表數(shù)據(jù)備份的,而SELECT INTO……和INSERT INTO SELECT…… 這兩種語(yǔ)句就是用來(lái)進(jìn)行表數(shù)據(jù)復(fù)制,下面簡(jiǎn)單的介紹下:

1、INSERT INTO SELECT

語(yǔ)句格式:Insert Into Table2(column1,column2……) Select value1,value2,value3,value4 From Table1 或 Insert Into Table2 Select * From Table1

說(shuō)明:這種方式的表復(fù)制必須要求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)建測(cè)試數(shù)據(jù)
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.復(fù)制table1數(shù)據(jù)到table2中
Insert into Table2(a, c, d) select a,b,c from Table1;
commit;
--或,此種方式必須要求table2和table1的列數(shù)相等,而且類型兼容
Insert into Table2 select * from table1;
commit;

以上這些sql在oracle和MS SqlServer中的語(yǔ)法是一樣的,可以通用.

2、SELECT INTO……

這種方式的語(yǔ)句可以在Table2不存在的時(shí)候進(jìn)行表數(shù)據(jù)復(fù)制,編譯器會(huì)根據(jù)Table1的表結(jié)構(gòu)自動(dòng)創(chuàng)建Table2,Table2和Table1的結(jié)構(gòu)基本上是一致的,但是如果已經(jīng)存在Table2,則編譯器會(huì)報(bào)錯(cuò).

這種方式的語(yǔ)句在Oracle中和MS SqlServer中是有點(diǎn)差別的,,如下:

語(yǔ)句格式:

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)建測(cè)試數(shù)據(jù)
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.復(fù)制table1數(shù)據(jù)到table2中
Create Table Table2 as select a,b,c From table1;
Commit;
--或(這兩種方式的sql只能應(yīng)用一次)
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)建測(cè)試數(shù)據(jù)
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.復(fù)制table1數(shù)據(jù)到table2中
Select a,b,c into Table2 From table1;
Commit;
--或(這兩種方式的sql只能應(yīng)用一次)
Select * into table2 From Table1;
Commit;
--刪除表
drop table table1;
drop table table2;
commit;

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

您可能感興趣的文章:
  • sqlserver存儲(chǔ)過(guò)程中SELECT 與 SET 對(duì)變量賦值的區(qū)別
  • SQLServer中SELECT語(yǔ)句的執(zhí)行順序
  • SQLServer之常用函數(shù)總結(jié)詳解
  • SqlServer數(shù)據(jù)庫(kù)遠(yuǎn)程連接案例教程

標(biāo)簽:汕頭 武威 廣東 泰安 濟(jì)源 安徽 濟(jì)寧 臺(tái)州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解》,本文關(guān)鍵詞  SQL,Server,之,SELECT,INTO,和,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    宜州市| 陇南市| 方山县| 乐至县| 九寨沟县| 潼南县| 和平县| 鹤山市| 永泰县| 布尔津县| 阜新| 光泽县| 大化| 汉源县| 东台市| 临漳县| 雷山县| 三河市| 老河口市| 游戏| 东港市| 黎平县| 石渠县| 瓮安县| 聂拉木县| 柯坪县| 井陉县| 林口县| 虹口区| 石狮市| 涞源县| 乐东| 台山市| 兴安盟| 青铜峡市| 县级市| 云梦县| 双鸭山市| 绥宁县| 固原市| 濉溪县|