SQL Server之SELECT INTO 和 INSERT INTO SELECT案例詳解
做數(shù)據(jù)庫開發(fā)的過程中難免會遇到有表數(shù)據(jù)備份的,而SELECT INTO……和INSERT INTO SELECT…… 這兩種語句就是用來進行表數(shù)據(jù)復(fù)制,下面簡單的介紹下:
1、INSERT INTO SELECT
語句格式:Insert Into Table2(column1,column2……) Select value1,value2,value3,value4 From Table1 或 Insert Into Table2 Select * From Table1
說明:這種方式的表復(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)建測試數(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中的語法是一樣的,可以通用.
2、SELECT INTO……
這種方式的語句可以在Table2不存在的時候進行表數(shù)據(jù)復(fù)制,編譯器會根據(jù)Table1的表結(jié)構(gòu)自動創(chuàng)建Table2,Table2和Table1的結(jié)構(gòu)基本上是一致的,但是如果已經(jīng)存在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)建測試數(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)建測試數(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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sql Server導(dǎo)入mdf和ldf文件的教程詳解
有時候我們需要導(dǎo)入mdf和ldf文件進入SQL SERVER中,本文給大家介紹了Sql Server導(dǎo)入mdf和ldf文件的詳細教程,文中介紹了導(dǎo)入的兩種方法,通過圖文結(jié)合的方式介紹的非常詳細,需要的朋友可以參考下2024-06-06SQL?Server查看服務(wù)器角色的實現(xiàn)方法詳解
這篇文章主要為大家介紹了SQL?Server查看服務(wù)器角色的實現(xiàn)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值
一句Sql把縱向表轉(zhuǎn)為橫向表,并分別分組求平均和總平均值,需要的朋友可以參考下。2010-06-06SQL Server雙服務(wù)器架設(shè)并數(shù)據(jù)自動同步教程
自編程序由單機版改為網(wǎng)絡(luò)版后,使用范圍迅速擴大,如何保障數(shù)據(jù)庫萬無一失成為一個重要解決的問題于是想到架設(shè)雙服務(wù)器并數(shù)據(jù)自動同步,詳細步驟如下2012-11-11