我有一個SQL Server函數從表中獲取下一個ID。這足夠直接,但我也想更新表中所用的ID。我正在使用該技術更新到表格中,但更新沒有發生。代碼如下所示,任何人都知道爲什麼?在SQL Server函數中更新
CREATE FUNCTION [dbo].[GetNextKeyID]
(
@user nvarchar(10),
@keytype nvarchar(20)
)
RETURNS nvarchar(15)
AS
BEGIN
DECLARE @NextID nvarchar(15);
-- get the next id for this keytype
SELECT @NextID = @user + '_' + @keytype + '_' + CONVERT(VARCHAR(3),
(SELECT CASE WHEN @keytype = 'sess' THEN SessionNext
WHEN @keytype = 'event' THEN EventNext
WHEN @keytype = 'evtc' THEN EventConsultantNext
WHEN @keytype = 'act' THEN ActivityNext
END)
)
FROM dbo.[BARKeys]
WHERE [UserID] = @user
-- increment the next id for this keytype
DECLARE @BARKeys TABLE
(
UserID nvarchar(10),
SessionNext int,
EventNext int,
EventConsultantNext int,
ActivityNext int
)
UPDATE @BARKeys
SET SessionNext = CASE WHEN @keytype ='sess' OR @keytype = 'All'
THEN SessionNext + 1
ELSE SessionNext END
,EventNext = CASE WHEN @keytype ='event' OR @keytype = 'All'
THEN EventNext + 1
ELSE EventNext END
,EventConsultantNext = CASE WHEN @keytype ='evtc' OR @keytype = 'All'
THEN EventConsultantNext + 1
ELSE EventConsultantNext END
,ActivityNext = CASE WHEN @keytype ='act' OR @keytype = 'All'
THEN ActivityNext + 1
ELSE ActivityNext END
WHERE [UserID] = @user
-- return the next id
RETURN @NextID ;
END
但是存儲過程不能從其他SP作爲函數調用。我以爲我讀過通過變量更新會發揮作用。它沒有錯誤,它只是不更新。 –
解決方法不起作用。 UPDATE#tbl1 SET X = getx(tbl1.Z),Y = getx(tbl1.W)UPDATE#tbl2 SET X = getx(tbl2.Y)...不會很好地轉換爲存儲過程。 – Joshua