I recently saw that when I issue
select DB_NAME(0)
there is some answer, and when I check sys.databases there is no DB ID 0, so I think there should be a NULL return. If I select DB_NAME(1000) and there is no db id 1000 the return is NULL.
I noticed that the database I'm currently using is always displayed.
Is this normal? is this a bug or something?
I'm not 100% but shouldnt you use:
If i'm way off sorry about that.
I do not think that any database would have an id of zero, or a negative number. According to the official Microsoft Books Online documentation for db_name():
Arguments: database_id Is the identification number (ID) of the database to be returned. database_id is int, with no default. If no ID is specified, the current database name is returned.
So, I would say that the behavior sort of interesting, but that it is undefined and should not be relied on for production code.
I've always used db_name(), with no parameters, to find the name of the current database.