SQL Server : DefaultJob on ServerRestart or Start - sql-server

I want to give our IT a hand regarding the creation of a DefaultJob, that executes himself every server restart or start (Windows SQL Server 2012)
There is a trace that always activates himself after the server restarts or starts (after a hard shutdown or updates).
Basically I want to execute the following query (deactivate the Trace with the ID 1) automatically over a DefaultJob (a Job in the SQL Server Agent):
exec sp_trace_setstatus 1,0
Before executing it, I want to check if the trace with the ID 1 is active, if yes, then execute the query, if not, do nothing.
Any help would be much appreciated.
If you guys have questions, just ask... :-)
Thanks a lot in advance.
Cheers!

Related

(SQL) Server Agent Job not stopping

I am using SQL Server 2016, and have some Jobs running in the SQL Server Agent. Today I found one of the job is taking too long (10hours!) to run and is still processing, so I try to stop that. I tried right-click and stop the job, it showed a success message. However, when I go to the Job Activity Monitor, it is showing that the job is still running! I also tried the following code:
USE [msdb]
GO
EXEC dbo.sp_stop_job N'Process Reserving MI (except problematic tables)'
GO
It also says the job stopped successfully. But again when I go to the Job Activity Monitor, it is showing that the job is still running!
Can any one please help?
At the end I have to ask the server team to reboot the server in order to solve this problem

SQL Server 2017 monitor and alert of stored procedure duration while it is still running

SQL Server 2017 CU6 Enterprise. Is there a way to monitor and alert on running stored procedures on specific database if more than - say two minutes - has passed - but, while it is still running. I know multiple resources exist to capture that after it was done - e.g. xEvents, QueryStore, .. - but I need to capture that while stored procedure is still running and then write to eventlog or sending email with powershell or else.
Thanks.
For this I would use Adam Mechanic's sp_WhoIsActive.
This well documented procedure is a go-to in the community to see what is running on your server. By default, it doesn't return sleeping SPIDs.
To capture the results, check out this part of the documentation.
You will also want to limit it to a specific database or host or whatever. For that, Check out deciding what not to see.
You can find all of the help by running sp_WhoIsActive #help = 1 or just reading the full docs.

Identify source of a SQL Server job running nightly

I am looking into performance issues with an app that uses SQL Server Express (Microsoft SQL Server 2014 (SP2) (KB3171021) - 12.0.5000.0 (X64)) for the back end database.
I'm logging slow running queries using Extended Events. Looking at these I have identified a job that runs every night a series of queries, some of which take quite an extended period. The longest single query takes 10 minutes or so. It seems to be some sort of reporting, gathering broad information about everything in the tables it is looking at.
This seems to be an automated process that is running nightly at exactly the same time and takes about the same length of time to execute each night (around 20 minutes).
It runs on a Windows account with Administrative privileges that connects to SQL using Windows authentication. There is other legit stuff we do know about also running on this account.
The client doesn't know what it is.
The app name logged is just ".Net SqlClient Data Provider" which I believe is a generic default.
This is different than the client application which identifies itself correctly. I have identified with the client everything else that is running against the database- there are a few other jobs that generate accounting emails for example.
I can't find anything in Windows Scheduled Tasks that corresponds and SQL Express obviously doesn't have SQL Server Agent- everything other SQL job I have identified is scheduled in Windows Scheduled Tasks.
I've tried turning login auditing on and I can see the login occurring but it gives even less information as to what it is, it doesn't even give me the client app name.
I have no reason to believe it is in any way malicious, it is likely something that was set up years ago and simply forgotten.
Any ideas for how I could pinpoint where exactly this is coming from?
You will need to do this while you think the mystery program is running.
Open a Remote Desktop windows session on the actual SQL server since you are pretty sure that is where it is running.
Have TaskMgr.exe ready. Go to the “Details” tab so that you can search by TaskManager PID (not SQL PID) . Sort by this TaskMgr PID.
Right Click on the Columns at the top of this Details tab.. Choose “Select Columns”
Add this column …
Command line
While you think the mystery program is running run the following sql.
select x.host_process_id as USE_ME_TaskMgr_PID_OnClient_PC
, x.host_name as PCNameFromClient
, x.program_name as ProgNmFromClient
, x.client_interface_name
, x.original_login_name
, x.login_name
, x.nt_domain
, x.nt_user_name
, x.*
FROM sys.dm_exec_sessions x
order by x.host_name DESC
, x.program_name DESC
GO
Look at the first column of this sql. Find your mystery program in the list. Take the first value. This is the Program PID that will be the PID on the TaskMgr PID. Then you will see the command line full execution directory path.
By using something like sp_who you can find the hostname for each user connecting. Maybe that can give you a hint on where to look?

Job run in sql sever agent insert all null data.

I have a store procedure which insert data into a table in sql. It work find in SQL however when i create a job in sql server management studio to execute this store procedure by
exec store procedure name
the job does run successfully but all the data insert into the table is null.
i have no idea how this happen, please help. Thanks
Part 1:
We need to know more information than what you are supplying.
Here is a list of things to start.
1 - Generate TSQL for the table.
2 - Send us a couple of records to insert.
3 - Sample TSQL code inside the job step.
4 - What account is SQL Agent running under?
5 - Is the job running in the correct database when executing the TSQL statement?
6 - Are there any errors in the job history.
In short, this task can be easily done.
A screen shot or two would be worth a 1000 words!
Part 2:
Please use the snippet tool in windows to grab a screen shot so that I can see what is wrong.
It is very difficult to diagnose things remotely via just words.
Here are some more things to check.
1 - Are you a local admin of the laptop or server?
2 - Make sure you open the SQL server configuration manager as an admin.
This can be accomplished by right clicking the icon and selecting run as an admin instead of a double click to launch.
Below is a sample image of my configuration manager.
Here is the status of my SQL Server agent and the account it is running under.

Netscaler MSSQL Mirror Monitor query not getting a response

I'm trying to setup a NetScaler (version 9.3) monitor to determine which SQL server my database is running on (principal / mirror). The query I'm using is:
select mirroring_role_desc from sys.database_mirroring where database_id = DB_ID('OperationsManager')
The query works fine running under the user credentials that I created. The problem is, whenever the query comes from the NetScaler it's not completing. In the SQL Profiler trace it's showing an abort / rollback and I have no idea why.
I've enabled literally every trace event, including all the errors, to try to see why it's not completing and I've gotten nowhere. There are no Reads and no RowCounts. The connection appears to be aborting, but not from any sort of error.
This is what profiler looks like when it runs from the NetScaler
This is what it looks like when I run the same query, as the same user, from SSMS.
I thought maybe the NS was closing the connection too early so I upped the response timeout from 2s to 30s. That didn't make any difference. I'm completely baffled.
Can you let us know the version of NetScaler code you are running, and paste in the monitor CLI command from the CLI?
Also - you need to add a semi colon at the end of your query.
Add a semi colon to the end of your SQL statement in the monitor string. Otherwise the statement is not executed.

Resources