Simple way to programmatically get all stored procedures

后端 未结 12 2148
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 09:22

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 o

相关标签:
12条回答
  • 2020-12-24 09:42
    begin
    --select column_name from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='Products' 
    --Declare the Table variable 
    DECLARE @GeneratedStoredProcedures TABLE
    (
            Number INT IDENTITY(1,1), --Auto incrementing Identity column
            name VARCHAR(300) --The string value
    )
    
    --Decalre a variable to remember the position of the current delimiter
    DECLARE @CurrentDelimiterPositionVar INT 
    declare @sqlCode varchar(max)
    --Decalre a variable to remember the number of rows in the table
    DECLARE @Count INT
    
    --Populate the TABLE variable using some logic
    INSERT INTO @GeneratedStoredProcedures SELECT name FROM sys.procedures where name like 'procGen_%'
    
    --Initialize the looper variable
    SET @CurrentDelimiterPositionVar = 1
    
    --Determine the number of rows in the Table
    SELECT @Count=max(Number) from @GeneratedStoredProcedures
    
    --A variable to hold the currently selected value from the table
    DECLARE @CurrentValue varchar(300);
    
    --Loop through until all row processing is done
    WHILE @CurrentDelimiterPositionVar <= @Count
    BEGIN
        --Load current value from the Table
        SELECT @CurrentValue = name FROM @GeneratedStoredProcedures WHERE Number = @CurrentDelimiterPositionVar 
        --Process the current value
        --print @CurrentValue
        set @sqlCode = 'drop procedure ' + @CurrentValue
        print @sqlCode
        --exec (@sqlCode)
    
    
        --Increment loop counter
        SET @CurrentDelimiterPositionVar = @CurrentDelimiterPositionVar + 1;
    END
    
    end
    
    0 讨论(0)
  • 2020-12-24 09:45
    ;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
    
    0 讨论(0)
  • 2020-12-24 09:45

    If you open up a can of reflector on sqlmetal.exe (a stand-alone part of LINQ-to-SQL that generates code from a database), you can see the SQL statements it uses to get a list of all stored procedures and functions. The SQL is similar, but not identical, to the one in this answer.

    0 讨论(0)
  • 2020-12-24 09:47

    This is a SQL that I have just tested and used in MSSQL

    SELECT NAME from SYS.PROCEDURES 
    order by name
    

    In case that you need to look for a specific name or substring/text

    SELECT NAME from SYS.PROCEDURES 
    where name like '%<TEXT_TO_LOOK_FOR>%'
    order by name
    

    Replace with exactly that for example:

    SELECT NAME from SYS.PROCEDURES 
    where name like '%CUSTOMER%'
    order by name
    

    And calling

    EXEC sp_HelpText SPNAME 
    

    for each stored procedure, you'll get a record set with one line of text per row

    0 讨论(0)
  • 2020-12-24 09:50
    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
        }
    
    0 讨论(0)
  • 2020-12-24 09:58

    You can write C# code to run the following query on your database.

    Select * from sys.procedures
    
    0 讨论(0)
提交回复
热议问题