6

I am using the Microsoft.SqlServer.Management.Smo classes to script out SQL scripts for stored procedures, tables, views etc. I am doing this for putting them in source control.

Instead of the CREATE scripts for stored procedures, how can I get ALTER scripts for them? Is there an setting / option in the ScriptingOptions' properties?

1
  • As a workaround, once the scripts are created, I'm reading them, changing the CREATE to ALTER and writing them back out. Commented Jul 7, 2010 at 17:30

5 Answers 5

5

The StoredProcedure class has a method that creates ALTER statements.

https://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.storedprocedure.scriptheader(v=sql.120).aspx

Usage

var sprocHeader = sproc.ScriptHeader(forAlter: true);
var sprocBody = sproc.TextBody;
Sign up to request clarification or add additional context in comments.

1 Comment

This actually is the key point for SP creating "alter" script. Thanks !
2
  1. Use the normal scriptingoptions and tools for SMO c#
  2. then simply use.

    foreach (string line in script)
                {
                    string l = line.Replace("CREATE FUNCTION", "ALTER FUNCTION");
                }
    
  3. Ta Da. :).

Comments

1

There seems to be no setting for ALTER scripts. The property with ScriptingOptions.ScriptDrops as true creates the drop statements. The downside to this is that the permissions would have to reassigned.

Closing this question.

1 Comment

I agree - I tried this before and never found a method for scripting out an alter. I ended up resulting to using the ScriptingOptions (msdn.microsoft.com/en-us/library/…) to specify a check for existence and drop if found.
1

Have a look at DBSourceTools.

It is a GUI tool to script all objects in a SQL Database to disk, specifically for source-code control of databases.The back-end uses SMO.

Comments

0

I use this in compare between servers. toSP is the textbody of the SP. If there is none on the "TO" server I do Create() otherwise it is Alter() for the loaded up StoredProcedure spT

If toSP = "" Then  ' Empty
    spT.Create()
Else
    spT.Alter()
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.