Tuesday 21 April 2009

cursor to fetch database views


   1:  declare @fetchStatus as int;
   2:  set @fetchStatus = 0
   3:  declare @str as varchar(100), @views as varchar(100)
   4:  declare tmp cursor for
   5:  select    [name] from    sys.views
   6:  open tmp
   7:   
   8:  while @fetchStatus = 0 begin   
   9:  fetch tmp into @views   
  10:  set @fetchStatus = @@fetch_status   
  11:  if @fetchStatus = 0 begin       
  12:  print @views       
  13:  set @str = 'select * from ' + @views + ' where 0 = 1'       
  14:  exec (@str)   
  15:  end
  16:  end
  17:   
  18:  close tmp
  19:  deallocate tmp

No comments: