SQL Server/T-SQL Tutorial/System Functions/db name
Check current database name by using DB_NAME()
5> USE Northwind;
6> DECLARE @db AS NVARCHAR(258);
7> SET @db = QUOTENAME(N"pubs");
8> EXEC(N"USE " + @db + ";");
9> SELECT DB_NAME();
10> GO
if db_name() <> "pubs"
if db_name() <> "pubs"
raiserror("Error in InstPubs.SQL, ""USE pubs"" failed!.",22,127) with log
GO