我有一个从表中获取 nextID 的 SQL Server 函数。这很简单,但我还想更新所用 ID 的表。我正在使用该技术更新到表中,但更新并没有发生。代码如下所示,有人知道为什么吗?
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