SQL Server/T-SQL Tutorial/System Tables Views/sys.databases
Query sys.databases
3> USE master
4> GO
Changed database context to "master".
1>
2> DECLARE @DBName nvarchar(128)
3> DECLARE @TBLName nvarchar(128)
4> SET @DBName = N"Northwind"
5> SET @TBLName = N"Shippers"
6>
7>
8>
9> SELECT name
10> FROM sys.databases
11> WHERE name = @DBName
12> GO
name
--------------------------------------------------------------------------------------------------------------------------------
(0 rows affected)
1> SELECT name, object_id
2> FROM sys.tables
3> WHERE name = @TBLName
4> GO
Msg 137, Level 15, State 2, Server J\SQLEXPRESS, Line 3
Must declare the scalar variable "@TBLName".
Query sys.databases for database name
10> USE master
11> GO
13> IF EXISTS(SELECT name FROM sys.databases
14> WHERE name = N"NewDB")
15> DROP DATABASE NewDB
16> GO
17>
18> CREATE DATABASE NewDB
19> GO
21> USE NewDB
22> GO
SELECT name, is_auto_close_on FROM sys.databases
3> USE master
4> GO
Changed database context to "master".
1>
2> SELECT name, is_auto_close_on
3> FROM sys.databases
4> GO
name is_auto_close_on
-------------------------------------------------------------------------------------------------------------------------------- ----------------
master 0
tempdb 0
model 0
msdb 0
ClubRoster 1
(5 rows affected)
1>