How to view a stored function - SQL Server

后端 未结 7 2392
陌清茗
陌清茗 2020-12-28 17:03

Version: SQLServer 8

I would like to view the contents of a stored function in sqlserver, i.e. what exactly the function is doing.

None of the options listed

相关标签:
7条回答
  • 2020-12-28 17:29

    You can use sp_helptext command to view the definition. It simply does

    Displays the definition of a user-defined rule, default, unencrypted Transact-SQL stored procedure, user-defined Transact-SQL function, trigger, computed column, CHECK constraint, view, or system object such as a system stored procedure.

    E.g;

    EXEC sp_helptext 'StoredProcedureName'

    EDIT: If your databases or server are different then you can do it by specifying them as well

    EXEC [ServerName].[DatabaseName].dbo.sp_helptext 'storedProcedureName'
    
    0 讨论(0)
  • 2020-12-28 17:36
    select definition 
    from sys.sql_modules 
    where object_name(object_id) like 'functionName'
    
    0 讨论(0)
  • 2020-12-28 17:42

    Whether it is Stored Procedure OR Function OR any SQL object below script will give the full definition

    USE<Your Data base>
    SELECT OBJECT_DEFINITION (OBJECT_ID('<YOUR OBJECT NAME>')) AS ObjectDefinition 
    

    where OBJECT NAME could be your object name such as Stored Procedure / Function / Trigger ...etc name

    0 讨论(0)
  • 2020-12-28 17:50

    You can go to Object Explorer, choose the Database containing the Stored Procedure and then choose 'Script Stored Procedure As ':

    And then check in the destination folder you chose.

    0 讨论(0)
  • 2020-12-28 17:51

    Yes it is working fine.

    To view the stored procedures... SELECT * FROM sys.procedures;

    and get procduere name and use the below query for the same(I'm using SQuirreL SQL Client Version 3.2.0-RC1).

    EXEC sp_helptext 'StoredProcedureName'.

    0 讨论(0)
  • 2020-12-28 17:52

    I rather use INFORMATION_SCHEMA.ROUTINES:

    select ROUTINE_NAME, ROUTINE_DEFINITION, LAST_ALTERED 
    from INFORMATION_SCHEMA.ROUTINES where SPECIFIC_NAME = 'usp_mysp'
    

    Just copy the ROUTINE_DEFINITION column to a new window to see the full content.

    0 讨论(0)
提交回复
热议问题