本文介紹了在兩行數(shù)據(jù)之間交換值的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
以下 T-SQL 代碼段有效,但我想知道是否有更聰明和/或更簡(jiǎn)潔的方法來(lái)完成兩個(gè)不同行之間的字段值交換.(為簡(jiǎn)單起見(jiàn),代碼具有硬編碼的 pkey 值.)
The following T-SQL code segment works but i'm wondering if there's a cleverer and/or less verbose way to accomplish the swapping of field values between two different rows. (The code has hard-coded pkey values for simplicity.)
BEGIN TRAN;
declare @swapFormSeqA int;
declare @swapFormSeqB int;
SELECT @swapFormSeqA = DisplaySeq
FROM CustomCatalogForm
WHERE CustomCatalogFormId = 1;
SELECT @swapFormSeqB = DisplaySeq
FROM CustomCatalogForm
WHERE CustomCatalogFormId = 2;
UPDATE CustomCatalogForm
SET DisplaySeq = @swapFormSeqB
WHERE CustomCatalogFormId = 1;
UPDATE CustomCatalogForm
SET DisplaySeq = @swapFormSeqA
WHERE CustomCatalogFormId = 2;
COMMIT TRAN;
我專(zhuān)門(mén)使用 Sql2k5,所以 2k8 不是一個(gè)選項(xiàng).
i'm using Sql2k5 specifically so 2k8 is not an option.
推薦答案
你可以這樣做:
begin tran
declare @sum int
select @sum = sum(DisplaySeq)
from CustomCatalogForm
where CustomCatalogFormId in (1,2)
update CustomCatalogForm
set DisplaySeq = @sum - DisplaySeq
where CustomCatalogFormId in (1,2)
commit tran
這篇關(guān)于在兩行數(shù)據(jù)之間交換值的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!