tags:

views:

81

answers:

4

Is there any way to uniquely identify a database?

If we were to copy a database to another machine, this instance is assumed to be different. I checked on master tables, but could not identify any information that can identify this.

+3  A: 

You could make a table in it with a unique name, and simply do a query on that. It's a bit of a hack, sure, but it'd work...

Mark Mayo
+1. A beauty of a hack :o)
Ardman
If i copy a database, i am not sure if we can identify if the database has changed. Not sure if i am missing something, can you elaborate a bit more please?
that changes the wording inference. Do you mean for a way to recognise a db on another machine, or a way to make the db change when it is copied to another machine, or a way to confirm the db has somehow changed on the other machine?
Mark Mayo
+1  A: 

Create a scalar function that returns an ID/Version number:

create function fnGetThisDBID() returns varchar(32) as begin
    return ('v1.1,origin=server1')
end 

select 'version is: ' + dbo.fnGetThisDBID()
Alex K.
+2  A: 

You could put the information in an extended property associated with the database itself:

USE AdventureWorks2008R2;
GO
EXEC sys.sp_addextendedproperty 
@name = N'MS_DescriptionExample', 
@value = N'AdventureWorks2008R2 Sample OLTP Database';
GO

http://msdn.microsoft.com/en-us/library/ms190243.aspx

In your case, I would use something like this:

EXEC sys.sp_addextendedproperty 
@name = N'UniqueID', 
@value = N'10156435463';

select objname, [name], [value] 
from fn_listextendedproperty (null, null, null, null, null, null, null)
Jonathan
+3  A: 

service_broker_guid in sys.databases comes pretty close to what you ask. It is a uniqueidentfier generated when the database is created and is preserved as the database is moved around (detach and attach, backup and restored, server rename etc). It can be explicitly changed with ALTER DATABASE ... SET NEW_BROKER;.

Remus Rusanu
Will need exactly opposite of that i think. If a database is copied to a new server, then this has to be marked unique. If i combine server name and the id, this might make it unique.