2 SQL中相同列的变量

时间:2014-01-17 06:56:50

标签: sql sql-server tsql

我正在努力理解以下代码部分,因为它与整个脚本有关。有人可以用简单的英语向我解释以下部分代码吗?为什么看起来像是'Seller_No'列的2个声明变量?

SET @strSellerNo2 = NULL

SELECT  @strSellerNo2 = Seller_No
FROM    Outlet.tblProductMaster
WHERE   Product_No = @strProductNo
        AND Seller_No <> @strSellerNo1
        AND Product_Status = 'Available'

USE [OutletRetail]
GO
/****** Object:  StoredProcedure [Outlet].[sp_UpdateProductStatus]    Script Date: 01/16/2014 19:58:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

--Updates Product status codes to Available if NULL
ALTER   PROCEDURE [Outlet].[sp_UpdateProductStatus]
AS

DECLARE @strProductNo varchar(20)
DECLARE @strSellerNo1 varchar(10)
DECLARE @strSellerNo2 varchar(10)


DECLARE UpdateProductCursor CURSOR FOR

    SELECT Product_No, Seller_No
    FROM Outlet.tblProductMaster
    WHERE Product_Status IS NULL

OPEN UpdateProductCursor
FETCH NEXT FROM UpdateProductCursor INTO @strProduct_No, @strSellerNo1

WHILE @@FETCH_STATUS = 0

BEGIN

    SET @strSellerNo2 = NULL

    SELECT  @strSellerNo2 = Seller_No
    FROM    Outlet.tblProductMaster
    WHERE   Product_No = @strProductNo
        AND Seller_No <> @strSellerNo1
        AND Product_Status = 'Available'


IF (@strSellerNo2 IS NULL)
BEGIN
        UPDATE Outlet.tblProductMaster
        SET Product_Status = 'Available'
        WHERE Product_No = @strProductNo
            AND Seller_No = @strSellerNo1
    END


UPDATE  Outlet.tblProductMaster
SET Product_Status = 'Not Available'
WHERE   Product_No = @strProductNo
        AND Seller_No <> @strSellerNo2
        AND Product_Status IS NULL

    FETCH NEXT FROM UpdateProductCursor INTO @strProductNo, @strSellerNo1
END

CLOSE UpdateProductCursor
DEALLOCATE UpdateProductCursor

1 个答案:

答案 0 :(得分:2)

希望这会有所帮助;

SELECT  @strSellerNo2 = Seller_No       --  Find me the Seller Number
FROM    Outlet.tblProductMaster         --  In the tblProductMaster table
WHERE   Product_No = @strProductNo      --  where the product number is the product we're looking for
    AND Seller_No <> @strSellerNo1      --  and the Seller Number is not the Seller Number previously found
    AND Product_Status = 'Available'    --  and the Product Status is "Available"