views:

196

answers:

7

I once needed the lines of the stored procedures, to be able to trace whether i have a reference to some function, procedure or table, or sometimes to try to find something inside of the sp's code. Where does the sql server stores the procedures's code?

A: 

If you are just trying to view the stored procedures code you go into the progammabiltity folder within your DB and they should be all stored in there under stored procedures.

mysql_quest
Not an easy way to search the contents though.
Charles Boyung
A: 

If you use SQL Server Management Studion, you can right click on the database you want, then click "Tasks -> Generate Scripts".

There you can generate a script with all the SP's in one single file, separated files, or directly to a query window, and search/change what you want.

Hope this helps.

(this is for SQL Server 2008, but i think 2005 has this functionality too)

EDIT:

You can also see one single SP code, by following this path "YourDB -> Programmability -> Stored Procedures", then right click on the SP you want to see, and click "Modify", and a query window is opened with the code.

Tom S.
+5  A: 

It stored it inside the system schema tables:

SELECT * FROM INFORMATION_SCHEMA.ROUTINES

See MSDN about the INFORMATION_SCHEMA.ROUTINES view.

For a content search on this, you can do the follwing:

SELECT * FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%search_string%'
Oded
in INFORMATION_SCHEMA.ROUTINES ROUTINE_DEFINITION is only nvarchar(4000) so try view the text of a long procedure and you will see it is truncated. See my answer for how to get the complete code of long procedures.
KM
+1  A: 

If you are trying to search for references to other objects, then you can run a query like this:

SELECT * FROM syscomments WHERE TEXT LIKE '%searchstring%'

This will return any objects in the database that reference the search string. You can then look at these objects to see what stored procedures (and views and functions) are doing so.

Charles Boyung
syscomments.text is nvarchar(4000) and as a result will truncate anything that is longer. See my answer for a system view that uses a nvarchar(max) column and as a result, will not truncate
KM
it doesn't actually truncate, it just creates multiple rows for any procedures that get too long. It is definitely harder to use if you have stored procedures that are that long, and the view you made reference to is definitely a better solution. Of course, if you are creating individual procedures that are 4000+ characters long, maybe you deserve to be forced to do things that are more difficult :)
Charles Boyung
A: 

View Dependencies

In SQL Server Management Studio, right-click on a table, and choose "View Dependencies". You will see every object that references the table

INFORMATION_SCHEMA

The actual code for a stored proc, view, constraint, etc is stored in SysComments. You should query this using the views provided in the schema Information_Schema. Here are all the components of the Information_Schema.

Raj More
+4  A: 

Use sys.sql_modules because definition is nvarchar(max) because it will not truncate long code.

In INFORMATION_SCHEMA.ROUTINES the ROUTINE_DEFINITION column is only nvarchar(4000) so if you try view the text of a long procedure and you will see that it is truncated.

Use this to search for text in any procedure, view, function:

SELECT DISTINCT
    o.name AS Object_Name,o.type_desc
    FROM sys.sql_modules        m 
        INNER JOIN sys.objects  o ON m.object_id=o.object_id
    WHERE m.definition Like '%'+@Search+'%'
    ORDER BY 2,1

use this to view the text of a given procedure, view, function:

select * from sys.sql_modules where object_id=object_id('YourProcedure')
KM
sys.sql_modules seems to be nearly unknown. it is the true sql05+ solution, rather than infoschema and syscomments
devio
@devio, it is in BOL: http://msdn.microsoft.com/en-us/library/ms175081.aspx, so I'm not sure why no one knows about it. If you want the full definition, then it is `THE` solution.
KM
A: 

You can use

"select object_definition(object_id(routine_name)) from information_schema.routines"

or

"select object_definition(object_id) from sys.objects where name = 'foo'"

or even

"select object_definition(object_id('foo'));" -- 'foo' is the table name

These versions are never truncated.

t-bone