How to check if SQL Server Agent is running - asp.net

I am using SQL Server 2008 r2. I want to find a way to verify if SQL Server Agent is running. I am suspicious that the Agent isn't running, but I don't know how to check.

In Management Studio, you can check if SQL Server Agent is running by looking at the SQL Server Agent node in Object Explorer. In the following screen shot, SQL Server Agent on my SQL Server 2012 instance is running (green arrow overlaid on the SQL Server Agent icon), but the agent for SQL Server 2000 is stopped (red x).
You can also check in Control Panel > Administrative Tools > Services:
Or in Program Files > Microsoft SQL Server > Configuration Tools > Configuration Manager:
Finally, you can check the state using T-SQL:
DECLARE #agent NVARCHAR(512);
SELECT #agent = COALESCE(N'SQLAgent$' + CONVERT(SYSNAME, SERVERPROPERTY('InstanceName')),
N'SQLServerAgent');
EXEC master.dbo.xp_servicecontrol 'QueryState', #agent;

The quickest, easiest, most directest way to determine if SQL Agent is running, and that can be done easily in SSMS, is a query (hence it can be automated), and isn't querying a deprecated system table (i.e. sysprocesses) or EXECing xp_servicecontrol, is a DMV that was introduced in SP1 for SQL Server 2008 R2:
sys.dm_server_services
SELECT dss.[status], dss.[status_desc]
FROM sys.dm_server_services dss
WHERE dss.[servicename] LIKE N'SQL Server Agent (%';
Returns:
status status_desc
4 Running
It just requires the VIEW SERVER STATE server permission, but you already needed that in order to see the status of it in Object Explorer (in SSMS).
AND, if you don't want to grant VIEW SERVER STATE to a particular Login because it allows for getting too much other info, then you technically don't need to grant anything at all, at least not to an actual user. See the following two resources for full details (including working examples):
What minimum permissions do I need to provide to a user so that it can check the status of SQL Server Agent Service? (similar question on DBA.StackExchange)
Safely and Easily Use High-Level Permissions Without Granting Them to Anyone: Server-level (blog post)

If the SQL Server Agent is running, a green play button will be shown in the bottom right corner of the SQL Server Agent icon within SQL Server Management Studio.
To validate the status of the SQL Server Agent for a given instance using T-SQL execute the following code snippet:
IF EXISTS ( SELECT 1
FROM master.dbo.sysprocesses
WHERE program_name = N'SQLAgent - Generic Refresher')
BEGIN
SELECT ##SERVERNAME AS 'InstanceName', 1 AS 'SQLServerAgentRunning'
END
ELSE
BEGIN
SELECT ##SERVERNAME AS 'InstanceName', 0 AS 'SQLServerAgentRunning'
END
Source = Colin Stasiuk

IF EXISTS (SELECT 1 FROM sysprocesses WHERE LEFT(program_name, 8) = 'SQLAgent')
PRINT 'Agent is running!'
ELSE
PRINT 'Agent is not connected!';
Let me know if this works else try this
IF EXISTS ( SELECT 1
FROM master.dbo.sysprocesses
WHERE program_name = N'SQLAgent - Generic Refresher')
BEGIN
SELECT ##SERVERNAME AS 'InstanceName', 1 AS 'SQLServerAgentRunning'
END
ELSE
BEGIN
SELECT ##SERVERNAME AS 'InstanceName', 0 AS 'SQLServerAgentRunning'
END
Reference : http://benchmarkitconsulting.com/colin-stasiuk/2009/07/20/check-sql-server-agent-status-on-all-sql-servers/

Automate checking if SQL Server Agent is running with this batch file.
It will start start the service if it isn't running already.
You will need to tweak the service name (SQLServerAgent) for some versions of MSSQL.
echo off
echo Test SQL Server Agent Service
for /F "tokens=3 delims=: " %%H in ('sc query "SQLServerAgent" ^| findstr "STATE"') do (
if /I "%%H" NEQ "RUNNING" (
echo service was stopped, starting service
rem put your optional errorlog or warning message here
net start "SQLServerAgent"
)
)

