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 陳述式來使用 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 子句對于在 INSERT操作之后檢索標(biāo)識(shí)列或計(jì)算列的值可能非常有用。
另外OUTPUT子句也可以在UPDATE和DELETE語句中使用,從插入表或刪除表中得到數(shù)值,并返回這些數(shù)值。
以下語句中不支持 OUTPUT 子句:
l 引用本地分區(qū)視圖、分布式分區(qū)視圖或遠(yuǎn)程表的 DML 語句。
l 包含 EXECUTE 語句的 INSERT 語句。
l 不能將 OUTPUT INTO 子句插入視圖或行集函數(shù)。
簡潔的OUTPUT子句,使得向SQL Server導(dǎo)入數(shù)據(jù)的操作得到了極大的簡化。
相關(guān)文章
sql server 2005用戶權(quán)限設(shè)置深入分析
關(guān)于什么是用戶權(quán)限,最簡單的定義可能是,用戶能做什么和不能做什么,本文將詳細(xì)介紹sql server 2005用戶權(quán)限設(shè)置,需要了解的朋友可以參考下2012-11-11
PowerDesigner中如何導(dǎo)入SQL Server數(shù)據(jù)庫
這篇文章向我們介紹了如何導(dǎo)入SQL Server數(shù)據(jù)庫到PowerDesigner中的詳細(xì)步驟說明,并附帶圖片介紹,需要了解的朋友可以參考下2015-07-07
監(jiān)視SQLServer數(shù)據(jù)庫鏡像[圖文]
SQLServer2005以上版本提供了一個(gè)數(shù)據(jù)庫鏡像監(jiān)視器,不過打開它的方法比較特別。有了圖形界面以后,SQLServer數(shù)據(jù)庫鏡像的維護(hù)難度跟成本大大降低了。2009-05-05
SQLServer 2008中通過DBCC OPENTRAN和會(huì)話查詢事務(wù)
無論是有意無意,如果事務(wù)在數(shù)據(jù)庫中保持打開,則它會(huì)阻塞其他進(jìn)程對修改后的數(shù)據(jù)進(jìn)行操作。同樣,對事務(wù)日志進(jìn)行備份也只會(huì)截?cái)嗖换顒?dòng)事務(wù)的那部分事務(wù)日志,所以打開的事務(wù)會(huì)導(dǎo)致日志變多(甚至達(dá)到物理限制),直到事務(wù)被提交或回滾。2011-05-05
sql 2005不允許進(jìn)行遠(yuǎn)程連接可能會(huì)導(dǎo)致此失敗的解決方法
用vs.net2005連接sql server 2005 出現(xiàn)以下錯(cuò)誤:在建立與服務(wù)器的連接時(shí)出錯(cuò)。在連接到 SQL Server 2005 時(shí),在默認(rèn)的設(shè)置下 SQL Server 不允許進(jìn)行遠(yuǎn)程連接可能會(huì)導(dǎo)致此失敗。2008-04-04
sql server 2005中使用with實(shí)現(xiàn)遞歸的方法
這篇文章介紹了sql server 2005中使用with實(shí)現(xiàn)遞歸的方法,有需要的朋友可以參考一下2013-09-09
SQLServer ntile獲取每組前10%的數(shù)據(jù)
sqlserver2005有關(guān)鍵字ntile(x)和over(partition by.. order by..)子句配合.2009-08-08

