title: T-SQL: sp_depends with a cursor
body: <p>I have a bunch of Views in my DB that I would like to get the dependencies listed for.  Currently, I'm using the 'sp_depends' sproc to do this.  To speed up the process, I'm attempting to use the sp_depends sproc in a cursor that iterates over the list of views.  However, I'm not having any luck and I've spent an embarrassing amount of time trying to "shotgun" fixit.  Below is what I've got thus far.</p>
  
  <pre><code>DECLARE @ViewNames TABLE
  (
       ViewName VARCHAR(255)
  )
  INSERT INTO @ViewNames
  select name  from [AMF_Article].sys.views
  
  declare @tableCursor cursor,
           @viewName varchar(100);
  set @tableCursor = cursor for select ViewName from @ViewNames
  
  open @tableCursor
  fetch next from @tableCursor into @viewName
  while(@@fetch_status = 0)
  begin
  
      declare @sql varchar(max)
      set @sql = 'sp_depends ''[dbo].' + @viewName + ''
      PRINT @sql
      exec @sql
  
   fetch next from @tableCursor into @viewName
  end
  </code></pre>
  
  <p>I think something is going on with the quoting combined with the EXEC call.  I can't get my single quotes to match up, and when I do, it still tells me no.  When I run the statement</p>
  
  <pre><code>sp_depends '[dbo].[V_AMF_Distinct_Products]'
  </code></pre>
  
  <p>all is well, but in a loop, not so much.</p>
  
  <p>Any help is greatly appreciated.</p>