List of all different database tables used in views - sql-server

I need to find all the different database tables used in views.
I tried information_schema.view_table_usage.
I got results for current database.
But tables associated with different database are not fetching.
Please guide.

You can use the sys tables sys.objects and sys.sysdepends to retrieve the information you as follows
--Replace 'YOU VIEW NAME' by the view you want
--1) using old sys tables
select distinct(ov.name),ov.xtype from sysobjects o
inner join sysdepends d on o.id=d.id
inner join sysobjects ov on d.depid=ov.id
where o.name='YOUR VIEW NAME'
--2) using new sys.table
select distinct(ov.name),ov.type from sys.objects o
inner join sys.sysdepends d on o.object_id=d.id
inner join sys.objects ov on d.depid=ov.object_id
where o.name='YOUR VIEW NAME'

Don't use information_schema (here's why) or sysdepends (here's why).
SELECT v.name,
ed.referenced_database_name,
ed.referenced_schema_name,
ed.referenced_entity_name
FROM sys.views AS v
INNER JOIN sys.sql_expression_dependencies AS ed
ON v.[object_id] = ed.referencing_id;
If you only want things in another database, add:
WHERE ed.referenced_database_name IS NOT NULL;
You have to run this query from the context of the database where the view exists.
As an aside, permissions are a little different. From the documentation:
Requires VIEW DEFINITION permission on the database and SELECT permission on sys.sql_expression_dependencies for the database.
If you don't have these permissions (explicitly, or though db_owner/sysadmin or a few other roles), you won't get an error message, the view will just return no rows.
INFORMATION_SCHEMA.VIEW_COLUMN_USAGE, on the other hand, has a little more freedom, but still has the limitation you observed (only returns references within the same database):
Returns one row for each column in the current database that is used in a view definition. This information schema view returns information about the objects to which the current user has permissions.
In order to use sys.sql_expression_dependencies, you'll need someone to grant you the appropriate permissions, or they'll need to create a stored procedure that calls it, executes as owner, and give you permissions to call that stored procedure.

Related

How to find all tables depends to stored procedure

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.

How to Get list of views with underlying Tables along with their owner information

I would like to get the list of all views in a database with their underlying tables. The underlying tables can be from other databases. So, I would like to know the database names of those tables as well. So, can anyone please help?
I tried below things :
SELECT *
FROM [INFORMATION_SCHEMA].[VIEW_TABLE_USAGE]
But, it is not showing any database information of the tables.
I would also like know the owner information of those tables and views present in the database. So, can anyone please help?
Information on dependencies are available via the dependency functions and views
sys.sql_expression_dependencies
sys.dm_sql_referenced_entities
sys.dm_sql_referencing_entities
here's an example using the sys.sql_expression_dependencies:
SELECT DISTINCT
s.name AS view_schema_name,
v.name AS view_name,
dp.name AS [owner],
d.referenced_entity_name,
d.referenced_database_name,
d.referenced_server_name
FROM
sys.sql_expression_dependencies d
INNER JOIN
sys.views v ON d.referencing_id = v.object_id
INNER JOIN
sys.schemas s ON v.schema_id = s.schema_id
INNER JOIN
sys.database_principals dp ON s.principal_id = dp.principal_id
This query gets the tables for the views ([VIEW_CATALOG] is the db):
SELECT [VIEW_CATALOG] AS [database]
, *
FROM [INFORMATION_SCHEMA].[VIEW_TABLE_USAGE] AS [VIEW_TABLE_USAGE]
LEFT JOIN [sys].[tables] AS [tables]
ON [tables].[name] = [VIEW_TABLE_USAGE].[TABLE_NAME]
AND object_schema_name([tables].[object_id]) = [VIEW_TABLE_USAGE].[TABLE_SCHEMA];
This topic tells you how to get the owner information: List table names, owner, schema and columns in SQL server database

How to check a SQL Server view dependencies

I need to modify a SQL Server view in a large database and need to know what other objects in this database use this view. How can I do that?
I tried viewing dependencies in SQL Server Management Studio but that does not list the objects that depend on this view (I know of at least one stored procedure that depends on this view, and it is not listed as a dependency).
The other option that I considered is scripting the whole database and doing the text search in it, but I want to see if there is an automated way to do this.
Try to query syscomments table and find the usage.
select o.name,o.type from sys.syscomments c
inner join sys.objects o
on o. object_id = c.id
where c.text like '%your view name%'
Update #1
select o.name,o.type from sys.sql_modules m
inner join sys.objects o
on o.object_id = m.object_id
where m.definition like '%your view name%'

Finding a referenced table in multiple views using SQL

I have a huge database with 50+ Views, Tables and Stored Procedures. I want to run a search to find I specific piece of text, i.e a table name, to see if it is being referenced anywhere.
I originally tried a C# route but I am suspecting this will be easier in SQL. The logic I am thinking about is possibly creating a query that loops through all Tables, Views and Stored Procedures and returns the data if it is available.
Any ideas?
In the below query, instead of the matchingstring replace your tablename, it will returns list of objects related to the search string
SELECT DISTINCT SO.[name]
FROM sysobjects SO
JOIN syscomments SC ON SC.Id = SO.Id
WHERE SC.[text] LIKE '%matchingstring%'
SELECT object_name(id) FROM sys.syscomments WHERE text LIKE'%yourtablename%'

Using SQL Server - is there a way to query 'Create view' SQL

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'

Resources