mssql insert into等数据更新操作

本文深入探讨了SQL中插入表记录的多种方法,包括单行插入、多行插入、查询结果插入等,并介绍了如何利用insert exec和insert select进行更灵活的数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值