Get number of connected users in SQL Server 2000 - sql-server-2000

While I'm trying to detach a database through Enterprise Manager, it shows the no. of users accessing the database and it didn't allow us to detach without clearing the database connection.
Well, I want to know whether the result (no. of users connecting a particular database) could be obtained through a SQL query? If yes, how ?
Happiness Always
BKR Sivaprakash

This will give you proper results. Add your database name in the query -
select spid, status, loginame,
hostname, blocked, db_name(dbid) as databasename, cmd
from master..sysprocesses
where db_name(dbid) like '%<database_name>%'
and spid > 50
This will include logins from SQL Agent. Note that the
same user can be using multiple connections from the same application,
and thus be counted more than once.

EXEC SP_WHO
or
EXEC SP_WHO2
maybe (think this might be SQL Server 2005 upwards):
SELECT COUNT(*) AS ConnectionCount,
CASE WHEN is_user_process =1 THEN 'UserProcess' ELSE 'System Process' END
FROM sys.dm_exec_sessions
GROUP BY is_user_process

Related

SQL Server 2012 audit report on database user login name

Trying to write an audit report for user permissions and access on a SQL Server 2012 database.
Using the following tables -
sys.database_role_members
sys.database_principals
sys.login_token
sys.database_permissions
sys.columns
sys.objects
Can get all the information required, except for the Login name associated with the database User.
Can anyone point me in the direction of where this information can be found?
Select ORIGINAL_LOGIN() as [Login]
The detailed info can be found here :
Microsoft ORIGINAL_LOGIN()
To add to your comment:
You can either restrict the user from accessing the database or not.
Same is applied to the server
SQL server does not provide you with the features to "Allow him/her to access the database, but if he/she does let me know"
In that case you would need to provide some custom functionality such as creating a job that executes the query :
select login_time,original_login_name,DB_NAME(database_id) from sys.dm_exec_sessions
where original_security_id <> 0x01
And depending on condition etc (User1 logged on DB1 at xx:xx:xx time) insert into table.
You should also take a look at Logon Triggers which could prevent users from logging under a certain conditions and then recording it Microsoft Logon Triggers if that is something you might want to consider

How to check Application Pool status remotely through SQL server

I am a SQL Database Admin and curious about is it possible to check Application pool site status remotely from DB server?
Is there any way (e.g. SSIS package or SQL script) that I can get the result in table or file?
Thanks in advance
Execute SQL query in SQL server management studio as below :
SELECT DB_NAME(dbid), COUNT(dbid) AS NoOfConnections, loginame
FROM sys.sysprocesses
WHERE dbid > 0
GROUP BY dbid, loginame

SQL Server Query : Host name

I have the exact Query Text and the exact time & date the Query was executed,
how can I find the Host Name that executed this query?
I'm using SQL Server 2008.
Do you need ##SERVERNAME
SELECT ##SERVERNAME
will return the server name where the query was executed.
HOST_NAME will return the workstation name
SELECT HOST_NAME()
There is not any table with historical information about the host that executed a query - http://www.sqlservercentral.com/Forums/Topic1334479-146-1.aspx
You can check with HostName() as
SELECT HOST_NAME() AS HostName, SUSER_NAME() LoggedInUser
http://blog.sqlauthority.com/2009/05/26/sql-server-find-hostname-and-current-logged-in-user-name/
or
SELECT #IP_Address = client_net_address
FROM sys.dm_exec_connections
WHERE Session_id = ##SPID;
How to get the client IP address from SQL Server 2008 itself?
How to identify the caller of a Stored Procedure from within the Sproc
This is as close as you'll get, I believe:
select host_name()
As is mentioned in the docs: "The client application provides the workstation name and can provide inaccurate data. Do not rely upon HOST_NAME as a security feature."

How do I see the NTusername in trigger?

SQL Server Profiler shows NtUsername.
How do I see the NTusername in trigger?
I don't understand. SQL Server Profiler shows the Windows username. SUSER_NAME, SUSER_SNAME ORIGINAL_LOGIN, USER, USER_NAME shows the username of the SQL user account. I want what the Profiler is showing. There must be a way if the profiler can do it.
You use SUSER_SNAME(). Not SUSER_NAME(). Note the extra S
SUSER_SNAME returns the connection user (SQL Server 2000+)
SUSER_NAME gives the value of sys.server_principals.principal_id (SQL Server 2005+)
SUSER_NAME will give NULL for a Windows user who connects via a Windows Group, because only the Windows Group has an entry in sys.server_principals
Notes:
you have SUSER_ID and SUSER_SID at the SQL Server Instance level which are the opposite
at the database level, you have USER_NAME and DATABASE_PRINCIPAL_ID (USER_ID is deprecated)
A SQL login won't have an ntusername value in Profiler, because it isn't available
Since I could not find how to get the NTUsername, I used HOST_NAME() which gave me the user's machine name.
SUSER_NAME, SUSER_SNAME, ORIGINAL_LOGIN, USER, USER_NAME all gave me the SQL account username.
There is no correlation between the NTUserName unless the login is a Windows Authentication login. But because suser_name() is not returning what you want, I'm assuming that you are using SQL Server Authentication.
In which case, I don't believe you can do this.
It apears that the protocol used in your case was NamedPipes.
if NTUserName is not blank and in LoginName you have SQL Login – then it is SQL Authentication Over Named Pipes
-- see http://sqlbg.wordpress.com/2011/05/28/how-to-check-what-sql-server-network-protocol-is-used-by-connections/

Add a group in SQL 2008 RC

I have a group called kwr-fs-dws-sqladm (Active Directory/Exchange) and there are two users in this group.
This group will go to all sql server and instances. I have access to windows server 2008 as admin.
How can I add that group in all 60 SQL Servers and 200 Instances?
i.e. kwr-fs-dws-sqladm - user as sysadmin -.
Use [Master]
CREATE LOGIN [DOMAIN\kwr-fs-fws-sqladm] FROM WINDOWS;
EXEC sp_addsrvrolemember 'DOMAIN\kwr-fs-fws-sqladm', 'sysadmin';
Execute on each server, you will want to check out the CREATE LOGIN syntax for extra options.
I've never managed that many SQL servers, i'd assume there is a way of executing on all. One way is to add each server to a server group in SSMS, then you can right click that group and create a new query which will execute on each server.