Hi, we need to output list of tables with dependencies, indexes ,FK, constrains I used TSQL and scripted info using ---run in msdb to test it--run with output to grid to avoid warning about tables with no indexSET NOCOUNT ONdeclare @tbl varchar(100), @schema varchar(100) , @schema_table varchar(200) if OBJECT_ID('tempdb..#tmp0')is not null drop table #tmp0 create table #tmp0( tablename varchar(100), constrains text, ObjectsThatDependOnTable text, AllIndexes text, DefaultConstraints text, HasReferenceToTables text, ReferedByTables text ) if OBJECT_ID('tempdb..#tmp4')is not null drop table #tmp4 create table #tmp4(index_name varchar(100),index_description varchar(100),index_keys varchar(100))declare C1 cursor forselect table_schema, table_name from INFORMATION_SCHEMA.TABLES open c1fetch c1 into @schema, @tblwhile @@FETCH_STATUS = 0begin set @schema_table = @schema +'.'+@tbl insert into #tmp4 exec sp_helpIndex @schema_table insert into #tmp0 (tablename, constrains,allIndexes, DefaultConstraints, ObjectsThatDependOnTable, HasReferenceToTables, ReferedByTables) select @tbl, ( select '['+ constraint_type + ' = ' + CONSTRAINT_NAME + ']' AS [text()] FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS where TABLE_NAME = @tbl and TABLE_SCHEMA = @schema and constraint_type !='CHECK' FOR XML PATH('') ), ( select '[Name = (' + index_name + ') Type = (' + index_description + ') keys = (' + index_keys + ')]' AS [text()] from #tmp4 FOR XML PATH('') ), ( select '[DFC NAME = ' +c.name + ';DFC COLUMN = ' + col.name + '; DFC VALUE = '+ definition + '] ' AS [text()] from sys.default_constraints c inner join sys.columns col on col.default_object_id = c.object_id inner join sys.objects o on o.object_id = c.parent_object_id inner join sys.schemas s on s.schema_id = o.schema_id where s.name = @schema and o.name = @tbl FOR XML PATH('') ), ( SELECT '[ '+ o.type_desc + ' '+ IsNull(object_schema_name(Referencing_ID),'*No Ref. to schema*')+'.'+ object_name(Referencing_ID) +']' AS [text()] FROM sys.sql_expression_dependencies INNER JOIN sys.objects AS o ON referencing_id = o.object_id WHERE referenced_id =object_id(@schema_table) for xml path('') ) , ( SELECT distinct '['+ object_name(referenced_object_id) + ']' AS [text()] FROM sys.foreign_keys WHERE parent_object_id = object_id(@schema_table) for xml path('') ) , ( SELECT distinct '['+ object_name(parent_object_id) + ']' AS [text()] FROM sys.foreign_keys WHERE referenced_object_id = object_id(@schema_table) for xml path('') ) truncate table #tmp4 fetch c1 into @schema, @tblendclose c1deallocate c1Question ... SQL server management studio provide with option to view object dependencies example SSMS->table->View dependencies I am assuming SMO and scripter object used to generate and present those values Please point me to direction how to get similar view of dependencies using Powershell so far my attempts to use Database.Script Method did not provide me any results https://technet.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.database.script(v=sql.105).aspx Thank you
↧