亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

MSSQL2005 INSERT,UPDATE,DELETE 之OUTPUT子句使用實例

 更新時間:2009年10月07日 22:12:12   作者:  
MSSQL2005 INSERT,UPDATE,DELETE使用實例,大家可以看下。
復制代碼 代碼如下:

-->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'
--儲存此結果集保存到一個表值變量中
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語句中使用,從插入表或刪除表中得到數(shù)值,并返回這些數(shù)值。
以下語句中不支持 OUTPUT 子句:
l 引用本地分區(qū)視圖、分布式分區(qū)視圖或遠程表的 DML 語句。
l 包含 EXECUTE 語句的 INSERT 語句。
l 不能將 OUTPUT INTO 子句插入視圖或行集函數(shù)。
簡潔的OUTPUT子句,使得向SQL Server導入數(shù)據(jù)的操作得到了極大的簡化。

相關文章

最新評論