Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to view a stored function - SQL Server

Tags:

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?

like image 743
etech Avatar asked Mar 20 '13 11:03

etech


2 Answers

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'
like image 125
Sachin Avatar answered Sep 29 '22 21:09

Sachin


select definition 
from sys.sql_modules 
where object_name(object_id) like 'functionName'
like image 28
Pritesh Avatar answered Sep 29 '22 23:09

Pritesh