User access log to SQL Server - sql-server

I need to get a log of user access to our SQL Server so I can track average and peak concurrency usage. Is there a hidden table or something I'm missing that has this information for me? To my knowledge the application I'm looking at does not track this at the application level.
I'm currently working on SQL Server 2000, but will moving to SQL Server 2005 shortly, so solutions for both are greatly appreciated.

In SQL Server 2005, go to tree view on the left and select Server (name of the actual server) > Management > Activity Monitor. Hope this helps.

on 2000 you can use sp_who2 or the dbo.sysprocesses system table
on 2005 take a look at the sys.dm_exec_sessions DMV
Below is an example
SELECT COUNT(*) AS StatusCount,CASE status
WHEN 'Running' THEN 'Running - Currently running one or more requests'
WHEN 'Sleeping ' THEN 'Sleeping - Currently running no requests'
ELSE 'Dormant – Session is in prelogin state' END status
FROM sys.dm_exec_sessions
GROUP BY status

Related

Debezium: No maximum LSN recorded in the database; please ensure that the SQL Server Agent is running

This question is related to: Debezium How do I correctly register the SqlServer connector with Kafka Connect - connection refused
In Windows 10, I have Debezium running on an instance of Microsoft SQL Server that is outside of a Docker container. I am getting the following warning every 390 milliseconds:
No maximum LSN recorded in the database; please ensure that the SQL
Server Agent is running
[io.debezium.connector.sqlserver.SqlServerStreamingChangeEventSource]
I checked Debezium's code on Github and the only place that I can find this warning states in the code comments that this warning should only be thrown if the Agent is not running. I have confirmed that the SQL Server Agent is running.
Why is this warning showing up and how do I fix it?
Note:
My current solution appears to only work in a non-production environment - per Docker's documentation.
LSN is the "pieces" of information related about your SQL Server changes. If you don't have LSN, is possible that your CDC is not running or not configured properly. Debezium consumes LSNs to replicate so, your SQL Server need to generate this.
Some approaches:
Did you checked if your table are with CDC enabled? This will list your tables with CDC enabled:
SELECT s.name AS Schema_Name, tb.name AS Table_Name
, tb.object_id, tb.type, tb.type_desc, tb.is_tracked_by_cdc
FROM sys.tables tb
INNER JOIN sys.schemas s on s.schema_id = tb.schema_id
WHERE tb.is_tracked_by_cdc = 1
Your CDC database are enabled and runnig? (see here)
Check if enabled:
SELECT *
FROM sys.change_tracking_databases
WHERE database_id=DB_ID('MyDatabase')
And check if is running:
EXECUTE sys.sp_cdc_enable_db;
GO
Your CDC service are running on SQL Server? See in docs
EXEC sys.sp_cdc_start_job;
GO
On enabling table in CDC, I had some issues with rolename. For my case, configuring at null solved my problem (more details here)
EXEC sys.sp_cdc_enable_table
#source_schema=N'dbo',
#source_name=N'AD6010',
#capture_instance=N'ZZZZ_AD6010',
#role_name = NULL,
#filegroup_name=N'CDC_DATA',
#supports_net_changes=1
GO
Adding more to William's answer.
For the case SQL Server Agent is not running
You can enable it by following :
Control panel >
Administrative Tools >
Click "Services"
Look for SQL Server Agent
Right click and Start
Now you can fire cdc job queries in your mssql.
PS: you need to have login access to windows server.
Another possibility of this error (I just ran into this warning myself this morning trying to bring a new DB online) is the SQL login does not have the permissions needed. Debezium runs the following SQL. Check that the SQL login you are using has access to run this stored procedure and it returns the tables you have set up in CDC. If you get an error or zero rows returned, work with your DBA to get the appropriate permissions set up.
EXEC sys.sp_cdc_help_change_data_capture

SQL Server access and usage log

ANY IDEA ?
I have Sql Server Enterprise 64 need to find:
The last time the SQL Server DB have been accessed (time)
Detailed information (the inventory) of all applications and/or users accessing the DB.
Explanation: I am doing an inventory of all my databases (around 120) which would help me decide whether to keep the DB and that will help me a decision about: If the DB should be kept or consolidate it with another one.
I have been working with thise Scripts below but they are limited
1 not working on 2000-05. I need more details like program name, statut, hostname, userinfo ...
2-Need last used info
May be both needed to be combined and refined.
Script 1
WITH last_query_by_db (dbid, Last_query)
AS (select dbid, max(last_execution_time) 'Last_query'
from sys.dm_exec_query_stats
cross apply
sys.dm_exec_sql_text(plan_handle)
group by
dbid
)
select d.name, Last_query
from
sys.databases d
left outer join
last_query_by_db q on q.dbid = d.database_id
where d.name not in ('master','msdb','model','tempdb')
order by 1
Script 2
SELECT HOSTNAME, PROGRAM_NAME, STATUS, SPID
FROM MASTER..SYSPROCESSES
WHERE DBID= DB_ID('TestDB')
AND SPID != ##SPID
You can enable Login Auditing in SQL Server Management Studio. After that, you can see the logs like this:
In Object Explorer, expand a server, expand Management, and then expand SQL Server Logs.
Right-click a log and click View SQL Server Log.
Source: MSDN
To check for the application name, use SELECT APP_NAME(). (Source)
Also, there is an Activity Monitor in SQL Server that can provide valuable information:
Implement an audit session (enterprise ed only) or xevents session (which version are you on?) or logon trigger.
Here's how to implement a logon trigger:
http://technet.microsoft.com/en-us/library/bb326598.aspx

