System Views text in SQL Server 2005 - sql-server-2005

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.

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

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.

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 :)

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.

Related

How to get the actual script of a stored procedure in another server?

I use SQL Server 2017. I have linked a server\database to another my main database. So, if I execute the query below, it works and brings me the correct data:
use [myMainServer].[myMainDatabase] GO
...
Select * from [myOtherServer].[myOtherDatabase].dbo.[myTable]
What I want to do is to get the actual script of a stored procedure from the linked database. Assume I have the stored procedure: sp_GetNumbers, then I can receive its content if I execute the code below in the linked server itself:
SELECT OBJECT_DEFINITION(OBJECT_ID('sp_GetNumbers'))
However I couldn't manage to do it from my main database. I've tried below but does not work.
SELECT OBJECT_DEFINITION(OBJECT_ID('[myOtherServer].[myOtherDatabase].sp_GetNumbers'))
My question is: How can I get the script of a Stored Procedure in SQL server B ([myOtherServer].[myOtherDatabase]) by running a query in SQL server A ([myMainServer].[myMainDatabase])?
The object functions are context specific so that won't work, but you could use the system views. Something like this:
SELECT [definition]
from [myOtherServer].[myOtherDatabase].sys.sql_modules m
inner join [myOtherServer].[myOtherDatabase].sys.objects o
on m.[object_id] = o.[object_id]
where o.[name] = 'sp_GetNumbers'

Copying a stored procedure from one database to another

I have a central management database which collates some information and runs some dynamic SQL for various other tasks when a new database is restored into the environment. One of those tasks is going to be a bit complex to achieve through dynamic SQL so I had the idea of creating a master copy stored procedure in the central DB and copying that over to the new databases after they are restored.
I've seen a few examples of people trying to do that on here but I can't get anything to play ball.
Here's what i am trying to achieve conceptually, note that I'm trying to cater for potentially multiple stored procedures to be created in this way just for future proofing.
declare #sql nvarchar(max), #DatabaseName nvarchar(200)
set #DatabaseName = 'TargetDatabase'
set #sql =
(
SELECT definition + char(13) + 'GO'
FROM sys.sql_modules s
INNER JOIN sys.procedures p
ON [s].[object_id] = [p].[object_id] WHERE p.name LIKE '%mastercopy%'
)
exec #sql
Thanks
Instead of creating dynamic script you could use one script with all the procedures that you want to create (you can script all the procs you want using 2 click in SSMS), you then run this script manually in the context of the database where you want to create these procedures or by passing the file with this script to sqlcmd with -i and passing the correct database name with -d.
Here Use the sqlcmd Utility you can see the examples.

How to do bulk update of views?

My database has about 30 views, most of which have a reference to another database on this server (call it DB1).
Now, without going into the reasons why, I need to update all those views to DB2, also on the local server.
I would hate to have to do this manually on each view. Is there some SQL query I can run that will replace all occurrences of the string 'DB1' with 'DB2' in all my views?
navigate to the views folder, press F7 (Object Explorer Details) now from the right pane select all the views you want, right click-->script view as--> DROP and CREATE TO --> new query window. In there change DB1 to DB2 and voila
see image
You could query sys.sql_modules like this, which I'd use to generate a file via bcp (SSMS will truncate results) which can be run as another SQL script. This preserves permissions too.
I've used this technique before and it works.
SELECT
REPLACE (REPLACE (sm.definition, 'CREATE VIEW', 'ALTER VIEW'), 'DB1.', 'DB2.') + '
GO'
FROM
sys.sql_modules sm
JOIN
sys.objects o ON sm.object_id = o.object_id
WHERE
sm.definition LIKE '%DB1.%' AND o.type = 'V'

Where does SQL Server store the stored procedure code?

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 SQL Server store the procedure's code?
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 o.type_desc,o.name
use this to view the text of a given procedure, view, function:
select * from sys.sql_modules where object_id=object_id('YourProcedure')
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.
It stored it inside the system schema tables:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
See MSDN about the INFORMATION_SCHEMA.ROUTINES view:
ms-help://MS.SQLCC.v10/MS.SQLSVR.v10.en/s10de_6tsql/html/c75561b2-c9a1-48a1-9afa-a5896b6454cf.htm
For a content search on this, you can do the follwing:
SELECT * FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%search_string%'
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.
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.
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.
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.

SQL Server 2005 search views for certain database objects

Is there a way in SQL Server to list all the views within a database that join from a particular object?
ie: find all the views that join from the table myTable
You can use sys.sql_dependencies:
select object_name(object_id),*
from sys.sql_dependencies
where referenced_major_id = object_id('<tablename>');
This will list all objects that depend on your table, you can restrict this to views by joining against sys.views:
select v.*
from sys.sql_dependencies d
join sys.views v on d.object_id = v.object_id
where referenced_major_id = object_id('<tablename>');
You have to search code and you have two options only. See comments below why other methods are not reliable.
select
object_name(m.object_id), m.*
from
sys.sql_modules m
where
m.definition like N'%my_view_name%'
or use OBJECT_DEFINITION
syscomments and INFORMATION_SCHEMA have nvarchar(4000) columns. So if "myViewName" is used at position 3998, it won't be found. syscomments does have multiple lines but ROUTINES truncates.
In SQL Server 2000, the sys.depends was unreliable, which affected "View dependencies" in the menus. I don't know if it's better in SQL 2005+. Example: view uses table, table is dropped and recreated, has different objectid, dependency = broken.
In SQL Server 2005, you can use a combination of sys.sql_dependencies and brute force parsing of the object text (as in gbn's answer). For more info on SQL Server 2005 dependencies, see http://msdn.microsoft.com/en-us/library/ms345449(SQL.90).aspx
In SQL Server 2008, there are new dependency DMVs and catalog views that are a bit more trustworthy than previous methods (sys.dm_sql_referenced_entities / sys.dm_sql_referencing_entities / sys.sql_expression_dependencies), but it is still easy to break them.
See this article for some ideas to make this work better. Also see http://msdn.microsoft.com/en-us/library/bb677168.aspx
Here's some of the examples from the link provided by unknown (in case that site disappears before stackoverflow does)
For SQL Server 2005, right click on the table name and select "View Dependencies"
A couple of the SQL only methods mentioned for SQL Server:
SELECT routine_name, routine_type FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%Employee%'
EXEC sp_depends #objname = N'HumanResources.Employee' ;