SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Theophilus

Add to Technorati Favorites Add to Google
Browse by Tag : Replication (RSS)

Table, Column, Replication Dependencies

By David Benoit in Theophilus | 06-12-2008 10:59 AM | Categories: Filed under: ,
Rating: (not yet rated) Rate this |  Discuss | 2,675 Reads | 125 Reads in Last 30 Days |no comments

 I was asked yesterday by a co-worker how we can pull dependency information from the database for a given table / column. Having not really looked into this in great detail it took me a few minutes but I found the sys.sys_dependencies catalog view. (The many views that are provided in 2005 have been such an eye-opener into the DBMS!) I was able to come up with something pretty simple but my co-worker came across a post from Umachandar Jayachandran (http://blogs.msdn.com/sqltips/archive/2005/07/05/435882.aspx) that covered most of our needs. However, we still needed to gather replication related dependencies so, I modified it to inlude that as well. I realize that 2005 does a much better job of handling ddl changes to replicated objects but, it sure is nice to have a little view beforehand to see what you are going to be potentially affecting before making any changes. So, the script from Umachandar Jayachandran with my changes for replication information is below. Hope it is helpful to someone else out there as well.  

declare
@table varchar(100)
, @column varchar(100)

select @table = 'YourTable'
select @column = 'YourColumn';

with rel_objs (obj_name, type_name, type_desc, parent_object_id, parent_column_id)
as
(
select
d.name
, d.type
, d.type_desc
, d.parent_object_id
, d.parent_column_id
from sys.default_constraints as d -- defaults
union all
select
c.name
, c.type
, c.type_desc
, c.parent_object_id
, c.parent_column_id
from sys.check_constraints as c -- check constraints
union all
select
k.name
, k.type
, k.type_desc
, k.parent_object_id
, kc.column_id
from sys.key_constraints as k -- primary key and unique constraints
join sys.index_columns as kc
on kc.object_id = k.parent_object_id and kc.index_id = k.unique_index_id
union all
select
f.name
, f.type
, f.type_desc
, f.parent_object_id
, fc.parent_column_id
from sys.foreign_keys as f -- foreign key constraints
join sys.foreign_key_columns as fc
on fc.constraint_object_id = f.object_id and fc.parent_object_id = f.parent_object_id
union all
select
c.name, 'CC'
, 'COMPUTED_COLUMN'
, d.referenced_major_id
, d.referenced_minor_id
from sys.sql_dependencies as d -- computed columns
join sys.columns as c
on c.object_id = d.object_id and c.column_id = d.column_id
where
d.object_id = d.referenced_major_id and d.referenced_minor_id > 0 and d.column_id > 0
union all
select
o.name
, o.type
, o.type_desc
, d.referenced_major_id
, d.referenced_minor_id
from sys.sql_dependencies as d -- views
join sys.objects as o
on o.object_id = d.object_id
where d.object_id <> d.referenced_major_id and d.referenced_minor_id > 0 and o.type = 'V'
union all
select
i.name
, 'IX'
, i.type_desc
, i.object_id
, ic.column_id
from sys.indexes as i -- indexes
join sys.index_columns as ic
on ic.index_id = i.index_id and ic.object_id = i.object_id
where i.is_primary_key = 0 and i.is_unique_constraint = 0
)
, rel_objs_det (schema_name, table_name, column_name, rel_obj_name, rel_type_name, rel_type_desc, column_replication_status, object_replication_agent)
as
(
select
s.name
, o.name
, c.name
, r.obj_name
, r.type_name
, r.type_desc
, case
when c.is_replicated = 1 OR c.is_merge_published = 1 OR c.is_dts_replicated =1 THEN 'Column Replicated'
else 'Not Replicated'
end as column_replication_status
, sp.name as object_replication_agent
from rel_objs as r -- names for the table/column to query for:
join sys.objects as o
on r.parent_object_id = o.object_id
join sys.columns as c
on c.object_id = o.object_id and c.column_id = r.parent_column_id
join sys.schemas as s
on s.schema_id = o.schema_id
left join sysarticles sa
on o.object_id = sa.objid
left join syspublications sp
on sa.pubid =  sp.pubid
)
select
r.schema_name
, r.table_name
, r.column_name
, case
when r.rel_obj_name like 'syncobj_%' then 'Replication View'
else r.rel_obj_name
end as rel_obj_name
, r.rel_type_name
, r.rel_type_desc
, r.column_replication_status
, r.object_replication_agent
from rel_objs_det as r
/* MODIFY or REMOVE where clause if you want run this query for other tables. */
where
r.table_name = @Table
-- and r.column_name like @Column;