tsql - While循环问题使用临时表

时间:2012-09-28 11:11:00

标签: sql-server tsql while-loop

我们的DBA已将某个功能更改为一个程序,因此我正在修改我的一些程序以满足此要求。我在我的一个程序中遇到了一个问题,我有一个while循环。

我从新程序(#DGContol)填充临时表,然后进行以下while循环:

SELECT @MinRcd = MIN(RcdNum) FROM #PortfolioDisclosure 
SELECT @MaxRcd = MAX(RcdNum) FROM #PortfolioDisclosure 

SET @RcdNum = @MinRcd
WHILE @RcdNum <= @MaxRcd

BEGIN

    -- Temporarily assign values to variables
    SELECT
            @PortfolioGroup = PortfolioCode
        ,   @EndDateTime    = MaxPositionDate_DetailedDisclosure

    FROM  #PortfolioDisclosure
    WHERE RcdNum = @RcdNum


    INSERT INTO #PositionsTable
        SELECT


                fi.ID_ISIN                          AS [Fund_ISIN]                  
            ,   RTRIM(a.acct_id)                    AS [Internal_Portfolio_Code]    
            ,   a.acct_desc                         AS [Portfolio/Fund_Name]        
            ,   CONVERT(CHAR(11),p.as_of_tms,103)   AS [Portfolio_Date]         
            ,   a.alt_curr_cde                      AS [Portfolio_Base_Ccy]     
            ,   i.iss_desc                          AS [Security_Description]       
            ,   RTRIM(i.pref_iss_id)                AS [Security_ID SEDOL/Internal]
            ,   RTRIM(ia.iss_id)                    AS [Security_ID ISIN]           
            ,   i.denom_curr_cde                    AS [Denomination_Ccy]           

            ,   SUM(p.valval_alt_cmb_amt) OVER (PARTITION BY RTRIM(a.acct_id))      
                                                    AS [Total_Fund_Value]

            ,   p.orig_quantity                     AS [Shares/Par_Value]           
            ,   p.valval_alt_cmb_amt                AS [Market_Value]               
            ,   p.fld5_rte                          AS [Pct_of_NAV] 

            ,   SUM(CASE WHEN i.issue_cls1_cde = '010' THEN p.valval_alt_cmb_amt ELSE 0 END) OVER (PARTITION BY a.acct_id)      
                                                    AS [Cash/Cash_Equivalents]

            ,   i.inc_proj_cmb_rte                  AS [Coupon_Rate]                
            ,   CONVERT(CHAR(11),i.mat_exp_dte,103) AS [Maturity_Date]              


        FROM dw_position_dg AS p

            INNER JOIN #DGControl AS dgc -- [M]onthly, [M]ost recent position
                ON dgc.DataGrpCtlNum = p.data_grp_ctl_num

            INNER JOIN dw_ivw_acct AS a WITH (NOLOCK)
                ON a.acct_id = p.acct_id

            INNER JOIN dw_issue_dg AS i WITH (NOLOCK)
                ON i.instr_id = p.instr_id

            LEFT OUTER JOIN dw_issue_alt_id AS ia WITH (NOLOCK)
                ON ia.instr_id = i.instr_id
                AND ia.id_ctxt_typ = 'ISIN'

            INNER JOIN #PortfolioDisclosure AS fi
                ON fi.PortfolioCode = p.acct_id
                and fi.MaxPositionDate_DetailedDisclosure >= p.as_of_tms

        WHERE RTRIM(a.acct_id) NOT IN ( SELECT xref.internal_value FROM  dbo.DP_CrossReference as xref
                                        WHERE xref.Codeset_type_id = 10401 
                                        AND xref.Originator_id = 'DataVendorPortfolioExclusion')

            -- Clear down variable values
            SET @PortfolioGroup = NULL
            --SET @StartDateTime = NULL
            SET @EndDateTime = NULL

            -- Move to next record
            SET @RcdNum = @RcdNum + 1

END -- END WHILE LOOP

然而,这会返回大量重复记录。如果我用原始函数替换临时表#DGControl,那么我得到正确的记录数。

我真的不知道问题是什么,或者我如何在循环时重新编码,以便使用表#DGControl我得到正确的记录数。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

您是否将SELECT * FROM OldFunction(args ..)的输出与EXEC NewStoredProcedure args ...进行了比较?如果是这样,当DBA将函数重构为proc时,返回的数据是否相同或者是否有重复信息。

如果是这样,您可能需要首先从sp输出,然后通过剩余的代码运行它。简而言之,如果你对两者都使用相同的参数,但它们会给你不同的结果,那么你需要回到DBA。

答案 1 :(得分:0)

您未在此代码中使用任何本地变量(@PortfolioGroup,@ EndDateTime)。它只是插入相同的记录集N次。此外,我认为您可以将其编写为单个选择查询而不使用临时表或while循环,这样可以减少混淆。

答案 2 :(得分:0)

感谢您的反馈。我解决了这个问题。我的代码现在看起来如下:

BEGIN

SELECT @MinRcd = MIN(RcdNum) FROM #PortfolioDisclosure 
SELECT @MaxRcd = MAX(RcdNum) FROM #PortfolioDisclosure 

SET @RcdNum = @MinRcd
WHILE @RcdNum <= @MaxRcd

BEGIN

    -- Temporarily assign values to variables
    SELECT
            @PortfolioGroup = PortfolioCode
        ,   @EndDateTime    = MaxPositionDate_DetailedDisclosure

    FROM  #PortfolioDisclosure
    WHERE RcdNum = @RcdNum

    -- Insert DGControl record into table based on the MaxPositionDate_DetailedDisclosure from Portfolio Disclosure function
    INSERT INTO #DGControl
        EXEC InfoPortal..usp_Generic_DGControl '', '', @PortfolioGroup, '1', @EndDateTime, @EndDateTime, @PeriodType, 'M', 'POSITION'    -- [M]onthly, [M]ost recent position

    -- Insert into #PositionsTable
    INSERT INTO #PositionsTable
        SELECT
                fi.ID_ISIN                          AS [Fund_ISIN]                  
            ,   RTRIM(a.acct_id)                    AS [Internal_Portfolio_Code]    
            ,   a.acct_desc                         AS [Portfolio/Fund_Name]        
            ,   CONVERT(CHAR(11),p.as_of_tms,103)   AS [Portfolio_Date]         
            ,   a.alt_curr_cde                      AS [Portfolio_Base_Ccy]     
            ,   i.iss_desc                          AS [Security_Description]       
            ,   RTRIM(i.pref_iss_id)                AS [Security_ID SEDOL/Internal]
            ,   RTRIM(ia.iss_id)                    AS [Security_ID ISIN]           
            ,   i.denom_curr_cde                    AS [Denomination_Ccy]           

            ,   SUM(p.valval_alt_cmb_amt) OVER (PARTITION BY RTRIM(a.acct_id))      
                                                    AS [Total_Fund_Value]

            ,   p.orig_quantity                     AS [Shares/Par_Value]           
            ,   p.valval_alt_cmb_amt                AS [Market_Value]               
            ,   p.fld5_rte                          AS [Pct_of_NAV] 

            ,   SUM(CASE WHEN i.issue_cls1_cde = '010' THEN p.valval_alt_cmb_amt ELSE 0 END) OVER (PARTITION BY a.acct_id)      
                                                    AS [Cash/Cash_Equivalents]

            ,   i.inc_proj_cmb_rte                  AS [Coupon_Rate]                
            ,   CONVERT(CHAR(11),i.mat_exp_dte,103) AS [Maturity_Date]              

        FROM dw_position_dg AS p

            INNER JOIN #DGControl AS dgc
                ON dgc.DataGrpCtlNum = p.data_grp_ctl_num

            INNER JOIN dw_ivw_acct AS a WITH (NOLOCK)
                ON a.acct_id = p.acct_id

            INNER JOIN dw_issue_dg AS i WITH (NOLOCK)
                ON i.instr_id = p.instr_id

            LEFT OUTER JOIN dw_issue_alt_id AS ia WITH (NOLOCK)
                ON ia.instr_id = i.instr_id
                AND ia.id_ctxt_typ = 'ISIN'

            INNER JOIN #PortfolioDisclosure AS fi
                ON fi.PortfolioCode = p.acct_id

            -- Clear down variable values
            SET @PortfolioGroup = NULL
            --SET @StartDateTime = NULL
            SET @EndDateTime = NULL
            -- Clear down #DGControl table to allow new record to be inserted
            DELETE FROM #DGControl
            -- Move to next record
            SET @RcdNum = @RcdNum + 1

END -- END WHILE LOOP

在开头添加存储的proc usp_Generic_DGControl的执行,然后在每个循环停止重复记录后将其清除。