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

后端 未结 9 2035
孤独总比滥情好
孤独总比滥情好 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

    You will have to hand code it, SQL Profiler reveals the following.

    SMSE executes quite a long string of queries when it generates the statement.

    The following query (or something along its lines) is used to extract the text:

    SELECT
    NULL AS [Text],
    ISNULL(smsp.definition, ssmsp.definition) AS [Definition]
    FROM
    sys.all_objects AS sp
    LEFT OUTER JOIN sys.sql_modules AS smsp ON smsp.object_id = sp.object_id
    LEFT OUTER JOIN sys.system_sql_modules AS ssmsp ON ssmsp.object_id = sp.object_id
    WHERE
    (sp.type = N'P' OR sp.type = N'RF' OR sp.type='PC')and(sp.name=N'#test___________________________________________________________________________________________________________________00003EE1' and SCHEMA_NAME(sp.schema_id)=N'dbo')
    

    It returns the pure CREATE which is then substituted with ALTER in code somewhere.

    The SET ANSI NULL stuff and the GO statements and dates are all prepended to this.

    Go with sp_helptext, its simpler ...

提交回复
热议问题