views:

1413

answers:

4

Hello Friends

I am looking for viewing the text of the system views and procedures in SQL Server 2005 using the object explorer or using sp_helptext.

actually i am coming from the SQL Server 2000 background, where we have the feature of retreiving the code of the view and the stored procedure using SQL Server 2000 Enterprise manager directly, but still i am unable to find this feature in the SQL Server 2005 Management Studio and still looking for the same feature for getting the view and procedure text in SQL Server 2005.

Kindly please help me in this.

+1  A: 

Do you have access to SQL Server Management Studio? It is now sys.sp_helptext and can be browsed at master --> Programmability --> Stored Procedures --> System Stored Procedures in the object browser and executed with

exec sys.sp_helptext ObjectName

All the information you are looking for can be found in the syscomments table which stores the definitions of all views, rules, defaults, triggers, CHECK constraints, DEFAULT constraints, and stored procedures. The SQL definition statements are stored in the "text" column.

select text from syscomments  where id =
OBJECT_id('objectname')
order by colid
cmsjr
thank you for your comments, and again i am asking you the same question in case of views, please let me know how to get the view text in SQL Server 2005.here i am again looking for the text of the system views as present in the SQL Server 2005 Master database.
Everest
The same query will for with views, procs, functions, etc. e.g.select text from master..syscomments where id = object_id('information_schema.columns')
cmsjr
But Friend, it is like using the query analyzer and the sp_helptext command in SQL Server 2000, but how to achieve the same thing using the SQL Server 2005 Enterprise manager, as we can easily do that for views in SQL Server 2000 Enterprise manager, hope i am clear on this and thanks for the query
Everest
I see what you mean now, I just tried to right click and script a system view. Sorry I was not of more assistance. I'll share if I find anything further.
cmsjr
A: 

This Stackoverflow posting has a database reverse engineering script that (amongst other things) reverse engineers view definitions. From the script

-- This generates view definitions 
--
select definition + char(10) + 'go' + char(10)
  from sys.sql_modules c
  join sys.objects o
    on c.object_id = o.object_id
  join #views o2
    on o.object_id = o2.object_id

Note that #views is populated earlier in the script with a list of views to dump out. To select for a particular schema (also from the script)

select o.name
      ,o.object_id
  into #views
  from sys.objects o
  join sys.schemas s
    on s.schema_id = o.schema_id
 where o.type in  ('V')
   and s.name = @schema

To get stored procedures, substitute 'P'; to get functions substitute 'FN' or 'TF'

In the master database the definitions for the system stored procedures live in sys.system_views, 'sys.system_objects, 'sys.system_columns, and sys.system_sql_modules. The queries in the reverse engineering script could be adapted fairly readily to get out the definitions of the system items by using these tables.

ConcernedOfTunbridgeWells
Is the information in syscomments volatile in such a way that it is inadvisable to just retrieve the definition?
cmsjr
Probably not, but syscomments is a backwards compatilbility feature for SQL2000 and may or may not last indefinitely (in practice it will probably be around for a while yet). Also, the OP seems to after the definitions of the system sprocs and views, which live in a different set of tables.
ConcernedOfTunbridgeWells
A: 

If you just want to see the text that defines a procedure:

Right click the stored procedure, and choose Modify. This will show the SQL that defines the stored procedure.

Or: right click, Script Stored Procedure as, CREATE To, New Query Editor Window.

The other answers are more advanced but I thought maybe you were asking a simple question :)

Andomar
A: 

Here's how to list all the Dynamic Management Views:

SELECT * FROM sysobjects
WHERE name LIKE 'dm_%'
order by name

Unfortunately, if you run sp_helptext on these it doesn't give you much. For example,

exec sp_helptext   N'sys.dm_os_sys_info'

Returns:

CREATE VIEW sys.dm_os_sys_info AS
    SELECT *
    FROM OpenRowset(TABLE SYSINFO)

All the ones I tried gave the same result.

Mitch Wheat