SQL存储过程使用While循环和结果插入临时表

时间:2014-07-08 08:25:43

标签: sql sql-server-2008 ms-access stored-procedures while-loop

我正在尝试创建一个带有循环的动态存储过程,该循环将数据插入到SQL Server 2008中的临时表中。

到目前为止,我有这段代码,但不确定创建表的逻辑是否正确,我不知道在哪里放置查询的插入部分。

CREATE PROCEDURE [dbo].[SP_NominalRecord]
@ORGCODE VARCHAR(6),
@CurrentPeriod INT,
@ENDPeriod INT, 
@Nominal INT

AS 
BEGIN 
SET NOCOUNT ON;
DECLARE @SQL NVARCHAR(MAX)

 SET @ENDPeriod = @CurrentPeriod-5

IF OBJECT_ID('tempdb..#TempResults')IS NOT NULL DROP TABLE #TempResults

WHILE (@CurrentPeriod<> @ENDPeriod)--loop through until endperiod is reached

    BEGIN
        CREATE TABLE #TempResults(ID bigint IDENTITY(1,1) NOT NULL PRIMARY KEY, CostCode INT, CostDescr VARCHAR(50), BankACType CHAR(2), FinPeriodNr SMALLINT, 
                BalJan DECIMAL(12, 2), BalFeb DECIMAL(12, 2), BalMar DECIMAL(12, 2),
                BalApr DECIMAL(12, 2), BalMay DECIMAL(12, 2), BalJun DECIMAL(12, 2),
                BalJul DECIMAL(12, 2), BalAug DECIMAL(12, 2), BalSep DECIMAL(12, 2),
                BalOct DECIMAL(12, 2), BalNov DECIMAL(12, 2), BalDec DECIMAL(12, 2), Balance DECIMAL(12, 2))


    SET @SQL = N'SELECT CostCodes.CostCode, CostCodes.CostDescr, CostCodes.BankACType, CostCodeBalance' + @ORGCODE + '.FinPeriodNr, 
                CostCodeBalance' + @ORGCODE + '.BalJan, CostCodeBalance' + @ORGCODE + '.BalFeb, CostCodeBalance' + @ORGCODE + '.BalMar,
                CostCodeBalance' + @ORGCODE + '.BalApr, CostCodeBalance' + @ORGCODE + '.BalMay, CostCodeBalance' + @ORGCODE + '.BalJun,
                CostCodeBalance' + @ORGCODE + '.BalJul, CostCodeBalance' + @ORGCODE + '.BalAug, CostCodeBalance' + @ORGCODE + '.BalSep,
                CostCodeBalance' + @ORGCODE + '.BalOct, CostCodeBalance' + @ORGCODE + '.BalNov, CostCodeBalance' + @ORGCODE + '.BalDec,
                Sum(CostCodeBalance' + @ORGCODE + '.BalJan + CostCodeBalance' + @ORGCODE + '.BalFeb + CostCodeBalance' + @ORGCODE + '.BalMar +
                CostCodeBalance' + @ORGCODE + '.BalApr + CostCodeBalance' + @ORGCODE + '.BalMay + CostCodeBalance' + @ORGCODE + '.BalJun +
                CostCodeBalance' + @ORGCODE + '.BalJul + CostCodeBalance' + @ORGCODE + '.BalAug + CostCodeBalance' + @ORGCODE + '.BalSep +
                CostCodeBalance' + @ORGCODE + '.BalOct + CostCodeBalance' + @ORGCODE + '.BalNov + CostCodeBalance' + @ORGCODE + '.BalDec) AS Balance

        FROM CostCodeBalance' + @ORGCODE + ' INNER JOIN CostCodes ON CostCodeBalance' + @ORGCODE + '.CostCode = CostCodes.CostCode 

        WHERE Costcodes.Costcode = @Nominal AND CostCodeBalance' + @ORGCODE + '.FinPeriodNr =@CurrentPeriod

        GROUP BY CostCodes.CostCode, CostCodes.CostDescr, CostCodes.BankACType, CostCodeBalance' + @ORGCODE + '.FinPeriodNr, 
                CostCodeBalance' + @ORGCODE + '.BalJan, CostCodeBalance' + @ORGCODE + '.BalFeb, CostCodeBalance' + @ORGCODE + '.BalMar,
                CostCodeBalance' + @ORGCODE + '.BalApr, CostCodeBalance' + @ORGCODE + '.BalMay, CostCodeBalance' + @ORGCODE + '.BalJun,
                CostCodeBalance' + @ORGCODE + '.BalJul, CostCodeBalance' + @ORGCODE + '.BalAug, CostCodeBalance' + @ORGCODE + '.BalSep,
                CostCodeBalance' + @ORGCODE + '.BalOct, CostCodeBalance' + @ORGCODE + '.BalNov, CostCodeBalance' + @ORGCODE + '.BalDec;';

    SET @CurrentPeriod = @CurrentPeriod -1; --subtrace current period by 1
    END

    EXEC sp_executesql @sql, N'@ORGCODE VARCHAR(6),@CurrentPeriod INT, @ENDPeriod INT, @NOMINAL INT', @ORGCODE,@CurrentPeriod, @ENDPeriod, @NOMINAL; 

    SELECT * FROM #TempResults --to select the results of the code above

END

我还是SQL的新手,所以让我知道你的想法。我将此称为Access 2013中的传递查询。

1 个答案:

答案 0 :(得分:1)

