Sql Server 插入數據


--插入數據

 

/*重點

--將現有表ABC導出到一個新表ttb中。 --如果不是導出全部則可以不使用* 而使用明確列(可以導出部分列)。
--可使用where 和groupby 等子句。
--可聯結多個表插入數據。
--不管從多少個表中檢索數據,數據都只能插入到一個表中。
--語法: SELECT * INTO 目的表 FROM 源表
--如果需要進行試驗新SQL語句,可以使用SELECT INTO 在復制出來的表上進行測試。
select * into ttb from abc;
select * from ttb;


-- 使用插入數據時 可以配合select 一起使用。
insert into abc(id,name,aid) select id,name,cid from cba where id=9; --將CBA表中的數據插入到ABC表中, 必須保證CBA中主鍵或者不允許重復的列和ABC沒有沖突,否則則會執行失敗。


insert into abc(id,name,aid) select id,name,cid from cba;--將CBA表中的數據插入到ABC表中, 必須保證CBA中主鍵或者不允許重復的列和ABC沒有沖突,否則則會執行失敗。

--

*/

insert into Customers (cust_id,cust_contact,cust_email,cust_address,cust_city,cust_state,cust_zip,cust_country) select cust_id,cust_contact,cust_email,cust_address,cust_city,cust_state,cust_zip,cust_country from custNew;

 


create table cba (id int,name varchar(20),cid int)


insert into cba values(9,'999',91)

insert into cba values(8,'999',81)

insert into cba values(8,'999',81)


select * from abc

 


insert into abc(id,name) select id,name from cba;


insert into abc(id,aid) select cid,id from cba where cid=91;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM