E-Mail:
Get our new Windows 7 eBook (PDF) for $7 with 70+ Tips. Download Now!

Bug with db_name?

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

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.

thanks !! very helpful post!

What Do You Think?

 

Posted Recently

40 queries / 0.211 seconds.