How to check if SQL Server crashed - sql

I'm building a fully automated process for my company, which includes 2 processes. One, where a 3rd party application that off a stored procedure, at certain times per day. Two, the stored procedure then controls kicking off other processes. The procedure is controlled by a table with the list of jobs that will be kicked off for the day. If the status for the job item is set to Queue, the procedure will start running that item and set the status to Running. My problem is, if for some reason SQL Server crashes, whether it be a power outage or some odd reason. If the 3rd party application goes and kicks of that stored procedure another day, there might be a job that still says running which should've failed or set back to Queue since the server crashed.
Is there a way in SQL where I can check if the server crashed during the time a process is being ran?

you may put your script and 3rd party application in one SQL job on the SQL server. that may resolve the issue.

Related

When I start My SAP MMC EC6 server one service is not getting to wait mode

Can someone of you help me, how to make the following service selected in the image get into wait mode after starting the server.
Please let me know if developer trace is required to be posted for resolving this issue.
that particular process is a BATCH process, a process that runs scheduled background tasks (maintained by transaction SM36/SM37). If the process is busy right after starting the server, that means there were scheduled tasks with status released waiting for execution, and as soon as the server was up, it started those tasks.
If you want to make sure the system doesn't immediately start released background tasks, you'll have to set the status back to scheduled (which, thanks to a bit of weird translation, means they won't be executed because they are not released).
if you want to start the server without having a chance to first change the job status in SM37, you would either have to reset the status on database level (likely not officially supported by SAP) or first start the server without any BATCH processes (which would give you a number of great big warning messages upon login) and change the job status before then restarting the server with the BATCH processes. You can set the number of processes for each type in the profile of your instance (parameter rdisp/wp_no_btc).

Re-execute SQL Server job in case of failure

Currently I am creating a SQL Server job. My requirement is whenever the job fails, it needs to run one more time. Is it possible in SQL Server?
I'm not sure if this is the best way to do it, but you can configure every single step of a job to re-run a specific number of times after a specific number of minutes (in case of network troubles, for example). Open the step configuration in SQL Server Management Studio and set the "Retry attempts" and "Retry interval (minutes)" according to your preferences.
Of course, this will not work if you want to re-run the whole job from the beginning and it will not retry infinitely.

SQL Server LCK_M_S only happens in production

I have a stored procedure that is called by a SQL Server 2012 report that is taking an age to run in production compared to development because of a blocking session lck_m_s
The stored procedure runs instantaneously when executed in SQL Server Management Studio and also works well when called as part of the report from a dev laptop via Visual Studio.
When the report is uploaded to the production server this blocking issue appears.
How can I find out what is causing the lck_m_s issue when in production?
Execute this query when the problem happens again:
select * from
sys.dm_os_waiting_tasks t
inner join sys.dm_exec_connections c on c.session_id = t.blocking_session_id
cross apply sys.dm_exec_sql_text(c.most_recent_sql_handle) as h1
It will give you the spid of the session that caused blocking, on which resource was blocked, and text of the most rcent query for that session. This should give you a solid starting point.
You have a couple of options
Set up the blocked process report. Essentially, you set the blocked process threshold (s) system configuration to a non-zero number of seconds and set up an event notification on the BLOCKED_PROCESS_REPORT event. You'll get an XML report each time any process is blocked for more than the threshold you've set. The downside to this is that it'll be for anything getting blocked, not just your procedure, but you'll get whomever is holding the non-compatible lock in the report.
Set up an extended events session for your procedure to capture the lock_released event where the duration is longer than you care to wait. The upside is that this is extremely targeted and you can define the session so that you get very little noise. The downside is that you won't know what process is holding the incompatible lock (though you will get a pretty detailed description of what the locked resource is to further your investigation).

How to start a job or process when a job on another server finished?

I want to send a mail or automatically start a job on my server as soon as a job on another server has finished successfully. I have access to the other server and can view the job status but I cannot change the job itself, which is running an SSIS package.
Basically, I want to start refreshing my database (via running an ETL through job) as soon as source has stopped refreshing itself. I would love to have suggestion beside this windows service implementation.
I took the liberty of editing the question and title to make it more explicit. As I understand it from your description, you want to run a job (or start some other process) on server A when a job on server B has completed successfully. You cannot change the job definition on server B, but you can log on to it and view the job history.
If you can't change the job or anything else on server B, that means it cannot notify server A when the job is complete. Therefore, you need to query server B from server A, using a Windows service or possibly a simple script that runs every few minutes (or hours, or whatever is appropriate).
You can query the status of a job from .NET or PowerShell using the SMO Job class, or from TSQL using the sp_help_job procedure. Which of these is a better solution depends on how you want to implement your polling mechanism.

Continuously checking database from a Windows service

I am making a Windows service which needs to continuously check for database entries that can be added at any time to tell it to execute some code. It is looking to see if it's status is set to pending, and it's execute time entry is > than the current time. Is the only way to do this to just run select statements over and over? It might need to execute the code every minute which means I need to run the select statement every minute looking for entries in the database. I'm trying to avoid unneccesary cpu time because I'm probably going to end up paying for cpu cycles on the hosting provider
Be aware that Notification Services is only for SQL 2005, and has been dropped from SQL 2008.
Rather than polling the database for changes, I would recommend writing a CLR stored procedure that is called from a trigger, which is raised when an appropriate change occurs (e.g. insert or update). The CLR sproc alerts your service which then performs its work.
Sending the service alert via a TCP/IP or HTTP channel is a good choice since you can deploy your service anywhere, just by modifying some configuration parameter that is read by the sproc. It also makes it easy to test the service.
I would use an event driven model in your service. The service waits on an auto-reset event, starting a block of work when the event is raised. The sproc communications channel runs on another thread and sets the event on each incoming request.
Assuming the service is doing a block of work and a set of multiple pending requests are outstanding, this design ensures that those requests trigger just 1 more block of work when the current one is finished.
You can also have multiple workers waiting on the same event if overlapping processing is desired.
Note: for external network access the CREATE ASSEMBLY statement will require the PERMISSION_SET option to be set to EXTERNAL_ACCESS.
Given you talk about the service provider, I suspect one of the main alternatives will not be open to you, which is notification services. It allows you to register for data changed events and be notified, without the need to poll the database. It does however require service broker enabled for it to work, and that potentially could be a problem if it is hosted - some companies keep it switched off.
The question is not tagged to a specific database just SQL, the notification services is a SQL Server facility.
If you're using SQL Server and open to a different approach, check out SQL Server Notification Services.
Oracle also provides notifications, the call it Database Change Notification