sql server - T-SQL: sp_depends with a cursor -
i have bunch of views in db dependencies listed for. currently, i'm using 'sp_depends' sproc this. speed process, i'm attempting use sp_depends sproc in cursor iterates on list of views. however, i'm not having luck , i've spent embarrassing amount of time trying "shotgun" fixit. below i've got far.
declare @viewnames table ( viewname varchar(255) ) insert @viewnames select name [amf_article].sys.views declare @tablecursor cursor, @viewname varchar(100); set @tablecursor = cursor select viewname @viewnames open @tablecursor fetch next @tablecursor @viewname while(@@fetch_status = 0) begin declare @sql varchar(max) set @sql = 'sp_depends ''[dbo].' + @viewname + '' print @sql exec @sql fetch next @tablecursor @viewname end
i think going on quoting combined exec call. can't single quotes match up, , when do, still tells me no. when run statement
sp_depends '[dbo].[v_amf_distinct_products]'
all well, in loop, not much.
any appreciated.
aside performance considerations, take @ changed version of script , see if works you:
declare @tablecursor cursor declare @viewname sysname; declare @schemaname sysname; set @tablecursor = cursor select ss.name schemaname, sv.name viewname sys.views sv join sys.schemas ss on ss.schema_id=sv.schema_id open @tablecursor fetch next @tablecursor @schemaname, @viewname while @@fetch_status = 0 begin declare @sql varchar(max) set @sql = 'sp_depends '''+quotename(@schemaname) + '.' + quotename(@viewname) + '''' print @sql exec(@sql) fetch next @tablecursor @schemaname, @viewname end close @tablecursor deallocate @tablecursor
updated 16-07-2016, sean
to exclude cursor logic, can use local variable assignment below:
declare @sql nvarchar(max)='' select @sql += 'sp_depends '''+quotename(ss.name) + '.' + quotename(sv.name) + ''';' sys.views sv join sys.schemas ss on ss.schema_id=sv.schema_id exec(@sql)
Comments
Post a Comment