Programmatically retrieve SQL Server stored procedure source that is identical to the source returned by the SQL Server Management Studio gui?

后端 未结 9 1921
孤独总比滥情好
孤独总比滥情好 2020-11-30 20:38

Any pointers on how I can programmatically get exactly the identical stored procedure source from SQL Server 2005, as when I right-click on that stored procedure in SQL Serv

相关标签:
9条回答
  • 2020-11-30 21:24

    I saw a article via link. There are four methods, I just did a short summary here for helping other programmers.

    1. EXEC sp_helptext 'sp_name';

    2. SELECT OBJECT_ID('sp_name')

    3. SELECT OBJECT_DEFINITION( OBJECT_ID('sp_name') ) AS [Definition];

    4. SELECT * FROM sys.sql_modules WHERE object_id = object_id('sp_name');

    0 讨论(0)
  • 2020-11-30 21:27

    I agree with Mark. I set the output to text mode and then sp_HelpText 'sproc'. I have this binded to Crtl-F1 to make it easy.

    0 讨论(0)
  • 2020-11-30 21:29

    To alter a stored procedure, here's the C# code:

    SqlConnection con = new SqlConnection("your connection string");
    con.Open();
    cmd.CommandType = System.Data.CommandType.Text;
    string sql = File.ReadAllText(YUOR_SP_SCRIPT_FILENAME);
    cmd.CommandText = sql;   
    cmd.Connection = con;
    cmd.ExecuteNonQuery();
    con.Close();
    

    Things to note:

    1. Make sure the USER in the connection string have the right to alter SP
    2. Remove all the GO,SET ANSI_NULLS XX,SET QUOTED_IDENTIFIER statements from the script file. (If you don't, the SqlCommand will throw an error).
    0 讨论(0)
提交回复
热议问题