How to view a stored function - SQL Server
Asked Answered
C

7

28

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?

Commodus answered 20/3, 2013 at 11:13 Comment(2)
Why dont you use Management Studio or you certainly want to check with query?Willettawillette
@Willettawillette I want to check with a query because I am on Linux and none of the solutions I've found allow me to view the function directly.Commodus
R
34

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'
Raindrop answered 20/3, 2013 at 11:15 Comment(3)
How can I run this when the current database is different from the target database? When I'm connected to the default database and run the above command, I get: Error code 15250, SQL state S0001: The database name component of the object qualifier must be the name of the current database. This works only when currently connected to DBName: EXEC sp_helptext 'DBName.dbo.functionName'Commodus
try this EXEC [ServerName].[DatabaseName].dbo.sp_HelpText 'storedProcNameRaindrop
That worked! I didn't need the server name part, though. EXEC [DatabaseName].dbo.sp_helptext '[functionName]'Commodus
D
12
select definition 
from sys.sql_modules 
where object_name(object_id) like 'functionName'
Diver answered 20/3, 2013 at 11:36 Comment(3)
you can use like '%routineName%' or = 'routineName'Stipitate
Note that this table does not store all types of functions. Per the doc (msdn.microsoft.com/en-us/library/ms175081.aspx), it includes sprocs, replication filter procedures, views, SQL DML triggers, scalar functions, TVFs, and rules. That excludes all CLR functions, CLR triggers, constraints, and extended stored procedures.Anchoress
Ah thank you so much! This worked for functions (Not just stored procedures) and totally saved me. <3Gaullism
G
2
--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)
Germany answered 21/2, 2014 at 22:23 Comment(1)
this is much better than the best answerNerveracking
M
1

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

Moiramoirai answered 13/8, 2014 at 11:43 Comment(0)
P
1

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.

Prose answered 13/2, 2015 at 22:53 Comment(0)
P
1

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

Potbelly answered 21/9, 2018 at 7:19 Comment(0)
W
1

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

enter image description here

And then check in the destination folder you chose.

Witchhunt answered 24/12, 2020 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.