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