Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to programmatically get all stored procedures

Is there a way to get stored procedures from a SQL Server 2005 Express database using C#? I would like to export all of this data in the same manner that you can script it our using SQL Server Management Studio, without having to install the GUI.

I've seen some references to do thing via the PowerShell but in the end a C# console app is what I really want.

To clarify....

I'd like to script out the stored procedures. The list via the Select * from sys.procedures is helpful, but in the end I need to script out each of these.

like image 331
Douglas Anderson Avatar asked Oct 03 '08 23:10

Douglas Anderson


People also ask

How do I get the script of all stored procedures in SQL Server?

Export Stored Procedure in SQL ServerIn the Object Explorer, right-click on your database. Select Tasks from the context menu that appears. Select the Generate Scripts command.


2 Answers

public static void GenerateTableScript()
    {
        Server databaseServer = default(Server);//DataBase Server Name
        databaseServer = new Server("yourDatabase Server Name");
        string strFileName = @"C:\Images\Your FileName_" + DateTime.Today.ToString("yyyyMMdd") + ".sql"; //20120720`enter code here
        if (System.IO.File.Exists(strFileName))
            System.IO.File.Delete(strFileName);
        List<SqlSmoObject> list = new List<SqlSmoObject>();
        Scripter scripter = new Scripter(databaseServer);
        Database dbUltimateSurvey = databaseServer.Databases["YourDataBaseName"];//DataBase Name
        //Table scripting Writing
        DataTable dataTable1 = dbUltimateSurvey.EnumObjects(DatabaseObjectTypes.Table);
        foreach (DataRow drTable in dataTable1.Rows)
        {
            //string strTableSchema = (string)drTable["Schema"];
            //if (strTableSchema == "dbo")
            //    continue;
            Table dbTable = (Table)databaseServer.GetSmoObject(new Urn((string)drTable["Urn"]));
            if (!dbTable.IsSystemObject)
                if (dbTable.Name.Contains("SASTool_"))
                    list.Add(dbTable);
        }
        scripter.Server = databaseServer;
        scripter.Options.IncludeHeaders = true;
        scripter.Options.SchemaQualify = true;
        scripter.Options.ToFileOnly = true;
        scripter.Options.FileName = strFileName;
        scripter.Options.DriAll = true;
        scripter.Options.AppendToFile = true;
        scripter.Script(list.ToArray());//Table Script completed
        //Store Procedures scripting Writing
        list = new List<SqlSmoObject>();
        DataTable dataTable = dbUltimateSurvey.EnumObjects(DatabaseObjectTypes.StoredProcedure);
        foreach (DataRow row in dataTable.Rows)
        {
            string sSchema = (string)row["Schema"];
            if (sSchema == "sys" || sSchema == "INFORMATION_SCHEMA")
                continue;
            StoredProcedure sp = (StoredProcedure)databaseServer.GetSmoObject(
               new Urn((string)row["Urn"]));
            if (!sp.IsSystemObject)
                if (sp.Name.Contains("custom_"))
                    list.Add(sp);
        }
        scripter.Server = databaseServer;
        scripter.Options.IncludeHeaders = true;
        scripter.Options.SchemaQualify = true;
        scripter.Options.ToFileOnly = true;
        scripter.Options.FileName = strFileName;
        scripter.Options.DriAll = true;
        scripter.Options.AppendToFile = true;
        scripter.Script(list.ToArray());   // Stored procedure Script completed
    }
like image 191
Nag Avatar answered Oct 14 '22 21:10

Nag


;WITH ROUTINES AS (
    -- CANNOT use INFORMATION_SCHEMA.ROUTINES because of 4000 character limit
    SELECT o.type_desc AS ROUTINE_TYPE
            ,o.[name] AS ROUTINE_NAME
            ,m.definition AS ROUTINE_DEFINITION
    FROM sys.sql_modules AS m
    INNER JOIN sys.objects AS o
        ON m.object_id = o.object_id
)
SELECT *
FROM ROUTINES
like image 29
Cade Roux Avatar answered Oct 14 '22 22:10

Cade Roux