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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:40:25+00:00 2026-05-25T23:40:25+00:00

I have a query that runs against a sql server database that produces a

  • 0

I have a query that runs against a sql server database that produces a sql statement for each row of the query returned.

EG.

DROP PROCEDURE dbo.[Proc1]
DROP PROCEDURE dbo.[Proc2]

etc

In the same script, how can I execute the sql returned in the query?

  • 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-25T23:40:26+00:00Added an answer on May 25, 2026 at 11:40 pm

    Dump the results into a variable of type varchar(max) or nvarchar(max) and then EXEC them

    -- Create a terrible, horrible cursor to run the statements
    -- You have just now killed a kitten
    DECLARE Csr cursor for
    SELECT
        P.name AS proc_name
    ,   schema_name(P.schema_id) AS schemaname
    FROM
        sys.procedures P
    
    
    DECLARE @query varchar(max), @proc_name sysname, @schema sysname
    
    OPEN Csr
    FETCH NEXT FROM Csr INTO
        @proc_name, @schema
    
    WHILE (@@fetch_status <> -1)
    BEGIN
        IF (@@fetch_status <> -2)
        BEGIN
            BEGIN TRY
                -- build out the drop statement, may not be necc
                SET @query = 'DROP PROCEDURE' + QUOTENAME(@schema) + '.' + QUOTENAME(@proc_name)
                -- Actually run the dynamic query
                EXECUTE(@query)
            END TRY
            BEGIN CATCH
                -- Be good and note what failed, fix this manually
                PRINT 'This query failed'
                PRINT @query
            END CATCH
            FETCH NEXT FROM Csr INTO
                @proc_name, @schema
        END
    END
    
    CLOSE Csr
    DEALLOCATE Csr
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a monster query that I'm running against a SQL SERVER 2005 database
I have a query that runs super fast when executed in the sql editor
I have a sql query that runs super fast, around one second, when not
I have the following query that runs in my Oracle database and I want
I have a Delphi 2009 application that runs a query over a database using
I have a situation where a certain linq query that runs against a multi-million
I have a LINQ-to-SQL query that runs through a table, that I want to
i have a client that runs a windows application on a sql server 2000
I have a java program that runs a bunch of queries against an sql
I have an SQL query thats runs on the Postgres database of my Django

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.