Recently, I was reviewing a T-SQL script and saw a ALTER DATABASE statement with the CURRENT keyword. It seems redundant at first glance. After all, if you are already connected to a database, why would you want to refer to it as CURRENT?
In this post, I try to uncover the purpose, possible use cases and potential mistakes that one can make when using the CURRENT keyword.
The Purpose
First, let’s understand the purpose of the CURRENT statement. Put very simply, the CURRENT keyword in the ALTER DATABASE statement refers to the database that you are connected to. Instead of hard-coding the database name, you can use CURRENT to make your script more portable and dynamic.
-- Instead of this:
ALTER DATABASE [SQLTwins] SET COMPATIBILITY_LEVEL = 150;
-- You can write:
ALTER DATABASE CURRENT SET COMPATIBILITY_LEVEL = 150;
This is especially useful in deployment pipelines, maintenance scripts, or reusable procedures where the database context may vary.
Typical Use-cases
Here are some practical scenarios where CURRENT keyword is very useful:
- Automated Scripts: Avoid hard-coding database names when deploying changes across multiple databases.
- Cross-Environment Compatibility: Whether you’re running the script on dev, test, or prod,
CURRENTtargets the database that you are connected to. - Contained Databases: In Azure SQL Database or Managed Instances, where scripts run within a specific database context,
CURRENTensures the command targets the right scope.
CURRENT v/s DB_NAME()
You might wonder: why not dynamically fetch the database name using the very familiar DB_NAME() and build the statement?
DECLARE @dbName NVARCHAR(128) = DB_NAME();
EXEC('ALTER DATABASE [' + @dbName + '] SET COMPATIBILITY_LEVEL = 150');
While DB_NAME() works, it’s great for dynamic SQL. Dynamic SQL is often complicated to manage (esp. in deployment pipelines) and introduces potential quoting issues. CURRENT is cleaner, safer, and easier to read – more so for simple configuration changes.
Please consider
Before you start using CURRENT in your scripts, be on the lookout for the following common mistakes:
- Context Awareness: If your script runs in a multi-database environment, double-check that you’re in the right context before executing. Remember:
CURRENTrefers to the database you’re connected to, not necessarily the one you intend to modify. - Permissions:
CURRENTdoesn’t bypass permission checks and the scripts would fail if your user is not allowed to ALTER the database - Forgetting Test Coverage: Because
CURRENTabstracts the database name, it’s easy to overlook edge cases in testing—especially when deploying across environments. Always validate that your script behaves as expected in all environments & configurations that you intend to use. - Mixing with Dynamic SQL: Using
CURRENTwith dynamic SQL is unnecessary and can be confusing or redundant (unless it’s a code generator) - The CURRENT keyword is fully supported for Azure SQL Database and Azure SQL Managed Instances. However, for on-premise installations SQL Server 2012 (11.x) and above (Why are you on a lower version anyway?)
In conclusion
The CURRENT keyword may seem trivial. However, it plays a subtle role in making T-SQL scripts more portable. It also contributes to scripts being context-aware and cloud-friendly. Whether you’re managing databases in Azure or on-prem, using ALTER DATABASE CURRENT can streamline your operations. It also reduces hard-coded dependencies.
Further Reading
- ALTER DATABASE on MSDN: https://learn.microsoft.com/en-us/sql/t-sql/statements/alter-database-transact-sql
Until we meet next time,
Be courteous. Drive responsibly.

