How to view a stored function - SQL Server

被刻印的时光 ゝ 提交于 2019-11-29 00:59:12

问题


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 here work for me. There doesn't appear to be any database/table called sys.objects. I was able to query the information_table.routines table, but that does not contain the function that I am looking for. My function is located in:

DBName.dbo.functionName

How can I view the contents of this function?


回答1:


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'



回答2:


select definition 
from sys.sql_modules 
where object_name(object_id) like 'functionName'



回答3:


--ShowStoredProcedures
select p.[type]
      ,p.[name]
      ,c.[definition]
  from sys.objects p
  join sys.sql_modules c
    on p.object_id = c.object_id
 where p.[type] = 'P'
   --and c.[definition] like '%foo%'
ORDER BY p.[name]
___________

SELECT OBJECT_NAME(object_id) ProcedureName,
       definition
FROM sys.sql_modules
WHERE objectproperty(object_id,'IsProcedure') = 1
ORDER BY OBJECT_NAME(object_id)



回答4:


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.




回答5:


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'.




回答6:


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



来源:https://stackoverflow.com/questions/15521934/how-to-view-a-stored-function-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!