MSSQL2005 INSERT,UPDATE,DELETE 之OUTPUT子句使用實(shí)例
更新時(shí)間:2009年10月07日 22:12:12 作者:
MSSQL2005 INSERT,UPDATE,DELETE使用實(shí)例,大家可以看下。
復(fù)制代碼 代碼如下:
-->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 陳述式來(lái)使用 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
*/
--刪除剛才插入的紀(jì)錄
delete tb where [name]>'c'
--儲(chǔ)存此結(jié)果集保存到一個(gè)表值變量中
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 個(gè)資料列受到影響)
*/
--DELETE 陳述式使用 OUTPUT
delete tb output deleted.* where id=9
/*
ID name
----------- ----------
9 e
(1 個(gè)資料列受到影響)
*/
-- UPDATE 陳述式使用 OUTPUT INTO
update tb set [name]='test' output inserted.* where id=10
/*
ID name
----------- ----------
10 test
(1 個(gè)資料列受到影響)
*/
/*
OUTPUT 子句對(duì)于在 INSERT操作之后檢索標(biāo)識(shí)列或計(jì)算列的值可能非常有用。
另外OUTPUT子句也可以在UPDATE和DELETE語(yǔ)句中使用,從插入表或刪除表中得到數(shù)值,并返回這些數(shù)值。
以下語(yǔ)句中不支持 OUTPUT 子句:
l 引用本地分區(qū)視圖、分布式分區(qū)視圖或遠(yuǎn)程表的 DML 語(yǔ)句。
l 包含 EXECUTE 語(yǔ)句的 INSERT 語(yǔ)句。
l 不能將 OUTPUT INTO 子句插入視圖或行集函數(shù)。
簡(jiǎn)潔的OUTPUT子句,使得向SQL Server導(dǎo)入數(shù)據(jù)的操作得到了極大的簡(jiǎn)化。
相關(guān)文章
MSSQL 大量數(shù)據(jù)時(shí),建立索引或添加字段后保存更改提示超時(shí)的解決方法
一般我們都喜歡用數(shù)據(jù)庫(kù)管理器的UI來(lái)對(duì)數(shù)據(jù)表結(jié)構(gòu)進(jìn)行更改,然后自然而然地點(diǎn)"保存"按鈕進(jìn)行保存,但數(shù)據(jù)量比較大的時(shí)候,用這招往往會(huì)出現(xiàn)"無(wú)法創(chuàng)建索引“IX_索引名”。 超時(shí)時(shí)間已到。在操作完成之前超時(shí)時(shí)間已過(guò)或服務(wù)器未響應(yīng)。2011-08-08在SQL Server 2005所有表中搜索某個(gè)指定列的方法
這篇文章主要介紹了在SQL Server 2005所有表中搜索某個(gè)指定列的方法,需要的朋友可以參考下2016-11-11sqlserver 中charindex/patindex/like 的比較
sqlserver 中charindex/patindex/like 的比較,大家可以看下根據(jù)需要自行選擇。2009-09-09SqlServer2005 數(shù)據(jù)庫(kù)同步配置圖文詳解
SqlServer2005 數(shù)據(jù)庫(kù)同步配置圖文詳解,使用sqlserver2005的朋友可以參考下2012-06-06SQL Server 2005與sql 2000之間的數(shù)據(jù)轉(zhuǎn)換方法
這篇文章主要介紹了SQL Server 2005與sql 2000之間的數(shù)據(jù)轉(zhuǎn)換方法,需要的朋友可以參考下2014-08-08SQL2005學(xué)習(xí)筆記 APPLY 運(yùn)算符
APPLY 運(yùn)算符簡(jiǎn)介: APPLY 運(yùn)算符是Sql Server2005新增加的運(yùn)算符。2009-07-07