This is what I use in powershell
Get-Service | Where-Object -like -value '*sql*agent*' -Property 'name'

Updated in 2020 for Sql Server Developer Addition 2019
View --> Registered Servers --> Local Server Groups
This should list all local servers. If there is a Red X by the server, it's stopped. Or you can right click on it and go to Service Control. From there you can Start/Stop Restart, etc.

Related

SQL Job, Linked Server, Domain Account

I think I have tried everything but I can't figure out how to set up an SQL Job that have a T-SQL Step that performs a SELECT on a linked server.
1) I got a Domain user mydomain\SQLJob
2) I got a SQL 2017 server 'JOBSERVER' with the SQL Agent running log on as a domain user mydomain\sv_agent (this cannot be changed). No futher rights should be given to this user either.
3) On the JOBSERVER I created a linked server
EXEC sp_addlinkedserver 'LINKEDSERVER'
4) mydomain\SQLJob is data reader on a database on LINKEDSERVER
5) I am able to do a SELECT * FROM LINKEDSERVER... from JOBSERVER in a regular Query Window.
On JOBSERVER I have tried
ALTER DATABASE MyDatabase SET TRUSTWORTHY ON
And then set the job to execute in MyDatabase
I have also tried
Job Step Properties > Advanced > Run as user > mydomain\SQLJob
I have tried adding mydomain\SQLJob to the linked server on JOBSERVER both with and without Impersonate
Could someone let me know what the correct steps are ?
Thanks
I found another post that stated it could not be done.
What I did instead was changing the T-SQL to a PowerShell script. Here the Run As works just fine.

SQL works in R studio but not in R-Serivces

This code works perfectly in R-Studio but there is no way to make it work in MS Management studio. It keeps on saying that:
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'myserver\LOCAL01'.
That is not my user give it is a trusted connection. Can someone help me understand it?
ALTER PROCEDURE [dbo].[TESTIM] AS
BEGIN
SET LANGUAGE ENGLISH
CHECKPOINT 
DBCC DROPCLEANBUFFERS
EXEC sp_execute_external_script
#language = N'R'
, #script = N'
con <- "Server=myserver\\LOCAL;Database=mydb;Trusted_Connection=true";
sql <- RxInSqlServer(connectionString = con, shareDir = "c:\\TMP");
local <- RxLocalSeq(sql);
rxSetComputeContext(local)
ff <- RxSqlServerData(sqlQuery = "select top 1 * from mytable", connectionString = con);
t = rxImport(ff);
OutputDataSet <- data.frame(SUCCESS = TRUE);
'
WITH RESULT SETS (([SUCCESS] BIT))
END
So, when you execute sp_execute_external_script it executes under one of 20 Windows user accounts (worker accounts) that has been created during installation of SQL Server R Services. These accounts are created for the purpose of running tasks under a security token belonging to the SQL Server Trusted Launchpad service.
This works very well, but if you need to create a SQL connection inside your R script (as in your case) and you use trusted connection (Windows Authentication), you are executing under the user account mentioned above ('myserver\LOCAL01' in your case), and that account need to be given permission to log in to the SQL Server instance on your behalf.
To do this:
In SQL Server Management Studio, in Object Explorer, expand Security, right-click Logins, and select New Login.
In the Login - New dialog box, click Search.
Click Object Types and select Groups. Deselect everything else.
In Enter the object name to select, type SQLRUserGroup and click Check Names.
The name of the local group associated with the instance's Launchpad service should resolve to something like instancename\SQLRUserGroup. Click OK.
By default, the login is assigned to the public role and has permission to connect to the database engine.
Click OK.
That should do it (the above steps are copied from here.
If you want to read more about the user accounts you can have a look at my blog-post "Microsoft SQL Server R Services - Internals III".
Hope this helps!
Niels

Provider 'MICROSOFT.ACE.OLEDB.12.0' has been denied when executing stored procedure from ASPX page

I've been looking all around for a fix, but I cannot find it.
Basically I setup a stored procedure in sql server that reads an excel file into a table. I managed to make it work inside SQL Server Management Studio. But when I try to execute it inside a webpage I get the error:
Ad hoc access to OLE DB provider 'MICROSOFT.ACE.OLEDB.12.0' has been
denied. You must access this provider through a linked server.
Things I've tried include running the following commands:
sp_configure 'Show Advanced Options', 1;
RECONFIGURE;
GO
sp_configure 'Ad Hoc Distributed Queries', 1;
RECONFIGURE;
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'AllowInProcess' , 1
GO
EXEC master . dbo. sp_MSset_oledb_prop N'Microsoft.ACE.OLEDB.12.0' , N'DynamicParameters' , 1
GO
I've also tried giving access to the temp folder of SQL Server to my SQL user.
I've been running in circles for a while... here is the relevant SQL code that runs fine inside management studio:
set #q = 'INSERT INTO #RDV_BUFFER (noDossier,RDVDate,Facturation,debut,fin,debutreel,finreel,MinutesParUnites) SELECT nodos, daterdv, montfact, heuredebut, heurefin,heureassis,heureleve,horstep FROM OPENROWSET(''MICROSOFT.ACE.OLEDB.12.0'',''Excel 8.0; Database=C:\inetpub\wwwroot\ProDentaireElite\XLS_Root\' + #URL + '; HDR=YES; IMEX=1'', ''SELECT * FROM [' + #Sheetname + '$]'')'
EXEC sp_executesql #q
Thanks to all the gurus out there. Maybe your knowledge can help.
Ok I found it, there were 2 "Temp" folders I needed to give access to my SQL User. One located in the windows microsoft sql server folder, and the other, located in my own user. Make sure you set them up to ''Full control'' or it won't work.

Session management in ASP.NET with SQL Server 2005

I am getting the following error while executing InstallSqlSate.sql file
Msg 14261, Level 16, State 1, Procedure sp_add_category, Line 32
The specified #name ('[Uncategorized (Local)]') already exists.
SQLServerAgent is not currently running so it cannot be notified of
this action.
I want details about what is that error and how to fix it?
InstallState.sql is not intended to be run directly. Use aspnet_regsql.exe instead.
I had the same issue in SQL2000. Might work for 2005.
If you receive this error message, first make sure that the SQL Server
Agent service is running. To do this, follow these steps:
Click Start, click Run, type Services.msc, and then
click OK.
In the Services window, locate the SQL Server Agent
service.
Make sure that the value of the Status column of the SQL
Server Agent service is Running.
If the SQL Server Agent service is running, this problem occurs
because the SQL Server Agent service cannot interpret the dot mark (.)
that represents the local computer in the ObjectName registry entry
under the following registry subkey. For a default instance, the
registry subkey is as follows:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MSSQLSERVER\ For
a named instance, the registry subkey is as follows:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MSSQL$InstanceName\
Note InstanceName is a placeholder for the instance of SQL Server.
Mine was not running, it was "disabled". If this is the case for you: Right click properties, change it from disabled to Automatic. Press OK. Then right click the SQL Server Agent service and click "Start". It should change to have the "Running" status.
from: https://support.microsoft.com/en-us/kb/911841

Cannot connect to SQL Server

I installed new Visual Studio 2010. Now if I want to add the database sql file, it shows an error saying "server not found or it not installed".
I saw that SQL Server 2008 Express is being automatically installed along with Visual Studio. I did not customize the install in any way.
Please solve my issue, and remove the error so that I can connect to the database server.
Check the answers on this post:
Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?
Then, if your SQL Server service needs to be re-started and will not re-start under the account you have configured, you may be able to select a different account provided you have Administrator rights.
To change the account under which the service runs, try:
All Programs | Microsoft SQL Server 2012 | Configuration Tools | SQL Server Configuration Manager | SQL Server Services , Right Click SQL Server (instance) , Properties, this account (radio), Browse, Advanced, Find … then pick the account you like … “NT AUTHORITY\LOCAL SERVICE
“, CLICK “Apply” << ( IMPORTANT !!! ) then try to re-start under the new account you just selected.
For an answer on which account to choose check this post:
https://serverfault.com/questions/217654/difference-between-nt-authority-network-service-and-nt-authority-system
Good Luck

Resources