03 January 2011

Refresh Views in SQL database

This script will refresh views in a database, also handle the errors and will give names of views which cannot be refreshed.

declare c cursor for
select name from sysobjects where type = 'v' and uid = 1
open c
declare @ViewName varchar(500)
fetch next from c into @ViewName
while @@fetch_status = 0
begin
 BEGIN TRY
  exec sp_refreshView @viewName
 END TRY
 BEGIN CATCH
  print @viewName
 END CATCH
 fetch next from c into @viewName
end
close c
deallocate c

No comments: