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 8335477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:41:20+00:00 2026-06-09T03:41:20+00:00

I have dynamic SQL stored in a SQL table that I have to execute

  • 0

I have dynamic SQL stored in a SQL table that I have to execute under certain conditions. Currently, we use cursors to handle that for us, but I was always told to avoid cursors when possible as they aren’t the most efficient way of doing things. So, my question is: how do I execute dynamic SQL without them (if there’s a way)? The entire system is built around this dynamic SQL mess, so there is no changing it.

For this, just assume the table has Id AS IDENTITY and SQL AS VARCHAR fields, where the SQL field contains the SQL to be executed (obviously).

EDIT:
Basically, I want to loop through the table and execute the SQL in the SQL column.

So, a row in the table will basically look like this:

ID   SQL
--   ----------------------
1    SELECT * FROM RECORD
2    SELECT * FROM PERSON
3    SELECT * FROM LOCATION

I haven’t written any code because what I’d write is a cursor to traverse through the table and execute it. I just don’t know of any other ways of looping a table and executing that string as a SQL query other than something like:

DECLARE @sql VARCHAR(MAX)

DECLARE _cursor CURSOR
FOR
    SELECT  [SQL]
    FROM    #tmp2

OPEN _cursor

FETCH NEXT FROM _cursor INTO @sql

WHILE @@FETCH_STATUS = 0 
    BEGIN
        PRINT ( @sql )
    END
CLOSE _cursor   
DEALLOCATE _cursor
  • 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-09T03:41:22+00:00Added an answer on June 9, 2026 at 3:41 am

    You can use any number of concatenation tricks to make one big batch without using a cursor, I personally use the FOR XML trick a lot.

    Here’s an overview:

    http://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

    However, the cursor (while generally a code smell) isn’t going to contribute a terrible amount to the non-performance of this. And you will have an opportunity to handle errors etc a lot easier than with a single batch.

    In addition, if you have DDL in some of those statements which has to be the first statement in a batch, then you would need to submit them in separate batches. EXEC or sp_executesql doesn’t implement any batch splitting like SSMS has the GO batch separator.

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

Sidebar

Related Questions

I have a dynamic SQL statement I've created in a stored procedure. I need
I have a messy stored procedure which uses dynamic sql. I can debug it
It looks like #temptables created using dynamic SQL via the EXECUTE string method have
I have a stored procedure that takes in the name of a table as
I have stored my tags in the SQL Server database, TABLE NAME: Tags COLUMNS
I have a SQL stored proc that uses a linked server and Openquery to
I'm using SQL Server 2008 Express, and I have a stored procedure that do
I currently have a linked server that I am querying in a stored procedure.
I have dynamic listview on my android client app that receive data from remote
I have some divs that have dynamic heights controlled by a 'click' function as

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.