Refresh all views in a database

  • SQL_Hunt

    SSC-Dedicated

    Points: 33277

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

    Thanks.

  • Naidu PK

    SSCrazy

    Points: 2444

    Good one. Simple and Easy.

    Thanks,
    Naveen.
    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

    SSCoach

    Points: 18671

    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

    ...

  • Jeff Moden

    SSC Guru

    Points: 995155

    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.
    "If you think its expensive to hire a professional to do the job, wait until you hire an amateur."--Red Adair
    "Change is inevitable... change for the better is not."
    When you put the right degree of spin on it, the number 3|8 is also a glyph that describes the nature of a DBAs job. 😉

    Helpful Links:
    How to post code 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