SQL Server Management Studio Express not showing recently added data

I recently installed and have been using Microsoft SQL Server Management Studio Express 9.00.4035.00 to manage a remote database on my hosts' SQL Server 2005.
Until now I had been using EMS SQL Manager 2011 Lite and that was working fine.
2 new rows where inserted into one of my tables this morning; one by a customer signing up for a service and the other as a test signup by me.
When I run a typical select query:
[Select top 20 * From tblNotary Order By ID Desc]
I don't see the the 2 most recent rows. But when I run the same query from EMS SQL Manager Lite I see the records.
I also verified when connecting using MS Access 2010 I see the 2 new rows in the table. I have checked and double-checked the connection settings and they match EMS.
Is there a setting or something obvious I am missing? Why can't I see the most recent record insertions? I am on a Windows 7 desktop machine.
The most likely reason is you are connecting to a different database than you expected. You can select ##servername to verify both queries are running against the same server.
If the records are stil being inserted as part of an open transaction and have not been committed, they are called "phantom" rows. You will not see phantom rows if your query runs at transaction isolation level read committed or higher. It may be that EMS SQL Manager Lite is running at read uncommitted, in which case it will include phantom rows in the query.
Sometimes connection strings are a tricky thing. Run this to double check:
select ##servername servername, name, crdate
from sys.sysdatabases
where name = db_name()
if you have an issue running that, just do a simpler version:
select ##servername, db_name(), ##version
Select top 20 * From tblNotary Order By ID Desc

Is it possible to log the text of each query executed on SQL Server?

We have this recurring situation where several times a week our application stops responding. What I would like to do is be able to view the text of the query running on SQL Server.
I can use sp_who to see the open connections, but, it does not display the actual query text.
If I can see the query that is freezing my database I can have a starting point for optimization.
This happened a few minutes ago and our sys admin had to reboot the box. This rebooting is not sustainable.
What steps should I take?
I would like to see the actual text of the queries that are running on my server.
SQL Server 2000
use this while the block is happening:
SELECT
r.session_id AS spid
,r.cpu_time,r.reads,r.writes,r.logical_reads
,r.blocking_session_id AS BlockingSPID
,LEFT(OBJECT_NAME(st.objectid, st.dbid),50) AS ShortObjectName
,LEFT(DB_NAME(r.database_id),50) AS DatabaseName
,s.program_name
,s.login_name
,OBJECT_NAME(st.objectid, st.dbid) AS ObjectName
,SUBSTRING(st.text, (r.statement_start_offset/2)+1,( (CASE r.statement_end_offset
WHEN -1 THEN DATALENGTH(st.text)
ELSE r.statement_end_offset
END - r.statement_start_offset
)/2
) + 1
) AS SQLText
FROM sys.dm_exec_requests r
JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id
CROSS APPLY sys.dm_exec_sql_text (sql_handle) st
WHERE r.session_id!=##SPID
this will list all active SPIDs, who is blocking them and the SQL of each SPID
EDIT
this query is for SQL Server 2005+, initial question did not state SQL Server 2000
See the article How to monitor blocking in SQL Server 2005 and in SQL Server 2000 for the definition of sp_blocker_pss08 (a SQL Server 2000 compatible script).

On SQL Server 2008, how to find out when was any database offline/online?

I have to include one report in my application showing offline/online activity of few databases on SQL Server 2008.
Could you please suggest how can I collect teh same information from sql server?
SELECT DATABASEPROPERTYEX('YOURDATABASE', 'Status')
DatabaseStatus_DATABASEPROPERTYEX
GO
SELECT state_desc DatabaseStatus_sysDatabase
FROM sys.databases
WHERE name = 'YOURDATABASE'
GO
This will tell you the status of the database.
In order to find out when your database was taken OFFLINE, you can use the SQL that I posted before, or the easiest way is to check the Event Viewer and it will tell you when the Database was taken OFFLINE. I have just tested this on my local machine and SQL Server writes out an Information message to the Application log.
You can also use below query to check database status.
SELECT Name, state_desc FROM sys.databases

Resources