国产探花免费观看_亚洲丰满少妇自慰呻吟_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導入數據的操作得到了極大的簡化。
*/


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 翼城县| 宝坻区| 汉川市| 利辛县| 曲周县| 龙川县| 崇义县| 甘洛县| 苏尼特右旗| 罗田县| 额济纳旗| 嘉鱼县| 滦南县| 龙泉市| 上犹县| 丹棱县| 多伦县| 阿拉善盟| 牡丹江市| 彩票| 庆城县| 司法| 渝中区| 孟津县| 鄂托克旗| 名山县| 宁蒗| 蛟河市| 林西县| 侯马市| 镇江市| 朝阳市| 柳州市| 临清市| 浦北县| 珠海市| 和林格尔县| 濮阳市| 霍州市| 西丰县| 泗阳县|