我有一個表有兩列「名稱」和「ID」。其中ID不爲空。Sql Server 2005交換ID號
我正在嘗試創建一個存儲過程,以便在兩個名稱之間交換ID時,此處的估算值就是我目前爲止的值。
CREATE PROCEDURE dbo.procName
@OldName NVARCHAR(128),
@NewName NVARCHAR(128)
AS
DECLARE @NewId0 INT,
@NewId1 INT,
@OldId0 INT,
@OldId1 INT,
@Number INT
SELECT @NewId0 = ID FROM Table1 WHERE [Name] = @NewName
SELECT @NewId1 = ID FROM Table1 WHERE [Name] = @NewName
SELECT @OldId0 = ID FROM Table1 WHERE [Name] = @OldName
SELECT @OldId1 = ID FROM Table1 WHERE [Name] = @OldName
SELECT @Number = 0
UPDATE Table1 SET ID = @Number WHERE ID = @NewId0
UPDATE Table1 SET ID = @NewId1 WHERE ID = @OldId0
UPDATE Table1 SET ID = @OldID1 WHERE ID = @NewID0
Go
我得到的是有值爲0
我覺得我的邏輯是正確的,但它似乎並不奏效的第一個名字在那裏,我失去了一些東西?
如果你的表只有兩列,爲什麼不只是交換名稱列? – Arvo 2010-12-09 14:48:00