Refresh all views in a database

  • SQL_Hunt


    Points: 33453

    Comments posted to this topic are about the item Refresh all views in a database


  • Naidu PK


    Points: 2448

    Good one. Simple and Easy.

    Every thought is a cause and every condition an effect

  • akljfhnlaflkj

    SSC Guru

    Points: 76202

    I didn't know you could do this, thanks.

  • akljfhnlaflkj

    SSC Guru

    Points: 76202

    Just circling back to say thanks for this tip.

  • HappyGeek


    Points: 18684

    I seem to recall this script was plagiarised recently, which seems to have been sorted, but I recall a very pertinent question on that thread about 'what if the schema was different'. Without wishing to take anything from the original Author or Authors, perhaps this may work (there may be better solutions):

    DECLARE @sqlcmd NVARCHAR(MAX) = ''

    SELECT @sqlcmd = @sqlcmd + 'EXEC sp_refreshview ''' + TABLE_SCHEMA + '.'+ TABLE_NAME + ''';'



    SELECT @sqlcmd


  • Jeff Moden

    SSC Guru

    Points: 997178

    Heh... not only different schema but the quite different problem of a view that uses the WITH SCHEMABINDING directive.  You might also want to think about NOT rebuilding Indexed Views and the very real problems associated with views in general if they actually do need to be refreshed even in the face of column additions.  There are exceptions, of course, but generally not.

    --Jeff Moden

    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply