gpt4 book ai didi

sql - scope_identity 返回 null

转载 作者:行者123 更新时间:2023-12-02 07:38:59 29 4
gpt4 key购买 nike

我正在用 SQL server 写一个 SP

ALTER PROCEDURE [dbo].[spAddUserBadge]
AS
BEGIN
INSERT INTO dbo.SE_UserBadge
(
[UID],
BadgeID,
BadgeAssignmentOrRenewalDate,
BadgeExpiryDate ,
AmountPaid,
Remarks ,
AssignedBy
)
VALUES
(
17,1, 12-12-2012, 12-12-2013, 0.00, '', ''
)

SELECT SCOPE_IDENTITY() AS id
END
GO

SCOPE_IDENTITY 为 NULL。我哪里错了?

最佳答案

使用 OUTPUT运算符将插入的值输出到表变量中,然后检查这些值。

示例

ALTER PROCEDURE [dbo].[spAddUserBadge]
AS
BEGIN
DECLARE @TBL AS TABLE(ID1 INT, ID2 INT)

INSERT INTO dbo.SE_UserBadge
(
[UID],
BadgeID,
BadgeAssignmentOrRenewalDate,
BadgeExpiryDate ,
AmountPaid,
Remarks ,
AssignedBy
)
OUTPUT inserted.ID1, inserted.ID2 INTO @TBL
VALUES
(
17,1, 12-12-2012, 12-12-2013, 0.00, '', ''
)

SELECT * FROM @TBL
END
GO

关于sql - scope_identity 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13237015/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com