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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:33:41+00:00 2026-06-07T15:33:41+00:00

I have a seemingly simple request. I need to script all the stored procedures

  • 0

I have a seemingly simple request. I need to script all the stored procedures in a database following this criteria.

  1. The scripts need to include dropping if the proc exists like follows…

    IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[myproc]') AND type in (N'P', N'PC'))
    
  2. The script cannot have sp_executesql so nothing like this…

    EXEC dbo.sp_executesql @statement = '....'
    
  3. I need a separate script file for each stored procedure. So it would be [stored procedure name].sql

I notice when I try the built in sql generate scripts I can get the procs in a separate file via the checkbox for scripting objects in separate files and I can also get the if exists drop. However, it uses sp_executesql which they don’t want.

So I spent a bit trying SMO and found similar issues…

A. Sadly the following just scripts only the drop statements. No way I see to combine it with the create. So I can get the separate files and no sp_executesql but I’m still missing #1 above

    Scripter scripter = new Scripter();
    scripter.Options.ScriptDrops = true;

B. Secondly, the following option changes the output to use sp_executesql

    scripter.Options.IncludeIfNotExists = true;

C. Finally, I can add the text manually. It successfully is set in the TextHeader. However, the scripter.Script() throws an exception “{“Script failed for StoredProcedure ‘dbo.myproc’. “}

    storedProcedure.TextHeader = string.Format("IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'{0}') AND type in (N'P', N'PC')) \r\nDROP PROCEDURE {0} \r\nGO\r\n{1}", storedProcedure.Name, storedProcedure.TextHeader);
    scripter.Options.FileName = Path.Combine(storedProceduresPath, storedProcedure.Name + ".sql");
    scripter.Script(new Urn[] { storedProcedure.Urn });  //Exception! - Script failed for StoredProcedure

I can’t imagine this is all that strange of a thing to do so I’m wondering how people are accomplishing this??? Sad if needing to create the separate files using the sql – tasks – generate scripts, followed by an app to clear out the unwanted “EXEC dbo.sp_executesql @statement = N'”

  • 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-07T15:33:42+00:00Added an answer on June 7, 2026 at 3:33 pm

    The following code samples are in PowerShell, but uses SMO, so you can easily transform it to c#.

    Scripter felt a bit slow, so i’m using a slightly unorthodox method, but this worked for hundreds of times for me.

    $sp is a stored procedure in your db, just foreaching all SPs:

    foreach($sp in $db.StoredProcedures)
    

    Script drop: Just use String.Format 🙂

    $sbHead.AppendLine( [string]::Format(@"
    IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[{0}].[{1}]') AND type in (N'P', N'PC'))
    GO
    "@, $sp.Schema, $sp.Name))
    

    Script body

    $sbBody.AppendLine( [string]::Format(@"
    print 'creating "{1}"...'
    --   *  *  *  *  #{0}:{1}; CreateDate:{2}, DateLastModified:{3}   *  *  *  *
    GO
    {4}
    {5}
    GO
    "@, $cnt, $sp.Name, $sp.CreateDate, $sp.DateLastModified, $sp.TextHeader, $sp.TextBody))
    

    This prints out some extra info:

    • print 'creating "{1}"...' put it in there because one time the
      creation just hung for minutes. Knowing the current SP name lead us
      to the source of the problem: connection to a linked server was
      down…
    • SP creation and last modified date, but thats not really needed.

    This method does not takes script dependencies into account, so you will get a warning message in the console like “The current SP xy depends SP dsa, but creating it anyway”. But runs faster because of not working out those dependencies. Worked OF for me all the time…

    Uploaded the full script, and an example output to GitHub: SqlScriptExport.ps1

    This one does a few extra things:

    • script only SPs that start with a given prefix (or all, if the prefix is empty)
    • scripts views as well, same filtering for prefixes.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this seemingly simple piece of PHP in my website: <?php $_GET[sid]; if
I have a seemingly simple question that I need help with. I have a
I have the following seemingly simple scenario, however I'm still pretty new to NHibernate.
I have been trying to get this seemingly simple problem fixed for about half
I have a seemingly simple problem whereby I wish to reconcile two lists so
I have a seemingly simple problem, but I can't quite figure out a solution.
Forgive me for this seemingly obvious question, but my searches have not revealed the
Ok this is seemingly impossible to get right. I have a text box and
I have a seemingly simple requirement, but i can't figure out how to write
What I am trying to do is seemingly simple. I have a master report

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.