SQL Server 2012中的级联While循环

时间:2019-04-29 15:10:32

标签: sql sql-server tsql

尝试为当前正在处理的项目生成客户分类帐报告。该报告的外观应如下图所示:

enter link description here

为此,我编写了以下T-SQL代码:

CREATE PROCEDURE sp_ReportCustomerLedger 
    @fromDate DATETIME, @toDate DATETIME    
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @customers TABLE (cust_row_Id INT IDENTITY(1,1) PRIMARY KEY, AccountId INT)

    INSERT INTO @customers 
        SELECT a.AccountId 
        FROM tbl_Customer a 
        RIGHT JOIN tbl_Invoice b ON a.CustomerId = b.CustomerId
        GROUP BY b.CustomerId, a.AccountId 
        ORDER BY a.AccountId

    DECLARE @customerCount INT = 0

    SELECT @customerCount = COUNT(*) FROM @customers
    DECLARE @i INT = 1

    WHILE(@i <= @customerCount)
    BEGIN
        DECLARE @accountId INT

        SELECT @accountId = AccountId 
        FROM @customers 
        WHERE cust_row_Id = @i

        DECLARE @transactions TABLE (trans_row_id INT IDENTITY(1,1) PRIMARY KEY, Amount DECIMAL(18,2), TransactionDate DATE, EntryType INT, TransactionId INT)

       INSERT INTO @transactions 
           SELECT a.Amount, b.TransactionDate, a.EntryType, a.TransactionId 
           FROM tbl_Ledger a 
           LEFT JOIN tbl_Transaction b ON a.TransactionId = b.TransactionId
           WHERE a.AccountId = @accountId 
             AND b.TransactionDate BETWEEN @fromDate AND @toDate
           ORDER BY b.TransactionDate

       DECLARE @transCount INT = 0
       DECLARE @j INT = 0

       SELECT @transCount = COUNT(*) FROM @transactions

       DECLARE @result TABLE (TransactionDate DATE, Dr DECIMAL(18,2), Cr DECIMAL(18,2), Balance DECIMAL(18,2))

       DECLARE @initialBalance DECIMAL(18,2) =  0
       DECLARE @initialDr DECIMAL(18,2) =  0
       DECLARE @initialCr DECIMAL(18,2) =  0

       SELECT @initialDr = SUM(a.Amount) 
       FROM tbl_Ledger a 
       LEFT JOIN tbl_Transaction b ON a.TransactionId = b.TransactionId
       WHERE a.AccountId = @accountId 
         AND b.TransactionDate <= @fromDate 
         AND a.EntryType = 2

       SELECT @initialCr = SUM(a.Amount) 
       FROM tbl_Ledger a 
       LEFT JOIN tbl_Transaction b ON a.TransactionId = b.TransactionId
       WHERE a.AccountId = @accountId AND b.TransactionDate <= @fromDate AND a.EntryType = 1

SET @initialBalance = @initialDr - @initialCr

WHILE(@j<=@transCount)
BEGIN

DECLARE @entryType INT = (SELECT EntryType FROM @transactions WHERE trans_row_id = @j)
DECLARE @amount DECIMAL = (SELECT Amount FROM @transactions WHERE trans_row_id = @j)
DECLARE @transactionDate DATE = (SELECT TransactionDate FROM @transactions WHERE trans_row_id = @j)
DECLARE @dr DECIMAL(18, 0) = 0
DECLARE @cr DECIMAL(18, 0) = 0
DECLARE @balance DECIMAL(18, 0) = 0

IF(@entryType = 2)
BEGIN
SET @dr = @amount
SET @balance = @initialBalance + @dr
END
ELSE
BEGIN
SET @cr = @amount
SET @balance = @initialBalance - @cr
END

INSERT INTO @result SELECT @transactionDate, @dr, @cr, @balance


SET @j = @j + 1
END;

SELECT @initialBalance AS 'Initial Balance';

SELECT * FROM @result;


DELETE FROM @result
DELETE FROM @transactions

SET @i = @i + 1
END;

DELETE FROM @customers


END
GO

问题:执行完脚本后,空白数据将不停地运行。在每个出口处,我都确保清除了表变量或删除了它们的数据。

0 个答案:

没有答案