使用CTE更新记录?

时间:2011-08-08 07:29:02

标签: sql sql-server stored-procedures common-table-expression

ALTER PROCEDURE SP_PriceUpdate
-- Add the parameters for the stored procedure here
@PriceRuleID INT = NULL,
@CategoryID INT = NULL,
@SiteID INT = NULL
AS
SET NOCOUNT ON;
BEGIN
WITH PriceCTE(ProductID, CategoryID, SalePrice)
AS
(
    SELECT 
        ProductID, CategoryID, SalePrice
    FROM
        CAT_Products
    WHERE
        CategoryID = @CategoryID
)
SELECT ProductID,categoryID,SalePrice FROM PriceCTE

DECLARE 
    @Value DECIMAL(32,2),
    @Type NVARCHAR(5),
    @Inc_Dec NVARCHAR(5)

SELECT @Value = value FROM VB_PriceRule WHERE ID = @PriceRuleID AND SiteId = @SiteID
SELECT @Type = [type] FROM VB_PriceRule WHERE ID = @PriceRuleID AND SiteId = @SiteID
SELECT @Inc_Dec = Inc_Dec FROM VB_PriceRule WHERE ID = @PriceRuleID AND SiteId = @SiteID

IF(@Type = '+')
    BEGIN
        IF(@Inc_Dec = '%')
            BEGIN
            --print 'MSG'
                UPDATE CAT_Products SET
                    SalePrice = SalePrice + (@Value/100*SalePrice)
                FROM PriceCTE
                WHERE
                    CategoryID = @CategoryID
            END
        ELSE
            BEGIN
                UPDATE CAT_Products SET
                    SalePrice = SalePrice + @Value                      
                FROM PriceCTE
                WHERE
                    CategoryID = @CategoryID
            END
    END
ELSE
    BEGIN
        IF(@Inc_Dec = '%')
            BEGIN
                UPDATE CAT_Products SET
                    SalePrice = SalePrice - (@Value/100*SalePrice)                      
                FROM PriceCTE
                WHERE
                    CategoryID = @CategoryID
            END
        ELSE
            BEGIN
                UPDATE CAT_Products SET
                    SalePrice = SalePrice - @Value
                FROM PriceCTE
                WHERE
                    CategoryID = @CategoryID
            END
    END
END

问)这是我的查询...我想更新CAT_Products的销售价格。根据使用CTE的条件。但它给我的结果是“无效的对象名称PriceCTE”

1 个答案:

答案 0 :(得分:4)

CTE在其定义的单个语句的范围内有效。因此,由于PriceCTE是在SELECT … FROM PriceCTE语句的范围内定义的,因此无法在UPDATE中访问它。 / p>

以下解决方案将CTE定义与UPDATE相结合。它还将您的所有更新合并到一个声明中。

ALTER PROCEDURE SP_PriceUpdate
-- Add the parameters for the stored procedure here
@PriceRuleID INT = NULL,
@CategoryID INT = NULL,
@SiteID INT = NULL
AS
SET NOCOUNT ON;
BEGIN

DECLARE 
    @Value DECIMAL(32,2),
    @Type NVARCHAR(5),
    @Inc_Dec NVARCHAR(5);

SELECT
    @Value = value,
    @Type = [type],
    @Inc_Dec = Inc_Dec
FROM
    VB_PriceRule
WHERE
    ID = @PriceRuleID
AND SiteId = @SiteID;

WITH PriceCTE(ProductID, CategoryID, SalePrice)
AS
(
    SELECT
        ProductID, CategoryID, SalePrice
    FROM
        CAT_Products
    WHERE
        CategoryID = @CategoryID
)
UPDATE PriceCTE
SET
    SalePrice = SalePrice + @Value *
    (
        CASE @Type WHEN '+' THEN 1 ELSE -1 END *
        CASE @Inc_Dec WHEN '%' THEN SalePrice / 100 ELSE 1 END
    )

END

我没有更改变量名称,但正如我在对您的问题的评论中所说,两个变量@Inc_Dec@Type看起来好像需要交换位置。我只是根据他们的名字判断,特别是@Inc_Dec名称:一个更可能是+-,而不是%或者其他什么是相反的价值。但这只是一个假设,你必须更好地了解你的东西。