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

  • Home
  • SEARCH
  • 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 934935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:04:49+00:00 2026-05-15T21:04:49+00:00

I have a query which uses a cursor to cycle through the results of

  • 0

I have a query which uses a cursor to cycle through the results of a select statement.

The select statement in short selects all of the records from a mapping table I have. One of the columns is ‘SourceTableName’.

I use this field to generate some dynamic SQL.

I am looking to add a parameter to my stored procedure wrapped around this, which will allow me to only create dynamic SQL for the ‘SourceTableName’ that I want – IF I pass in a ‘SourceTableNameFilter’.

I am stuck with some logic which wraps my dynamic SQL.

IF @SourceTableNameFilter(SP parameter) = @SourceTableName(from mapping table)
BEGIN

Generate and execute some dynamic SQL based on the SourceTableName.

The problem is, I want this to either work on all tables that come back from a select against ‘SourceTableName’ BUT if a @SourceTableNameFilter parameter is present and not null – then only generate dynamic SQL for any rows in the cursor which match my filter parameter.

Is there a way for me to accomplish this with an IF statement without copying the logic inside the IF/ELSE twice?

 FETCH NEXT FROM TABLECUR INTO @SourceTableName
     ,@SourceInColumn
     ,@SourceOutColumn
     ,@TargetTableName
     ,@TargetLookupColumn
     ,@TargetLookupResultColumn
     ,@MappingTableID

 WHILE (@@fetch_status <> -1)
 BEGIN
  IF (@@fetch_status <> -2)
  BEGIN


                IF (@SourceTableName = @SourceTableNameFilter)

                 --GENERATE DYNAMIC SQL

                ELSE 
                 --GENERATE DYNAMIC SQL FOR ALL RECORDS

The generate dynamic SQL string is the same in both the if and the else, any way to change the conditions so that I’m not duplicating the dynamic SQL generation and to not generate dynamic SQL when the @SourceTableName != @SourceTableNameFilter?

Thank you

  • 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-05-15T21:04:49+00:00Added an answer on May 15, 2026 at 9:04 pm

    Consider adding this logic to the cursor definition, rather than having that logic within the processing of each cursor record.

    So if the cursor is normally:

    DECLARE MY_CURSOR Cursor     FOR
    SELECT SourceTableName, SourceInColumn, SourceOutColumn
           ,TargetTableName, TargetLookupColumn
           ,TargetLookupResultColumn, MappingTableID
    
    FROM MappingTable
    --get source tables when filter is specified; otherwise get all
    WHERE (SourceTableName = @SourceTableNameFilter) OR (LEN(ISNULL(SourceTableNameFilter,'')=0)
    

    Now you can execute your business logic within the cursor without having to detect the filtered table or not. The cursor is loaded with the records you need to care about. It sounds, from the question, that the business logic is the same, no matter if the filter was passed in or not. If this is incorrect, or if it doesn’t satisfy your requirement, please comment.

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

Sidebar

Related Questions

I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
I have a query which looks like this: SELECT LossCost, CoverageID FROM BGILossCost] WHERE
I have the following query, which uses a CASE statement. Is there anyway to
We have an WCF application which uses NHibernate to query data from the database.
I have a MySQL SELECT query which uses 20 different comparisons within the same
I have the query below which uses a union to pull data from three
i have been working on query which uses compute by clause for avg and
I have a query which returns a series of cells of data from a
I have the following query which uses a date variable, which is generated inside
I have a huge query which uses case/when often. Now I have this SQL

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.