January 26, 2022 at 11:47 pm
What actions aside from deleting and restoring from backup, would cause the value of DB_ID() for a database to change for a DB hosted on SQL Server 2016 - Build# 13.0.6300.2 ?
Kindest Regards,
Just say No to Facebook!January 27, 2022 at 1:12 am
I haven't tried it but, perhaps, Detaching/Attaching. I think that Offline/Online would still keep the same value.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 27, 2022 at 2:08 pm
Out of curiosity, did some tests
Offline/Online - No Change
Detach/Attach - No Change
Delete/Restore - No Change
However if you attach or restore a database with an id that is already in use then the server will allocate a new id.
Far away is close at hand in the images of elsewhere.
Anon.
January 27, 2022 at 2:21 pm
Out of curiosity, did some tests
Offline/Online - No Change Detach/Attach - No Change Delete/Restore - No Change
However if you attach or restore a database with an id that is already in use then the server will allocate a new id.
That's possible. If you still have the tests, try the same thing but create a small test database as the 2nd of the 3 steps and see.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 27, 2022 at 3:02 pm
Not sure what you are alluding too but I tried all three options without creating a new database and in each case the id was reused. Only the following two tests resulted in a new id, ie the id already exists.
1.
Create database A
Detach A
Create database B (gets same id as A)
Attach A, A gets new id
2.
Create database A
Backup A
Drop A
Create database B (gets same id as A)
Restore A, A gets new id
Far away is close at hand in the images of elsewhere.
Anon.
January 27, 2022 at 3:49 pm
Don't rely on the DB_ID() to reference a db. Assume it could change at any time.
If you'll notice, in its system tables, MS stores the db name rather than the id unless it is absolutely certain that the db_id would work correctly.
If you need a permanent number for a db name, you'll need to create your own.
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
January 27, 2022 at 8:18 pm
Not sure what you are alluding too but I tried all three options without creating a new database and in each case the id was reused. Only the following two tests resulted in a new id, ie the id already exists.
1.
Create database A Detach A Create database B (gets same id as A) Attach A, A gets new id
2.
Create database A Backup A Drop A Create database B (gets same id as A) Restore A, A gets new id
Yep... that's what I meant. Thanks, David.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 27, 2022 at 8:21 pm
Don't rely on the DB_ID() to reference a db. Assume it could change at any time.
If you'll notice, in its system tables, MS stores the db name rather than the id unless it is absolutely certain that the db_id would work correctly.
If you need a permanent number for a db name, you'll need to create your own.
You can rely on it for certain things. For example, if you want to run sys.dm_db_index_physical_stats in the "current" database, DB_ID() is the way to go.
I totally agree on the rest though.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply