• Thanks for the article.

    I tried it and got an error. The error was caused by one of the database is OFFLINE.

    Hence, I added the line 'and state_desc != 'OFFLINE' as below and no error occurs.

    ...

    FROM

    sys.databases

    WHERE

    database_id > 4

    and state_desc <> 'OFFLINE';