Find tables that a particular table depends on in SQL Server -


is there way tables table depends on? there must sql server management studio allows selecting find dependencies , option 'object [tablename] depends on'

i know sp_depends give me object depend on table not table depends on.

thanks in advance,

jon

based on discussion gbn , assumption care objects table depends on (rather depends on table), came contrived example:

use [master]; go  if db_id('foo') not null     drop database foo; go  create database foo; go  use foo; go  create type dbo.email varchar(320) not null; go  create schema foo authorization dbo; go  create type foo.email varchar(320) null; go  create function dbo.isgreaterthanzero1(@i int) returns bit begin     return (select case when @i>0 1 else 0 end); end go  create function dbo.isgreaterthanzero2(@i int) returns bit begin     return (select case when @i>0 1 else 0 end); end go  create table dbo.bar (     id int primary key ); go  create function dbo.maxbar() returns int begin     return (select max(id) dbo.bar); end go  create table dbo.foo (     id int foreign key references dbo.bar(id),          -- dependency on foreign key table     email1 dbo.email,          -- dependency on alias type     email2 foo.email,          -- dependency on alias type in different schema     ismorethanzero1 convert(bit, dbo.isgreaterthanzero1(id)),          -- computed column dependency     ismorethanzero1a dbo.isgreaterthanzero1(id),          -- computed column dependency     ismorethanzero2 bit check (dbo.isgreaterthanzero2(ismorethanzero2)=1),          -- check constraint dependency     ismorethanzero2a bit check (convert(bit,                  dbo.isgreaterthanzero2(ismorethanzero2a))=1),          check(ismorethanzero2a '[,%]'),         -- check constraint dependency     maxbar int not null default (dbo.maxbar())          -- default constraint dependency ); go  create trigger dbo.after_insert_foo on dbo.foo insert begin     set nocount on;     declare @x int;     select top (1) @x = id dbo.bar; end go 

okay, database chock full of stuff find, following script identify of object references above:

declare @tablename sysname = n'dbo.foo';  declare @object_id int = object_id(@tablename);  -- functions mentioned in check/default constraints -- , computed columns in @tablename  x  (     select [type], [obj], [count] = count(*)         (         select [type], obj = object_id(              substring(d, charindex('],', d) + 2,                  charindex('(', substring(d,                  charindex('],', d) + 2, len(d)))-1))                 (             select [type] = 'default', [object_id], d = [definition]             sys.default_constraints              parent_object_id = @object_id             , charindex('].[', [definition]) > 0              union             select 'check', [object_id], [definition]             sys.check_constraints             parent_object_id = @object_id             , charindex('].[', [definition]) > 0              union             select 'computed', null, [definition]             sys.computed_columns             [object_id] = @object_id              , charindex('].[', [definition]) > 0          ) x     ) y group [type], [obj]      union      -- triggers defined on @tablename     select 'trigger', obj = [object_id], 1         sys.triggers         parent_id = @object_id      union      -- objects referenced triggers on @tablename     select 'trigger references', [obj] = d.[referenced_major_id], count(*)         sys.sql_dependencies d         inner join sys.triggers tr         on d.[object_id] = tr.[object_id]         , tr.parent_id = @object_id         group d.referenced_major_id      union      -- foreign keys referenced @tablename     select 'foreign key', [obj] = referenced_object_id, count(*)         sys.foreign_keys         parent_object_id = @object_id         group referenced_object_id ) select      [obj] = quotename(object_schema_name(obj)) + '.'           + quotename(object_name(obj)),      [type],     [count] x union select     [obj],     [type],     [count] = count(*) (     select         [obj] = quotename(schema_name(t.[schema_id]))              + '.' + quotename(t.name),         [type] = 'alias type'             sys.types t         inner join sys.columns c         on t.user_type_id = c.user_type_id         t.is_user_defined = 1         , c.[object_id] = @object_id ) x group [obj], [type]; 

there more caveats here care mention. 1 definition parsing in sys.default_constraints, sys.check_constraints , sys.computed_columns assumes don't have constants amazingly object names (specifically parse ].[ show function name, since can't leave schema out , square brackets added you), function names don't include special characters "[", ".", or "]", or have arguments passed udf contain '[' or ']' because use determine in fact function (and assume there aren't nested functions). assumes references contained within same database. yet go 1 layer deep - if have trigger on dbo.foo calls function in turn references table, won't included. free going willing go far down rabbit hole. :-)

i still don't trust of dependencies views 100%, if system volatile safest bet follow gbn's advice , pursue brute force parsing using sys.sql_modules.definition parts of prone invalidation due schema changes. there many ways automating stuff can go wrong, don't know if you'll ever have 100% bullet-proof solution - though lot of work can pretty close.

but original question - maybe define explicitly types of dependencies you're looking for.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -