October 6, 2005 at 11:29 am
I am trying to find the list of tables that make up any particular view in SQL server 2000. The information schema - VIEW_TABLE_USAGE would be the best tool, if only the sysdepends table worked reliably! When I use the schema I get some but not all of my views.
Has anyone got a solution, prefably one without cursors, that can identitfy the source tables for a view?
I have used the following SQL, but unfortunately it gives too many results:
SELECT VIEWS.name AS VIEW_NAME,
TABLES.name AS TABLE_NAME,
VIEW_SQL.text
FROM sysobjects VIEWS
INNER JOIN
syscomments VIEW_SQL
ON VIEWS.id = VIEW_SQL.id
INNER JOIN
sysobjects TABLES
ON VIEW_SQL.text LIKE '%.' + TABLES.name + '%'
WHERE (VIEWS.xtype = 'V')
AND (TABLES.xtype = 'U')
ORDER BY VIEWS.name, TABLES.name
Justin
October 6, 2005 at 12:35 pm
The problem with this approach is that if you have the tables Users and UsersPermissions and that the view uses the table UsersPermissions, then the like will catch a hit for both UsersPermissions and Users. This is something really tricky to avoid (never actually made it work 100%) without advanced string manipulation that the server doesn't provide.
October 7, 2005 at 5:45 am
I've tried to get the catalog information from "the other side". Looking for tables in syscomments and then joining to views.
select
v.name as View_Name,
t.name as Table_Name,
c.text as View_Definition
from sysobjects t
inner join syscomments c
on c.text like '%.' + t.name + '%'
inner join sysobjects v
on v.id = c.id
and v.xtype = 'V'
where t.xtype = 'U'
order by v.name, t.name
Of curse the View_Name and the View_Definition repeats for each used Table_Name.
October 7, 2005 at 6:54 am
if you alter a view to make it use it's same definition, wouldn't sysdepends get updated with the correcct dependancies?
i would think that simply altering all views would 'refresh' the view and you could use you original plan.
Lowell
October 7, 2005 at 8:21 am
In theory it would work... but you could lose object to object dependences. But if you're only after the table use then you're alright.
October 7, 2005 at 9:24 am
Remi,
by object to object dependencies, do you mean for example if i had a view or stored proc that was dependant on another view? I would think you would be right...you'd end up having to refresh all the objects in dependency order right?...whew..that's annoying...
if you don't know stored proc x has a dependency on a view, and recreate them out of order of dependencies...but you are trying to find the dependencies in the first place...oww my head hurts.
Lowell
October 7, 2005 at 9:28 am
Precisely . I had my head hurt a long time over that one. I found out it was less trouble trying to find matches manually once the autamated part couldn't sort everything out by itself. But it can get you a long long way even if it's not all the way.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply