Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to access entire local database from server name ,but getting only single system details.

What I have tried:

SQL
SELECT	@@SERVERNAME as ServerName,
		DB_NAME(dbid) as DBName, 
		hostname as MachineName ,
		COUNT(dbid) as NumberOfConnections,
		loginame as LoginName,
		ConnectionProperty('client_net_address') AS 'Client Net 			Address'
		
--FROM sys.sysprocesses with (nolock) 
FROM sys.sysprocesses
WHERE dbid > 0 
and len(hostname) > 0 
and DB_NAME(dbid)=; 
Group by DB_NAME(dbid),hostname,loginame
order by DBName
Posted
Updated 5-Dec-16 1:23am

1 solution

Is that connection your machine?

If so, is anyone else connected to the database at the time?

BTW, get rid of the and DB_NAME(dbid)=; line. Your example query won't execute in sql server the way you have it there.

EDIT ==============================

I found this with google (search phrase="sql server get all current connections").

SQL
DECLARE @SPWHO2 TABLE 
    ([SPID] VARCHAR(1000),
     [Status] VARCHAR(1000) NULL, 
     [Login] VARCHAR(1000) NULL, 
     [HostName] VARCHAR(1000) NULL, 
     [BlkBy] VARCHAR(1000) NULL, 
     [DBName] VARCHAR(1000) NULL, 
     [Command] VARCHAR(1000) NULL, 
     [CPUTime] VARCHAR(1000) NULL, 
     [DiskIO] VARCHAR(1000) NULL, 
     [LastBatch] VARCHAR(1000) NULL, 
     [ProgramName] VARCHAR(1000) NULL, 
     [SPID2] VARCHAR(1000) NULL, 
     [Request] VARCHAR(1000) NULL)
INSERT INTO @SPWHO2 
    EXEC sp_who2 'Active'
SELECT * FROM @SPWHO2


I'm the only person logged into my server right now, so it only shows one record, so I cannot determine if this will be what you need.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900