• I was having the same issue with DBs on a SQL Server 2005 instance - but the DBs had been imported from an older instance and set to Compatibility 80 - SQL 2000.

    Adding the DB name to the DB_ID funtion does not help.

    I can Select DB_ID() to get the ID number, and the code works if I use the actual number in place of the function.

    Thanks for the code, I've been wanting to check this for a while!