国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

MSSQL2005 INSERT ,UPDATE,DELETE 之OUTPUT子句

2024-07-21 02:44:46
字體:
來源:轉載
供稿:網友

-->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語句中使用,從插入表或刪除表中得到數值,并返回這些數值。
以下語句中不支持 OUTPUT 子句:
l 引用本地分區視圖、分布式分區視圖或遠程表的 DML 語句。
l 包含 EXECUTE 語句的 INSERT 語句。
l 不能將 OUTPUT INTO 子句插入視圖或行集函數。
簡潔的OUTPUT子句,使得向SQL Server導入數據的操作得到了極大的簡化。
*/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 屏南县| 长岭县| 错那县| 巫溪县| 宣威市| 惠来县| 石河子市| 彰化市| 绥棱县| 会昌县| 大洼县| 揭阳市| 呼和浩特市| 上林县| 绥化市| 民县| 华蓥市| 平和县| 务川| 南木林县| 海晏县| 宽甸| 崇礼县| 郧西县| 会理县| 嘉义市| 秦皇岛市| 浙江省| 额尔古纳市| 雷山县| 巴东县| 简阳市| 明溪县| 丹阳市| 苗栗市| 永福县| 庆城县| 黔江区| 时尚| 临汾市| 凌源市|