In theory
Below actions are only for testing purposes. Do not attempt any of this if you are not ok with your database instance becoming unusuable, unresponsive and/or your data being lost
Detaching the master
database to try and reuse it's database_id is not possible.
Detaching model
will result in the instance crashing and you not able to attach it back. This results in you having to Rebuild all your system databases. This process removes all information such as logins, jobs, .... And your user databases are detached.
Detaching Tempdb
to try and reuse it's database_id's by attaching it back under a different name resulted in the database_id 2 not being reused & tempdb
not working.
name database_id
master 1
model 3
msdb 4
test3_2__ 5
test3_2_ 6
tempdb 7
I was able to change the name back to tempdb, but the instance was pretty messed up, and I was unable to start it normally.
Could not find database ID 2. Database may not be activated yet or
may be in transition. Reissue the query once the database is
available.
---> Rebuild all your system databases.
But, in theory there are 2 (or more) options to change the msdb
system database id:
You can change the msdb
database id by detaching msdb
, creating a new user database and finally creating a new msdb
database while following the documented steps to create a new msdb database.
You can change the msdb
database id by detaching msdb
, reattaching it under a different name and changing the name back to msdb
.
UNSUPPORTED, please do not do this
First start sql server in 'master only recovery mode'
NET START MSSQL$InstanceName /f /T3608"
Detach msdb and create a new database
EXEC sp_detach_db msdb;
GO
CREATE DATABASE test3;
GO
Change the names of msdb files, Restart the instance normally
Initialize msdb:
SQLCMD -E -S<servername> -i"C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Install\instmsdb.sql" -o"C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\Install\instmsdb.out"£
View the database id's:
SELECT name,database_id
FROM sys.Databases;
name database_id
master 1
tempdb 2
model 3
test3 4
...
msdb 12
Unsupported method
The 2 normal attach procedures
sp_attach_db
and create database ... for attach
do not work when trying to attach msdb
.
Error message:
FILESTREAM database options cannot be set on system databases such as
'msdb
What you can do, solely for testing purposes is create a user database, msdb2
CREATE DATABASE msdb2
ON (FILENAME = N'X:\MSSQL\DATA\MSDBData.mdf'),
(FILENAME = N'X:\MSSQL\DATA\MSDBLog.ldf')
FOR ATTACH;
Rename the database to msdb
ALTER DATABASE msdb2 SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE msdb2 MODIFY NAME = msdb ;
GO
ALTER DATABASE msdb SET MULTI_USER
Restart the instance normally
NET STOP MSSQL$InstanceName
NET START MSSQL$InstanceName
and view the database_id's
:
SELECT name, database_id
FROM sys.databases;
Result
name database_id
master 1
tempdb 2
model 3
test 4
...
msdb 10
In practice
I have fortunately not seen this anywhere, but database_id 4 could in theory be reused by user databases. If this happened filtering out system databases in my queries would probably be the least of my problems.
That said, using WHERE DB_NAME(database_id) NOT IN ('master','tempdb','model','msdb','distribution','ssisdb')
is the sure choice.