MSSQL竞争条件

时间:2015-02-05 09:27:25

标签: sql sql-server stored-procedures race-condition

此SQL存储过程是否会遇到竞争条件问题?

ALTER PROCEDURE [dbo].[basket_add]
    @item_no BIGINT,
    @child_item_no BIGINT,
    @order_no NVARCHAR(50),
    @qty SMALLINT,
    @curr_code NVARCHAR(3),
    @store_id TINYINT
AS
BEGIN
    SET NOCOUNT ON;
    BEGIN TRY
        DECLARE @price_gbp DECIMAL(18,2), @price_curr DECIMAL(18,2);
        DECLARE @rtn_tbl_basket TABLE (id bigint);
        IF @qty > 0 
        BEGIN
            --Call product price function to get latest product price
            SELECT  @price_gbp  = gbp_price 
                   ,@price_curr = curr_price
            FROM product_price(@item_no,@child_item_no,@curr_code,@store_id) WHERE curr_code = @curr_code;

            --Ensure price exists for specified product
            DECLARE @errormsg NVARCHAR(100) = 'Errors found, product/price does not exist: ' + CAST(@item_no AS NVARCHAR(10)) + '-' + CAST(@child_item_no AS NVARCHAR(10));
            IF ISNULL(@price_gbp,0) = 0 RAISERROR (@errormsg, 16, 2) WITH SETERROR; 

            INSERT INTO basket(order_no, item_no,child_item_no, qty, price, curr_price, curr_code)
            OUTPUT inserted.id INTO @rtn_tbl_basket(id)
            VALUES (@order_no,@item_no,@child_item_no,@qty,@price_gbp, @price_curr, @curr_code)

            --return success
            SELECT id AS rtn_basket_id ,1 AS success FROM @rtn_tbl_basket 
        END
    END TRY
    BEGIN CATCH 
    SELECT 
            --return failure
            (SELECT id FROM @rtn_tbl_basket) as rtn_basket_id, 
            0 AS success, 
            ERROR_NUMBER() AS ErrorNumber,
            ERROR_SEVERITY() AS ErrorSeverity,
            ERROR_STATE() AS ErrorState,
            ERROR_PROCEDURE() AS ErrorProcedure,
            ERROR_LINE() AS ErrorLine,
            ERROR_MESSAGE() AS ErrorMessage; 
    END CATCH
END

0 个答案:

没有答案