Bug with db_name?
- 2
- Add a Comment
The other day I was pulling data from the sysprocesses table on a remote SQL Server 2000 server. I was on a database server that has SQL Server 2005 Service Pack 2 on it and I have the destination (remote) SQL Server 2000 setup as a linked server on the SQL Server 2005 server. With a query window open on the SQL Server 2005 server I ran the following statment:
select db_name(dbid) as [database name],
hostname,
program_name,
nt_username,
loginame,
net_address,
last_batch
from SQL1.master.dbo.sysprocesses
The problem here is… the function db_name() was pulling the “local” database name for the remote dbid on the SQL Server 2000 server. I was unable to find documentation on this “issue” on BOL and the Internet. I found a workaround for this. Here is the work around:
select d.[name] as [database name],
p.hostname,
p.program_name,
p.nt_username,
p.loginame,
from SQL1.master.dbo.sysprocesses as p
inner join SQL1.master.dbo.sysdatabases as d
on p.dbid = d.dbid
This solution pulls the database name for the remote server and not the local database name on the SQL Server 2005 server. If you have experienced this issue or if this a known “bug” or not a “bug” please post and let us know!

2 Comments
Vinuraj
February 12th, 2009
at 5:49pm
Bug is there with ur code/ Logic
This is not wit the DB_Name Bug the function will make use of the local function where it points to the local DB, it the db id is available in local then it will bring that.
propecia
June 5th, 2009
at 3:29pm
thanks !! very helpful post!