我正在尝试编写一个只向表中插入唯一值的存储过程。让我展示示例代码:
ALTER PROCEDURE [dbo].[InsertSp] (
@memberId INT
) AS
BEGIN
INSERT INTO [dbo].[Members] (memberId)
SELECT @memberId
WHERE NOT EXISTS (
SELECT memberId
FROM [dbo].[Members] WITH (UPDLOCK)
WHERE memberId = @memberId)
END
但是,如果有许多调用试图同时插入相同的值,会发生什么。我是否需要交易,或者不是EXISTS检查对我来说足够了吗?