又一个主要的例外

时间:2012-02-22 11:25:40

标签: sql exception primary-key

去年我问这个问题:What could be causing the primary key exception?

但是我在这个存储过程中还有另一个错误:随机我得

  

使用FORETIGN对象“FK_Sessions”INSERT EXCEPTION   “会话”,列“id”。

CREATE PROCEDURE [dbo].[MyProcedure]
        @sessionId varchar(512),
        @variable varchar(350),
        @value image
AS
BEGIN   
        BEGIN TRAN
                DECLARE @result int = 0;
                DECLARE @locked bit;

                IF (SELECT COUNT(*) FROM Sessions WITH(ROWLOCK,HOLDLOCK) WHERE id = @sessionId) = 0
                BEGIN
                        SET @result = -1;
                END
                ELSE BEGIN
                        DELETE Variables WITH(ROWLOCK,HOLDLOCK) WHERE sessionId = @sessionId AND variable = @variable
                        IF @value IS NOT NULL
                        BEGIN
                                INSERT Variables VALUES(@sessionId, @variable, @value, 0)
                        END                                                  
                END    
    COMMIT TRAN
    RETURN @result
END

有什么想法吗?再次感谢

0 个答案:

没有答案