存储过程并发失败问题与插入主键值

时间:2015-04-07 10:04:57

标签: sql stored-procedures primary-key

我正在同时执行多个请求的存储过程然后失败并且我遇到错误。

  

“违反PRIMARY KEY约束'PK_catalogs_mstr'。无法在对象'dbo.catalogs_mstr'中插入>重复键。重复键值为>(1)。\ r \ n语句已终止。”

我的表将Catalog_Code列作为主键,不带自动增量。我试图在存储过程中为此列生成值。

CREATE PROCEDURE [dbo].[usp_CreateCatalogDefination]

@AccountId                  INT,
@UserId                     INT,
@CatalogName                NVARCHAR(100),
@DataSourceTypeId           SMALLINT,
@AttributeGroupId           SMALLINT,
@ReferenceCatalogCode       INT,
@ImageName           NVARCHAR (100),
@ImageFile            NVARCHAR (100),
@ImagePath        NVARCHAR (100),

@Result         int OUTPUT

AS

  BEGIN

 SET NOCOUNT ON 

    /* Declaration Block */
    DECLARE @ErrorMessage NVARCHAR(4000)
    DECLARE @ErrorSeverity INT
    DECLARE @ErrorState INT
    DECLARE @ValidReferenceCatalogCode INT


    BEGIN TRY 
    BEGIN TRANSACTION   

            IF EXISTS(SELECT id from accounts_mstr WHERE id=@AccountId AND account_type_id =2)    /*  Account should be of Seller Type   */
            BEGIN
                        IF EXISTS(SELECT id from users_mstr WHERE id=@UserId)    /* Check for Valid User Id   */
                        BEGIN               

                                                /* Generate Catalog Code   */ 
                                                DECLARE @CatalogCode INT

                                                SET @CatalogCode = (SELECT TOP 1 catalog_code FROM catalogs_mstr Order By catalog_code desc)                                                    

                                                IF(@CatalogCode IS NULL)
                                                BEGIN
                                                    SET @CatalogCode = 1
                                                END
                                                ELSE
                                                BEGIN
                                                    SET @CatalogCode = @CatalogCode + 1
                                                END

                                                            IF EXISTS(SELECT id from attribute_groups WHERE id=@AttributeGroupId)    /* Check for attribute group   */
                                                            BEGIN
                                                                        INSERT INTO [dbo].[catalogs_mstr](
                                                                            [catalog_code],
                                                                            [seller_account_id],
                                                                            [user_id],
                                                                            [catalog_name],
                                                                            [datasource_type_id],
                                                                            [attribute_group_id],                                                       
                                                                            [image_name],
                                                                            [image_file],
                                                                            [image_path]
                                                                        ) 

                                                                        VALUES (
                                                                                    @CatalogCode,
                                                                                    @AccountId,
                                                                                    @UserId,
                                                                                    LTRIM(RTRIM(@CatalogName)),
                                                                                    @DataSourceTypeId,
                                                                                    @AttributeGroupId,                                                              
                                                                                    LTRIM(RTRIM(@ImageName)),
                                                                                    LTRIM(RTRIM(@ImageFile)),
                                                                                    LTRIM(RTRIM(@ImagePath))
                                                                                ) 

                                                                        COMMIT TRANSACTION
                                                                        SET @Result=1 /* Success  */
                                                                        RETURN @Result
                                                            END
                                                            ELSE
                                                            BEGIN
                                                                        ROLLBACK TRANSACTION
                                                                        SET @Result=2 /* Invalid Attribute Group Id  */
                                                                        RETURN @Result  
                                                            END

                        END
                        ELSE
                        BEGIN
                                    ROLLBACK TRANSACTION
                                    SET @Result=4 /* Invalid User Id  */
                                    RETURN @Result  
                        END
            END
            ELSE
            BEGIN
                        ROLLBACK TRANSACTION
                        SET @Result=5 /* Invalid Seller Account Id  */
                        RETURN @Result
            END

    END TRY

    BEGIN CATCH 

          --USE CATCH BLOCK FOR RAISING ERROR TO APPLICATION END
        IF @@TRANCOUNT > 0 
                ROLLBACK TRANSACTION

         SELECT @ErrorMessage = 'Error : ' + HOST_NAME() + ' : ' + OBJECT_NAME(@@PROCID) + ' : ' 
                      + ERROR_MESSAGE(),
           @ErrorSeverity = ERROR_SEVERITY(),
           @ErrorState = ERROR_STATE();

        RAISERROR ( 
                    @ErrorMessage,
                    @ErrorSeverity,
                    @ErrorState
                  );

        RETURN

    END CATCH 

SET NOCOUNT OFF

END

0 个答案:

没有答案