SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Views created per table


Views created per table

Author
Message
Dugi
Dugi
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2212 Visits: 3511
Comments posted to this topic are about the item Views created per table

============================================================
SELECT YOUR PROBLEM FROM SSC.com WHERE PROBLEM DESCRIPTION =
http://www.sqlservercentral.com/articles/Best+Practices/61537/
John H Marsh
John H Marsh
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1813 Visits: 1460
Hello,

Thanks for the script.

An interesting experiment, but unless there were hundreds of Views in a DB, I think I would prefer using a straight select e.g. Select * From INFORMATION_SCHEMA.VIEW_TABLE_USAGE Order By TABLE_NAME, VIEW_NAME

Regards,

John Marsh

www.sql.lu
SQL Server Luxembourg User Group
tony.sawyer
tony.sawyer
SSC Veteran
SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)SSC Veteran (247 reputation)

Group: General Forum Members
Points: 247 Visits: 146
Hi,

At first I thought the query was really good - it would need a tweak to make it a bit better where you have nested views - so something like this:


with Table_analysis (View_Name , level )
as (
select view_name, 0 as level
from information_schema.view_table_usage
where table_name = @Table_Name
union all
select vtu.view_name, level + 1 as level
from information_schema.view_table_usage vtu
inner join Table_analysis ta
on ta.View_Name = vtu.table_name
)
select * from Table_analysis
order by view_name
go



unfortunately the results of this don't match up with the Red Gate Dependency checker which found an additional 10 or so views for one of our tables within the same database. After manually checking the differences I found that the nested views should have been included in both lists but sadly the view_table_usage didn't contain all the information it should.

A bit disappointing really - this could have been a really good technique to get view information - instead we have to use Red Gate dependency checker to work out what views access a certain table when we are making DDL changes to tables

Sorry for sounding like a salesman of Red Gate - but using this view_table_usage data doesn't work for us.

Tony
janis.l.murphy
janis.l.murphy
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 127
Most of the time, we need to know not only the views that depend on a table, but any other object, as well. For that matter, we often want to know what depends on other types of objects, such as UDFs, stored procedures, and views. For this I have a handy script I'll share with you. We use various schemas in out databases, so this script enables you to use the qualified name of the object.


/*
Find all dependencies on objects

1. Set @SEARCH_TABLE_NAME = NULL to see all dependencies
2. Set @SEARCH_TABLE_NAME to name of object for which you need to know what other objects
depend on it.
3. Result is list of objects that depend on the object dependencies
*/
DECLARE @SEARCH_TABLE_NAME VARCHAR(200) = NULL
DECLARE @TABLE_NAME SYSNAME = PARSENAME(@SEARCH_TABLE_NAME,1)
DECLARE @TABLE_SCHEMA_NAME SYSNAME = PARSENAME(@SEARCH_TABLE_NAME,2)

IF @SEARCH_TABLE_NAME IS NOT NULL -- only display dependent object information
SELECT DISTINCT o.type_desc AS [DependentObjectType], os.name+'.'+OBJECT_NAME(d.object_id) [DependentObjectName]
FROM sys.sql_dependencies d
INNER JOIN sys.objects o
ON d.object_id = o.object_id
INNER JOIN sys.schemas os
ON o.schema_id = os.schema_id
INNER JOIN sys.objects do
ON d.referenced_major_id = do.object_id
INNER JOIN sys.schemas dos
ON do.schema_id = dos.schema_id
WHERE d.object_id != do.object_id
AND OBJECT_NAME(referenced_major_id) = @TABLE_NAME
AND (@TABLE_SCHEMA_NAME IS NULL OR dos.name = @TABLE_SCHEMA_NAME)
ORDER BY 2

ELSE -- Display both dependent object and referenced object information

SELECT DISTINCT o.type_desc AS [DependentObjectType], os.name+'.'+OBJECT_NAME(d.object_id) [DependentObjectName],
do.type_desc AS [ReferencedObjectType],dos.name+'.'+OBJECT_NAME(referenced_major_id) [ReferencedObjectName]
FROM sys.sql_dependencies d
INNER JOIN sys.objects o
ON d.object_id = o.object_id
INNER JOIN sys.schemas os
ON o.schema_id = os.schema_id
INNER JOIN sys.objects do
ON d.referenced_major_id = do.object_id
INNER JOIN sys.schemas dos
ON do.schema_id = dos.schema_id
WHERE d.object_id != do.object_id
AND (@TABLE_NAME IS NULL OR OBJECT_NAME(referenced_major_id) = @TABLE_NAME)
AND (@TABLE_SCHEMA_NAME IS NULL OR dos.name = @TABLE_SCHEMA_NAME)
ORDER BY 2,3



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search