Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8614767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:11:58+00:00 2026-06-12T05:11:58+00:00

Our DBA has changed a function to be a procedure, so I am amending

  • 0

Our DBA has changed a function to be a procedure, so I am amending some of my procedures to cater for this. I’ve come across a problem with this in one of my procedures where I have a while loop.

I populate my temp table from the new procedure (#DGContol), and then have the following while loop:

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

However this returns lots of duplicate records. If I replace the temp table #DGControl with the original function then I get the correct number of records.

I don’t really know what the issue would be or how I could re code this while loop so that using the table #DGControl I get the correct number of records. Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T05:12:00+00:00Added an answer on June 12, 2026 at 5:12 am

    Thank you for your feedback. i worked out the issue. My code looks like the following now:

    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
    

    Adding the execution of the stored proc usp_Generic_DGControl at the beginning and then clearing it down after each loop stopped the duplication of the records.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our DBA requires us to return all tabular data from stored procedures in a
our dba wants to change from recieving files as attachements to some sort of
We've recently updated to Oracle 11g and our DBA has suggested using result caching
Per a question I posted yesterday , our website's DNS structure has changed to
I work at an eCommerce company. Our DBA recently told me that using SQL
Our application uses Hibernate with Sql Server 2005. Being a DBA, I am not
Our company has a share point document server where the UNC looks something like
One of our customers does not want to put the default instance name MSSQLSERVER
My colleague mentioned that our client DBA proposed the removal of all foreign key
In one of the sites I manage, the client has decided to take on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.