您需要在查询中检查一些内容:

  1. @ENDPeriod是一个输入变量,因为你已经对它进行了编码 值SET @ENDPeriod = @CurrentPeriod-5确保它是。{ 正确的逻辑。
  2. CostCodeBalance' + @ORGCODE + '.FinPeriodNr如果这是一个整数值,那么您可以将其与CostCodeBalance' + @ORGCODE + '.FinPeriodNr BETWEEN @CurrentPeriod AND @ENDPeriod进行比较。
  3. 所以重写你的sproc:

    CREATE PROCEDURE [dbo].[SP_NominalRecord]
    @ORGCODE VARCHAR(6),
    @CurrentPeriod INT,
    @ENDPeriod INT, 
    @Nominal INT
    
    AS 
    BEGIN 
    SET NOCOUNT ON;
    DECLARE @SQL NVARCHAR(MAX), 
            @END INT;
    
    SET @ENDPeriod = @CurrentPeriod-5 -- Please verify if this logic is actually required
    
    IF OBJECT_ID('tempdb..#TempResults')IS NOT NULL DROP TABLE #TempResults
    
    --WHILE (@CurrentPeriod<> @ENDPeriod)--loop through until endperiod is reached 
    -- No need of while loop so commented    
        --BEGIN
    
            CREATE TABLE #TempResults
            (ID bigint IDENTITY(1,1) NOT NULL PRIMARY KEY,
             CostCode INT,
             CostDescr VARCHAR(50),
             BankACType CHAR(2),
             FinPeriodNr SMALLINT,
             BalJan DECIMAL(12, 2),
             BalFeb DECIMAL(12, 2),
             BalMar DECIMAL(12, 2),
             BalApr DECIMAL(12, 2),
             BalMay DECIMAL(12, 2),
             BalJun DECIMAL(12, 2),
             BalJul DECIMAL(12, 2),
             BalAug DECIMAL(12, 2),
             BalSep DECIMAL(12, 2),
             BalOct DECIMAL(12, 2),
             BalNov DECIMAL(12, 2),
             BalDec DECIMAL(12, 2),
             Balance DECIMAL(12, 2))
    
    
        SET @SQL = N'
        SELECT CostCodes.CostCode,
        CostCodes.CostDescr,
        CostCodes.BankACType,
        CostCodeBalance' + @ORGCODE + '.FinPeriodNr, 
        CostCodeBalance' + @ORGCODE + '.BalJan,
        CostCodeBalance' + @ORGCODE + '.BalFeb,
        CostCodeBalance' + @ORGCODE + '.BalMar,
        CostCodeBalance' + @ORGCODE + '.BalApr,
        CostCodeBalance' + @ORGCODE + '.BalMay,
        CostCodeBalance' + @ORGCODE + '.BalJun,
        CostCodeBalance' + @ORGCODE + '.BalJul,
        CostCodeBalance' + @ORGCODE + '.BalAug,
        CostCodeBalance' + @ORGCODE + '.BalSep,
        CostCodeBalance' + @ORGCODE + '.BalOct,
        CostCodeBalance' + @ORGCODE + '.BalNov,
        CostCodeBalance' + @ORGCODE + '.BalDec,
        Sum(CostCodeBalance' + @ORGCODE + '.BalJan
        + CostCodeBalance' + @ORGCODE + '.BalFeb +
        CostCodeBalance' + @ORGCODE + '.BalMar +
        CostCodeBalance' + @ORGCODE + '.BalApr +
        CostCodeBalance' + @ORGCODE + '.BalMay + 
        CostCodeBalance' + @ORGCODE + '.BalJun +
        CostCodeBalance' + @ORGCODE + '.BalJul + 
        CostCodeBalance' + @ORGCODE + '.BalAug + 
        CostCodeBalance' + @ORGCODE + '.BalSep +
        CostCodeBalance' + @ORGCODE + '.BalOct + 
        CostCodeBalance' + @ORGCODE + '.BalNov + 
        CostCodeBalance' + @ORGCODE + '.BalDec) AS Balance
        FROM CostCodeBalance' + @ORGCODE + ' 
        INNER JOIN CostCodes ON CostCodeBalance' + @ORGCODE + '.CostCode 
        = CostCodes.CostCode 
        WHERE Costcodes.Costcode = @Nominal 
        AND CostCodeBalance' + @ORGCODE + '.FinPeriodNr 
        BETWEEN @CurrentPeriod  AND @ENDPeriod
        GROUP BY CostCodes.CostCode,
        CostCodes.CostDescr, 
        CostCodes.BankACType, 
        CostCodeBalance' + @ORGCODE + '.FinPeriodNr, 
        CostCodeBalance' + @ORGCODE + '.BalJan,
        CostCodeBalance' + @ORGCODE + '.BalFeb,
        CostCodeBalance' + @ORGCODE + '.BalMar,
        CostCodeBalance' + @ORGCODE + '.BalApr,
        CostCodeBalance' + @ORGCODE + '.BalMay,
        CostCodeBalance' + @ORGCODE + '.BalJun,
        CostCodeBalance' + @ORGCODE + '.BalJul,
        CostCodeBalance' + @ORGCODE + '.BalAug,
        CostCodeBalance' + @ORGCODE + '.BalSep,
        CostCodeBalance' + @ORGCODE + '.BalOct,
        CostCodeBalance' + @ORGCODE + '.BalNov, 
        CostCodeBalance' + @ORGCODE + '.BalDec;';
    
        --SET @CurrentPeriod = @CurrentPeriod -1; --subtrace current period by 1
        --END
    
        EXEC sp_executesql @sql, N'@ORGCODE VARCHAR(6),@CurrentPeriod INT, @ENDPeriod INT
        , @NOMINAL INT', @ORGCODE,@CurrentPeriod, @ENDPeriod, @NOMINAL; 
    
        SELECT * FROM #TempResults --to select the results of the code above
    
    END