MSSQL2005 INSERT,UPDATE,DELETE 之OUTPUT子句使用实例
数据库  /  管理员 发布于 4年前   97
复制代码 代码如下:
-->Title:Generating test data
-->Author:wufeng4552
-->Date :2009-10-07 15:16:26
if object_id('ta')is not null drop table ta
go
create table ta(ID int identity,[name] varchar(10))
insert ta([name]) select 'a' union all
select 'b' union all
select 'c' union all
select 'd' union all
select 'e' union all
select 'f' union all
select 'g'
if object_id('tb')is not null drop table tb
go
create table tb(ID int identity,[name] varchar(10))
insert tb([name]) select 'a' union all
select 'b' union all
select 'c'
--INSERT 述式硎褂 OUTPUT INTO
insert tb output
inserted.id,
inserted.[name]
select [name]
from ta where not exists(select 1 from tb where [name]=ta.[name])
/*
id name
----------- ----------
4 d
5 e
6 f
7 g
*/
--h除才插入的o
delete tb where [name]>'c'
--储存此结果集保存到一个表值变量中
declare @t table(ID int,[name] varchar(10))
insert tb output
inserted.id,
inserted.[name]into @t
select [name] from ta where not exists(select 1 from tb where [name]=ta.[name])
select * from @t
/*
ID name
----------- ----------
8 d
9 e
10 f
11 g
(4 Y料列受到影)
*/
--DELETE 述式使用 OUTPUT
delete tb output deleted.* where id=9
/*
ID name
----------- ----------
9 e
(1 Y料列受到影)
*/
-- UPDATE 述式使用 OUTPUT INTO
update tb set [name]='test' output inserted.* where id=10
/*
ID name
----------- ----------
10 test
(1 Y料列受到影)
*/
/*
OUTPUT 子句对于在 INSERT操作之后检索标识列或计算列的值可能非常有用。
另外OUTPUT子句也可以在UPDATE和DELETE语句中使用,从插入表或删除表中得到数值,并返回这些数值。
以下语句中不支持 OUTPUT 子句:
l 引用本地分区视图、分布式分区视图或远程表的 DML 语句。
l 包含 EXECUTE 语句的 INSERT 语句。
l 不能将 OUTPUT INTO 子句插入视图或行集函数。
简洁的OUTPUT子句,使得向SQL Server导入数据的操作得到了极大的简化。
原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..博主 在
科学上网翻墙之v2rayN-Core客户端免费公益节点使用教程中评论 @ mashrdn 多切换几个节点测试,免费ssr是没那么稳..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号