解析SQLServer2005的Top功能
更新時(shí)間:2013年06月26日 17:27:19 作者:
本篇文章是對SQLServer2005的Top功能進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
所有人都知道select top 的用法,但很多人還不知道update top 和 delete top 怎么用。以往的做法是set rowcount來指定,其實(shí)SQL2005中對于Top語句的增強(qiáng)除了參數(shù)化之外還包括對update和delete的支持,但可惜的是還不支持自定義的order by列。如果要自定義派序列可以借助CTE.對于CTE的任何更改都會影響到原始表。
我們看下面的測試代碼。
set nocount on
use tempdb
go
if (object_id ('tb' ) is not null )
drop table tb
go
create table tb (id int identity (1 , 1 ), name varchar (10 ), tag int default 0 )
insert into tb (name ) select 'a'
insert into tb (name ) select 'b'
insert into tb (name ) select 'c'
insert into tb (name ) select 'd'
insert into tb (name ) select 'e'
/*--更新前兩行
id name tag
----------- ---------- -----------
1 a 1
2 b 1
3 c 0
4 d 0
5 e 0
*/
update top (2 ) tb set tag = 1
select * from tb
/*--更新后兩行
id name tag
----------- ---------- -----------
1 a 1
2 b 1
3 c 0
4 d 1
5 e 1
*/
;with t as
(
select top (2 ) * from tb order by id desc
)
update t set tag = 1
select * from tb
/*--刪除前兩行
id name tag
----------- ---------- -----------
3 c 0
4 d 1
5 e 1
*/
delete top (2 ) from tb
select * from tb
/*--刪除后兩行
id name tag
----------- ---------- -----------
3 c 0
*/
;with t as
(
select top (2 ) * from tb order by id desc
)
delete from t
select * from tb
set nocount off
我會在下一篇文章中介紹一個(gè)應(yīng)用,就是很多人關(guān)心的如何獨(dú)占查詢(就是一條數(shù)據(jù)只被一個(gè)終端select到)。
如果你感興趣的話可以自己先思考一下,我給一個(gè)提示:
SQLServer2005有一個(gè)關(guān)鍵字Output,它可以將更改和插入的數(shù)據(jù)輸出,我們配合update top就可以模擬出來一個(gè)相對高效的獨(dú)占查詢的事物。此功能適合用在并行的任務(wù)處理或者消費(fèi)中。
我們看下面的測試代碼。
復(fù)制代碼 代碼如下:
set nocount on
use tempdb
go
if (object_id ('tb' ) is not null )
drop table tb
go
create table tb (id int identity (1 , 1 ), name varchar (10 ), tag int default 0 )
insert into tb (name ) select 'a'
insert into tb (name ) select 'b'
insert into tb (name ) select 'c'
insert into tb (name ) select 'd'
insert into tb (name ) select 'e'
/*--更新前兩行
id name tag
----------- ---------- -----------
1 a 1
2 b 1
3 c 0
4 d 0
5 e 0
*/
update top (2 ) tb set tag = 1
select * from tb
/*--更新后兩行
id name tag
----------- ---------- -----------
1 a 1
2 b 1
3 c 0
4 d 1
5 e 1
*/
;with t as
(
select top (2 ) * from tb order by id desc
)
update t set tag = 1
select * from tb
/*--刪除前兩行
id name tag
----------- ---------- -----------
3 c 0
4 d 1
5 e 1
*/
delete top (2 ) from tb
select * from tb
/*--刪除后兩行
id name tag
----------- ---------- -----------
3 c 0
*/
;with t as
(
select top (2 ) * from tb order by id desc
)
delete from t
select * from tb
set nocount off
我會在下一篇文章中介紹一個(gè)應(yīng)用,就是很多人關(guān)心的如何獨(dú)占查詢(就是一條數(shù)據(jù)只被一個(gè)終端select到)。
如果你感興趣的話可以自己先思考一下,我給一個(gè)提示:
SQLServer2005有一個(gè)關(guān)鍵字Output,它可以將更改和插入的數(shù)據(jù)輸出,我們配合update top就可以模擬出來一個(gè)相對高效的獨(dú)占查詢的事物。此功能適合用在并行的任務(wù)處理或者消費(fèi)中。
相關(guān)文章
SQL Server中使用sp_password重置SA密碼實(shí)例
這篇文章主要介紹了SQL Server中使用sp_password重置SA密碼實(shí)例,一般在忘記SA密碼時(shí)的恢復(fù)手段,需要的朋友可以參考下2014-08-08SQLSERVER數(shù)據(jù)備份文件的分割備份方法
這篇文章主要介紹了SQLSERVER數(shù)據(jù)備份文件的分割備份方法 ,需要的朋友可以參考下2014-12-12SQL?Server2012附加數(shù)據(jù)庫5120錯(cuò)誤(拒絕訪問)的解決方法
在附加數(shù)據(jù)庫的時(shí)候會遇到錯(cuò)誤代碼為5120等的錯(cuò)誤,提示為拒絕訪問,下面這篇文章主要給大家介紹了關(guān)于SQL?Server2012附加數(shù)據(jù)庫5120錯(cuò)誤(拒絕訪問)的解決方法,需要的朋友可以參考下2023-05-05sql 查詢記錄數(shù)結(jié)果集某個(gè)區(qū)間內(nèi)記錄
sqlserver如何實(shí)現(xiàn)查詢記錄數(shù)某個(gè)區(qū)間內(nèi)記錄,本文將提供多種解決方法,需要了解的朋友可以參考下2012-11-11SQL Server把某個(gè)字段的數(shù)據(jù)用一條語句轉(zhuǎn)換成字符串
這篇文章主要介紹了SQL Server把某個(gè)字段的數(shù)據(jù)用一條語句轉(zhuǎn)換成字符串,本文講解使用一條語句把字段中的數(shù)據(jù)查詢出來并以指定分割符組成字符串,需要的朋友可以參考下2015-07-07SQLSERVER中得到執(zhí)行計(jì)劃的兩種方式
得到執(zhí)行計(jì)劃的方式有兩種:一種是在指令的前面打開一些開關(guān),讓執(zhí)行計(jì)劃信息打在結(jié)果集里,這種方法比較適合在一個(gè)測試環(huán)境里對單個(gè)語句調(diào)優(yōu);另一種方法是使用SQL Trace里的事件跟蹤來跟蹤語句的執(zhí)行計(jì)劃,感興趣的朋友可以了解下2013-01-01