MSSQL2005 INSERT ,UPDATE,DELETE 之OUTPUT子句
2010-07-13 13:42
330 查看
-->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
*/
--刪除剛才插入的紀錄
delete tb where [name]>'c'
--储存此结 ªG集保存到一个 ªí值变 ¶q中
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 個資料列受到影響)
*/
--DELETE 陳述式使用OUTPUT
delete tb output deleted.* where id=9
/*
ID name
----------- ----------
9 e
(1 個資料列受到影響)
*/
-- UPDATE 陳述式使用OUTPUT INTO
update tb set [name]='test' output inserted.* where id=10
/*
ID name
----------- ----------
10 test
(1 個資料列受到影響)
*/
/*
OUTPUT 子句对于在 INSERT操作之后检索标识列或计算列的值可能非常有用。
另外OUTPUT子句也可以在UPDATE和DELETE语句中使用,从插入表或删除表中得到数值,并返回这些数值。
以下语句中不支持 OUTPUT 子句:
l 引用本地分区视图、分布式分区视图或远程表的 DML 语句。
l 包含 EXECUTE 语句的 INSERT 语句。
l 不能将 OUTPUT INTO 子句插入视图或行集函数。
简洁的OUTPUT子句,使得向SQL Server导入数据的操作得到了极大的简化。
*/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2009/10/07/4639733.aspx
-->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
*/
--刪除剛才插入的紀錄
delete tb where [name]>'c'
--储存此结 ªG集保存到一个 ªí值变 ¶q中
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 個資料列受到影響)
*/
--DELETE 陳述式使用OUTPUT
delete tb output deleted.* where id=9
/*
ID name
----------- ----------
9 e
(1 個資料列受到影響)
*/
-- UPDATE 陳述式使用OUTPUT INTO
update tb set [name]='test' output inserted.* where id=10
/*
ID name
----------- ----------
10 test
(1 個資料列受到影響)
*/
/*
OUTPUT 子句对于在 INSERT操作之后检索标识列或计算列的值可能非常有用。
另外OUTPUT子句也可以在UPDATE和DELETE语句中使用,从插入表或删除表中得到数值,并返回这些数值。
以下语句中不支持 OUTPUT 子句:
l 引用本地分区视图、分布式分区视图或远程表的 DML 语句。
l 包含 EXECUTE 语句的 INSERT 语句。
l 不能将 OUTPUT INTO 子句插入视图或行集函数。
简洁的OUTPUT子句,使得向SQL Server导入数据的操作得到了极大的简化。
*/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wufeng4552/archive/2009/10/07/4639733.aspx
相关文章推荐
- MSSQL2005 INSERT,UPDATE,DELETE 之OUTPUT子句使用实例
- MSSQL2005 INSERT ,UPDATE,DELETE 之OUTPUT子句
- 带有OUTPUT的INSERT,DELETE,UPDATE
- ORA-06547: INSERT, UPDATE 或 DELETE 语句必须使用 RETURNING 子句
- sql server 带有OUTPUT的INSERT,DELETE,UPDATE
- 带有OUTPUT的INSERT,DELETE,UPDATE使用方式记录
- 带有OUTPUT的INSERT,DELETE,UPDATE
- SQL Server--OUTPUT及OUTPUT...INTO... 在 INSERT、UPDATE 或 DELETE的用法
- 带有OUTPUT的INSERT,DELETE,UPDATE
- 带有OUTPUT的INSERT,DELETE,UPDATE
- SQL Server--OUTPUT及OUTPUT...INTO... 在 INSERT、UPDATE 或 DELETE的用法
- [学习SQL SERVER 2005系列]关于INSERT、UPDATE 或 DELETE的OUTPUT及OUTPUT...INTO...用法
- OUTPUT 作用在INSERT,UPDATE,DELETE后
- 带有OUTPUT的INSERT,DELETE,UPDATE
- Hql 也可以进行DML操作update delete insert
- SQL语句复习:insert,update,delete,select
- SELECT和一次的INSERT/DELETE/UPDATE需要用事物管理?
- 触发器 Insert Delete Update
- insert,update和delete造成阻塞的示例
- sql serveri不用游标,适合于循环DML (update delete insert)的场合实例