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?

hIpPy
  • 4,649
  • 6
  • 51
  • 65
  • As a workaround, once the scripts are created, I'm reading them, changing the CREATE to ALTER and writing them back out. – hIpPy Jul 07 '10 at 17:30

5 Answers5

4

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;
Steven Liekens
  • 13,266
  • 8
  • 59
  • 85
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. :).

Grim
  • 37
  • 2
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.

hIpPy
  • 4,649
  • 6
  • 51
  • 65
  • 1
    I agree - I tried this before and never found a method for scripting out an alter. I ended up resulting to using the ScriptingOptions (http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.scriptingoptions_members.aspx) to specify a check for existence and drop if found. – Bobby B Jul 13 '10 at 01:21
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.

Tim Stone
  • 19,119
  • 6
  • 56
  • 66
blorkfish
  • 21,800
  • 4
  • 33
  • 24
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
jcolebrand
  • 15,889
  • 12
  • 75
  • 121
SteveO
  • 477
  • 2
  • 9
  • 17