I am looking for a way to identify stored procedures that refer to tables or views that are no longer available (i.e. that have been deleted), in order to help resolve these conflicts by editing the procedures. I would also like to be able to find and display the table or view names that do not exist. I am using SQL Server 2014.
You can use the following query to list missing dependencies:
select
object_name(referencing_id) as 'object making reference',
referenced_class_desc,
referenced_schema_name,
referenced_entity_name as 'object name referenced',
(select object_id from sys.objects where name = [referenced_entity_name]) as 'Object Found?'
from sys.sql_expression_dependencies e
left join sys.tables t
on e.referenced_entity_name = t.name
Source
Related
I want to find all tables used in stored procedures but it only gives me the tables that are in the database that I'm just using. Is there a solution to find all tables used in stored procedures, which are in other databases (in the same server)?
I try this:
SELECT DISTINCT p.name AS proc_name, t.name AS table_name
FROM sys.sql_dependencies d
INNER JOIN sys.procedures p ON p.object_id = d.object_id
INNER JOIN sys.tables t ON t.object_id = d.referenced_major_id
WHERE p.name like '%sp_example%'
ORDER BY proc_name, table_name
The procedures I need to analyze contain tables from different databases, but the code above gives me only results from one database.
First of all, sys.sql_dependencies is deprecated. Instead, you should use sys.sql_expression_dependencies. In it, you will find 4-part names of referenced objects (this includes objects referenced via linked server, for example).
Second, any object_id in SQL Server only makes sense within its database. If you are looking for anything outside of your current DB, don't join tables by these identifiers - use object names instead.
As part of a script, in which execution is halted and changes are rolled back if there are any errors, I use the following statements to drop an xml schema collection if it exists
IF EXISTS (SELECT * FROM sys.xml_schema_collections WHERE name = 'MyXMLSchemaCollection')
BEGIN
PRINT 'Drop MyXMLSchemaCollection';
DROP XML SCHEMA COLLECTION MyXMLSchemaCollection;
END
GO
This works nicely except for when the schema collection is in use, the result of which is an error like the following:
Specified collection 'MyXMLSchemaCollection' cannot be dropped because it is used by object 'MyObject'.
How would I go about only dropping the schema collection if it is not used by any objects in the database? The xml schema collection does not need to be dropped if it's being used.
Using the example XML Schema Collection from MSDN documentation ALTER XML SCHEMA COLLECTION (Transact-SQL) I found that you can determine if an XML Schema Collection is being referenced by a table using:
select distinct t.name from sys.tables t
left join sys.columns c ON c.object_id = t.object_id
left join sys.xml_schema_collections x ON x.xml_collection_id = c.xml_collection_id
where x.name = 'MyXMLSchemaCollection'
Also for stored procedures and I imagine other object types sys.sql_expression_dependencies table will be of use to you.
select
*
from sys.sql_expression_dependencies sed
left join sys.objects o ON o.object_id = sed.referencing_id
where referenced_entity_name = 'MyXMLSchemaCollection'
Have a look at these questions for more information on exploring object catalogs:
List of Stored Procedure from Table
Find all references to an object in an SQL Server database
I want to know if a certain table dbo.person is being used in any of the database views in my database. There are a lot of views in this database.
I could right click each view "Script View as --> CREATE To" to see the SQL that was used to build this view but this will take a long time.
I am wondering if there is any way to query all of these "create view as" scripts to see if any of them mention my table dbo.person.
I hope this is clear.
You can query the system catalog for views (and use sql_modules to get the view definition) then use the system view sys.sql_expression_dependencies to find which of these views reference dbo.Person:
SELECT ViewName = QUOTENAME(OBJECT_SCHEMA_NAME(v.[object_id])) + '.' + QUOTENAME(v.Name),
m.[Definition]
FROM sys.views AS v
INNER JOIN sys.sql_modules AS m
ON m.[object_id] = v.[object_id]
WHERE EXISTS
( SELECT 1
FROM sys.sql_expression_dependencies AS d
WHERE d.Referenced_id = OBJECT_ID(N'dbo.Person', 'U')
AND v.[object_id] = d.referencing_id
)
ORDER BY ViewName;
There are some small issues with sys.sql_expression_dependencies, but I would still be more inclined to use this than to search for '%person%', since this could bring back 10s, or 100s of extra results, any time a table that contains person (e.g. dbo.PersonAddress) is referenced, or person is used as an alias (SELECT Forename + surname AS Person) etc. It really depends whether you are looking for something that is going to be accurate most of the time, but may, on occasion miss a reference, or whether you need a catch all solution that will bring back extra results.
You could try
select * from INFORMATION_SCHEMA.VIEWS
where VIEW_DEFINITION like '%Person%'
You might need to change the WHERE to dbo.person or [Person]
For a more detailed string search on SQL Server database object definitions please refer to SQL Object Where Used List script
You can simply call like
exec SearchInObjectsText 'name'
and it will return procedures, functions, views, etc where the text "name" is used
I know this doesn't answer the question directly, but it answers the need. I use a third party add-in called SQLSearch from redgate. It's free and I'm not an employee, so this isn't a "plug" of any kind.
I normally use this SQL to look in definitions. I didn't filter on VIEWS just in case it is used somewhere else.
SELECT
so.name, so.type, sm.Definition
FROM
sys.objects AS so INNER JOIN sys.sql_modules AS sm ON so.object_id = sm.object_id
WHERE
sm.Definition LIKE N'%dbo.person%'
ORDER BY so.name
Please query Information_Schema.View_Table system views
select * from INFORMATION_SCHEMA.VIEW_TABLE_USAGE where TABLE_NAME = 'Emp'
Try this
SELECT DISTINCT m.definition, 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 '%person%'
and type_desc = 'VIEW'
Is there any way to know if a particular table is being referenced by any Views or not.I used the below code which gives only SP's and function names:
select * from sys.objects p inner join sys.sql_modules m
on p.object_id = m.object_id
where m.definition like '%abc%'
Please help!!!
select *
from INFORMATION_SCHEMA.VIEWS
where VIEW_DEFINITION like '%abc%'
First, your query gives views in the result set (I tried it on AdentureWorks2012 -> Production.Product table):
If you're using SQL Server 2008 or above, you can use the sys.sql_expression_dependencies catalog view. For example:
SELECT
referencing_object_name = o.name,
referencing_object_type_desc = o.type_desc,
referenced_object_name = referenced_entity_name,
referenced_object_type_desc = o1.type_desc
FROM sys.sql_expression_dependencies sed
INNER JOIN sys.objects o
ON sed.referencing_id = o.[object_id]
LEFT OUTER JOIN sys.objects o1
ON sed.referenced_id = o1.[object_id]
WHERE referenced_entity_name = 'YourTable'
It will give you nice look on each by-name dependency on a user-defined entity
For column level dependencies you can use the sys.dm_sql_referenced_entities function
Hope this helps
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
If that doesn't fit your bill - you could also check out the sysdepends catalog view in SQL Server - it lists what objects depend on what (see details in the MSDN docs).
To find out what objects depend on a given table, you could use something like:
SELECT
id,
OBJECT_NAME(ID)
FROM sys.sysdepends
WHERE depid = OBJECT_ID('YourTable')
That should give you a list of all objects depending on that table (or view or whatever you're checking)
I need to change a column's data type but I do not every location in the database that might be referencing it. Is there a script or application that can search all views, stored procedures, functions, table computed columns, etc. within a database and show what is referencing the column? I am using SQL Server 2005 Standard.
Thanks,
Aaron
You can always inspect the sys.columns catalog view:
SELECT
c.NAME 'Col Name',
OBJECT_NAME(c.OBJECT_ID) 'Table Name',
t.name
FROM
sys.columns c
INNER JOIN
sys.types t ON c.system_type_id = t.system_type_id
WHERE
c.Name = 'your-column-name-here'
and based on that information, you can generate the ALTER statements for a database:
SELECT
'ALTER TABLE dbo.' + OBJECT_NAME(c.OBJECT_ID) +
' ALTER COLUMN ' + c.NAME ' NewDataType NULL'
FROM
sys.columns c
WHERE
c.Name = 'your-column-name-here'
This query generates a set of ALTER TABLE .... statements which you can then copy to a SSMS query window and execute.
Word of warning: if any of the columns are being referenced - in a foreign key relationship, or if there's a default or check constraint on them - this approach might fail. In that case, you'd need to do some extra steps for those columns (like drop the constraints first etc.)
Update: this searches for the columns as defined in tables.
If you need to search into stored procedures, view and functions as well, I would strongly recommend using Red-Gate's excellent and free (!!) SQL Search utility - excellent stuff!
I like using a free search add-in tool from redgate software. I'm amazed at how useful it is - you can find all references to text quickly with it.
This description is from SQL Curry:
SQL Search finds fragments of SQL text within stored procedures, functions, views and more and once you find them, it quickly allows you to click and jump to the objects, wherever they happen to be on your servers. It’s pretty cool!
Here is the link: SQL Search
This query will help you find any table's column and the column it is referring to -
SELECT OBJECT_NAME(f.parent_object_id) AS [Table], COL_NAME(fc.parent_object_id,fc.parent_column_id) AS [Column],
OBJECT_NAME (f.referenced_object_id) AS RefTable, COL_NAME(fc.referenced_object_id,fc.referenced_column_id) AS RefColumn,
f.name AS FK
FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
WHERE OBJECT_NAME(f.parent_object_id) = '<your table name>'
AND COL_NAME(fc.parent_object_id,fc.parent_column_id) = '<your column name>'
I work for Red Gate and I see that SQL Search as already been mentioned. Glad that works for you. Another tool that can specifically list column dependencies is SQL Prompt 5, due to be released soon. You can download the EA build by visiting: http://www.surveymk.com/s.aspx?sm=zDJogAY5rwdIwOX/SqtTCQ%3d%3d and joining the early access list. I'd welcome you to try this out and let me know if it doesn't match your requirements. Another great feature it has is the ability to list your invalid objects. In other words, if you rename a column and you have a stored procedure that references the old column, it will draw your attention to this.