SQL: When working with ALTER DATABASE, don't forget CURRENT

I've been seeing quite a lot of unnecessary dynamic SQL code lately, that's related to ALTER DATABASE statements.  It was part of code that was being scripted.

Generally, the code looks something like this:

(I've used setting a db_compat level as an example)

Or if it's slightly more reliable code, it says this:

If you are going to be concatenating things like database or column names, make sure you use QUOTENAME to avoid issues with object names that aren't valid SQL Server object names when they aren't quoted.

But in so many cases, there's no need to have this code as dynamic at all. The ALTER DATABASE statement can take the word CURRENT to refer to the current database instead. So you could write just:



2 thoughts on “SQL: When working with ALTER DATABASE, don't forget CURRENT”

Leave a Reply

Your email address will not be published. Required fields are marked *