0

我们的 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,那么我会得到正确数量的记录。

我真的不知道问题是什么,也不知道如何重新编码这个 while 循环,以便使用表#DGControl 我得到正确数量的记录。任何人都可以帮忙吗?

4

3 回答 3

0

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

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

于 2012-09-28T13:19:57.200 回答
0

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

于 2012-09-28T16:25:48.647 回答
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

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

于 2012-10-01T13:16:59.410 回答