使用sqlserver 2012将临时表更改为表变量

时间:2016-01-11 08:14:38

标签: loops sql-server-2012

我想从我的查询中删除#temp1并将其用作另一个表变量,它与使用#temp1表一样工作。 这是我的查询

DROP TABLE #temp1
;WITH cteSales AS(
    SELECT BillDate,
           BillNo,
           '-' AS     NarrationNumber,
           TotalAmount,
           0       AS ReceivedAmount
    FROM   SalesMaster
    WHERE  SalesMaster.ClientID = @ClientID
           AND SalesMaster.BillDate BETWEEN @FromDate AND @ToDate
    UNION ALL 
    SELECT [Date]  AS BillDate,
           SalesAccount.BillNo,
           SalesAccount.NarrationNumber,
           0       AS TotalAmount,
           Amount  AS ReceivedAmount
    FROM   SalesAccount
           LEFT JOIN SalesMaster sm
                ON  sm.BillNo = SalesAccount.BillNO
    WHERE  sm.ClientID = @ClientID
           AND sm.BillDate BETWEEN @FromDate AND @ToDate
), cteFormattedSales AS(
    SELECT ROW_NUMBER() OVER(ORDER BY BillNo ASC, BillDate ASC) AS RowNum,
           *
    FROM   cteSales
)

SELECT cfs1.RowNum,
       cfs1.BillDate,
       cfs1.BillNo,
       cfs1.NarrationNumber,
       cfs1.TotalAmount,
       cfs1.ReceivedAmount,
       (cfs1.TotalAmount - cfs1.ReceivedAmount) AS DueAmount INTO #temp1
FROM   cteFormattedSales cfs1
DECLARE @BillDate date,
        @PrevBillNo INT,
        @NextBillNo INT,
        @NarrationNumber VARCHAR(15),
        @TotalAmount DECIMAL(18, 2),
        @ReceivedAmount DECIMAL(18, 2),
        @NextDue DECIMAL(18, 2),
        @PrevDue DECIMAL(18, 2)
DECLARE @finalTable TABLE(
            BillDate date,
            BillNo INT,
            NarrationNumber NVARCHAR(15),
            TotalAmount DECIMAL(18, 2),
            ReceivedAmount DECIMAL(18, 2),
            DueAmount DECIMAL(18, 2)
        )

DECLARE @TotalRecords        INT,
        @Counter             INT = 1,
        @CarryOverAmount     DECIMAL(18, 2)

SELECT @TotalRecords = COUNT(*)
FROM   #temp1

SELECT @BillDate = t.BillDate,
       @PrevBillNo          = t.BillNo,
       @NarrationNumber     = t.NarrationNumber,
       @TotalAmount         = t.TotalAmount,
       @ReceivedAmount      = t.ReceivedAmount,
       @PrevDue             = t.DueAmount
FROM   #temp1 t
WHERE  t.RowNum = @Counter

WHILE @Counter <= @TotalRecords
BEGIN
    SELECT @BillDate = t.BillDate,
           @NextBillNo          = t.BillNo,
           @NarrationNumber     = t.NarrationNumber,
           @TotalAmount         = t.TotalAmount,
           @ReceivedAmount      = t.ReceivedAmount,
           @NextDue             = t.DueAmount
    FROM   #temp1 AS t
    WHERE  t.RowNum = @Counter

    IF (@Counter = 1)
        SET @CarryOverAmount = @TotalAmount

    SET @PrevDue = @CarryOverAmount - @ReceivedAmount

    IF @PrevBillNo <> @NextBillNo
    BEGIN
        SET @PrevBillNo = @NextBillNo
        SET @CarryOverAmount = @TotalAmount + @PrevDue
        SET @PrevDue = 0
    END

    INSERT INTO @finalTable
      (
        BillDate,
        BillNo,
        NarrationNumber,
        TotalAmount,
        ReceivedAmount,
        DueAmount
      )
    VALUES
      (
        @BillDate,
        @PrevBillNo,
        @NarrationNumber,
        @TotalAmount,
        @ReceivedAmount,
        CASE 
             WHEN @NarrationNumber = '-' THEN @CarryOverAmount
             ELSE @PrevDue
        END
      )

    IF @PrevBillNo = @NextBillNo
    BEGIN
        IF @Counter > 1
        BEGIN
            SET @CarryOverAmount = CASE 
                                        WHEN @NarrationNumber = '-' THEN @CarryOverAmount 
                                             + @PrevDue
                                        ELSE @CarryOverAmount - @ReceivedAmount
                                   END
        END
    END

    SET @Counter = @Counter + 1
END 
SELECT *
FROM   @finalTable

这里我在无效的ReceivedAmount

上收到错误的代码
SELECT @BillDate = t.BillDate,
           @PrevBillNo          = t.BillNo,
           @NarrationNumber     = t.NarrationNumber,
           @TotalAmount         = t.TotalAmount,
           @ReceivedAmount      = t.ReceivedAmount,
           @PrevDue             = t.DueAmount
    FROM   @Temp1 t
    WHERE  t.RowNum = @Counter

    WHILE @Counter <= @TotalRecords
    BEGIN
        SELECT @BillDate = t.BillDate,
               @NextBillNo          = t.BillNo,
               @NarrationNumber     = t.NarrationNumber,
               @TotalAmount         = t.TotalAmount,
               @ReceivedAmount      = t.ReceivedAmount,
               @NextDue             = t.DueAmount
        FROM   @Temp1 AS t
        WHERE  t.RowNum = @Counter

1 个答案:

答案 0 :(得分:1)

您只需要执行3个步骤:

一个。 DECLARE表变量

INSERT INTO @temp1
SELECT cfs1.RowNum,
       cfs1.BillDate,
       cfs1.BillNo,
       cfs1.NarrationNumber,
       cfs1.TotalAmount,
       cfs1.ReceivedAmount,
       (cfs1.TotalAmount - cfs1.ReceivedAmount) AS DueAmount 
FROM   cteFormattedSales cfs1   
... etc ...

B中。重写SELECT ... INTO - &gt; INSERT INTO .. SELECT

{{1}}

℃。将所有#temp1替换为@ temp1