Simple way to programmatically get all stored procedures

后端 未结 12 2135
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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
    

提交回复
热议问题