mssql insert into等数据更新操作

if OBJECT_ID('test','U') is not null drop table test
create table test
(
 tid int identity(1,1) primary key,
 tname varchar(32)
)
go

--单行插入
insert into test(tname) values('chensirbbk');

 

--多行插入
--方法1:
insert into test(tname)
select 'name1'
union all select 'name2'
union all select 'name3';


--方法2:
insert into test(tname)
values('name4'),
('name5'),
('name6');


还向大家介绍几种sql插入表记录的技巧
1.插入记录并查询结果
select * from (values
('name7'),
('name8'),
('name9')
) as T(tname);

 

2.insert exec将执行存储过程的查询结果放到插入语句当中
insert into test(tname)
exec proc_testsel @tname='XXXX';--存储过程略[可以不是当前表]

 

3.insert select将查询的结果插入已经存在的表中
if OBJECT_ID('test1','U') is not null drop table test1
create table test1
(
 tid int,
 tname varchar(32)
)
go

insert into test1(tid,tname) select tid,tname from test;

 

4.select into 特殊2不存在,会复制表的基本结构列名,数据类型,使用允许为空,identity属性和数据
不会复制2样东西:约束,索引和触发器
select * into test2 from test1

 

 


版权声明:本文为chensirbbk原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。