Refresh all views in a database

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

    Thanks.

  • Good one. Simple and Easy.

    Thanks,
    Naveen.
    Every thought is a cause and every condition an effect

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

  • Just circling back to say thanks for this tip.

  • 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 + ''';'

    FROM INFORMATION_SCHEMA.tables ISV

    WHERE ISV.TABLE_TYPE = 'VIEW'

    SELECT @sqlcmd

    ...

  • 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".

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

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

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