Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create ALTER scripts instead of CREATE scripts using SMO (Server Management Object)

Tags:

c#

tsql

smo

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?

like image 983
hIpPy Avatar asked Jul 01 '10 16:07

hIpPy


People also ask

How do you create a table script in SQL Server Management Studio?

Generate a Script in the SQL Server Management Studio Open SQL Server Management Studio (SSMS) Expand Databases. Select the database to script. Right-click on the database and select Tasks > Generate Scripts.


2 Answers

  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. :).

like image 80
Grim Avatar answered Oct 18 '22 03:10

Grim


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;
like image 20
Steven Liekens Avatar answered Oct 18 '22 05:10

Steven Liekens