Friday, 26 July 2019

Find the run-time status for all SQL Server Agent jobs

Use below script to Find the run-time status for all SQL Server Agent jobs

use msdb
go



SELECT
        sj.Name,
                 CASE
                      WHEN sja.start_execution_date IS NULL THEN 'Not running'
                      WHEN sja.start_execution_date IS NOT NULL AND sja.stop_execution_date IS NULL THEN 'Running'
                      WHEN sja.start_execution_date IS NOT NULL AND sja.stop_execution_date IS NOT NULL THEN 'Not running'
                      END  AS 'RunStatus'
FROM   msdb.dbo.sysjobs sj
JOIN   msdb.dbo.sysjobactivity sja
ON     sj.job_id = sja.job_id
WHERE  sja.session_id =
                    (SELECT MAX (session_id) FROM msdb.dbo.sysjobactivity)
order by RunStatus desc

Search SQL Agent Job using Specific keyword text or SP - 2000\2005\08\08R2\12\14\16\17\19

SQL Server 2000 Job Step Search

USE [msdb]
GO
SELECT    jb.job_id,
       jb.originating_server,
       jb.name,
       js.step_id,
       js.command,
       jb.enabled
FROM    dbo.sysjobs jb
JOIN    dbo.sysjobsteps js
       ON    js.job_id = jb.job_id
WHERE    js.command LIKE N'%KEYWORD_SEARCH%'

Search SQL Server Agent Job Steps for Specific Keyword Text or Stored Procedure-2005\08\08R2\12\14\16\17\19

SELECT 
       jb.job_id,
       js.srvname,
       jb.name,
       js.step_id,
       js.command,
       jb.enabled
FROM    msdb.dbo.sysjobs jb
JOIN    msdb.dbo.sysjobsteps js
       ON    js.job_id = jb.job_id
JOIN    master..sysservers s
       ON    js.srvid = jb.originating_server_id
WHERE    js.command LIKE N'%KEYWORD_SEARCH%'
GO



How to find Job owner for all SQL Server Agent Jobs and change the owner

How to find Job owner for all SQL Server Agent Jobs and change the owner

We got below error from so many servers in my organisation.

Error: "The job failed.  The owner '' of job '' does not have server access."

Causes: Most of the below cases.
1. Logins does not exists
2. Job owner left from the organization
3. Jobs owner disabled state.

That means a DBA had created a SQL Server Agent  job with his name as owner. Once he/she will leave the company and account will be removed from AD, the jobs will start failing.

It is good to use 'service account' or 'sa' as SQL Server Job owner, so you don't have to worry in case owner leave the company.

Fix:

Single job we can fix manually but multiple servers multiple jobs how can we fix this issue, so that we found a way using below script by joining the below tables and query.

master.sys.syslogins--Find the login Name
msdb..sysjobs--Find the jobs Name

Below query will return your server job names and respective job owners.


SELECT SJ.NAME AS JobName
    ,SL.NAME AS OwnerName
FROM msdb..sysjobs SJ
LEFT JOIN master.sys.syslogins SL ON SJ.owner_sid = SL.sid
  
Or

Adding little more stuff for serverName, job which is enabled\disabled state.

SELECT                     @@servername as Server_name,

                                 sj.name, CASE
                                                      WHEN sj.enabled = 1 THEN 'Enable'
                                                      ELSE 'Disable'
                                               END             AS JobStatus,
                                 SUSER_SNAME(sj.owner_sid) AS owner
from                msdb..sysjobs_view sj
left outer join master.sys.syslogins sl on sj.owner_sid = sl.sid
where               SUSER_SNAME(sj.owner_sid) <> 'sa'
or                         SUSER_SNAME(sj.owner_sid)  is null


Now if we would like to update all the job where owner is not 'sa', we can use below query. So you can also modify the query to filter the jobs for which you would like to update owner.

--Please provide the New Owner for The Jobs you like to Change, I am using sa

DECLARE @OWNERName VARCHAR(100)
SET @OWNERName = 'sa'--please change SA or SQL Instance service account
DECLARE @OldJobOwner VARCHAR(100)
DECLARE @JobName VARCHAR(1000)
DECLARE Job_Cursor CURSOR
FOR

--Change your Query as per requirements, I am selecting all Job where owner<>sa

SELECT SJ.NAME AS JobName
    ,SL.NAME
AS OwnerName
FROM msdb..sysjobs SJ
LEFT JOIN master.sys.syslogins SL ON SJ.owner_sid = SL.sid
WHERE L.NAME <> @OWNERName

OPEN Job_Cursor --Open a cursor
FETCH NEXT FROM Job_Cursor  --Fetch Data from cursor

INTO @JobName
    ,@OldJobOwner
WHILE (@@FETCH_STATUS <> - 1)

BEGIN
   
EXEC msdb..sp_update_job @job_name         = @JobName
                            ,@owner_login_name = @OWNERName

PRINT 'Ownerd Change for ' + @JobName + 'Job from ' + @OldJobOwner + ' to '
 + @OWNERName

FETCH NEXT
   
FROM Job_Cursor
   
INTO @JobName
        ,@OldJobOwner
END

CLOSE Job_Cursor -- Close the cursor
DEALLOCATE Job_Cursor --De-allocate the cursor

You can wrap up by creating the stored procedure with @OWNERName parameter and use that stored procedure as a job in every server to change the job owners to 'sa' or 'service account'. 

Just to be safe side please initially test is 'TEST\DEV' server and then move to production.

Sunday, 19 May 2019

How to Re-solve “There is not enough space on the disk” Query error?

How to Re-solve “There is not enough space on the disk” Query error?


One of my friends communicated to me “Whenever I queried in SQL Server Management Studio, I keep on getting this error. How do I solve this?”

An error occurred while executing batch\Big query. Error message is: There is not enough space on the disk.

There can be two reasons
1 There is no space in the Drive where SQL Server files are stored
2 The drive where Tempdb is stored may not have enough space

But these are not issues to my friend as the Drive in which SQL Server files (Both mdf and ldf) and TempDB are in E drive which has enough space (200 GB+ free space).

It should be noted that whenever Queries are executed in SSMS that need result to be returned, SSMS caches the resulsets in a C drive under the Users Folder. So I asked my friend to check this folder

Tools–>Options–>Query Results–>SQL Server


It was found that it was pointing to C Drive and the drive does not have any free space at all. I asked him to change this path into different drive or clear some space on C drive by deleting any unwanted files and the problem is solved.

Tuesday, 14 May 2019

Database Restoration error User does not have permission to perform this action

User does not have permission to perform this action

When we trying to restore a database we got below error.

TITLE: Microsoft SQL Server Management Studio
------------------------------

Restore of database 'xxxxxxx' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)

------------------------------
ADDITIONAL INFORMATION:

System.Data.SqlClient.SqlError: User does not have permission to perform this action. (Microsoft.SqlServer.SmoExtended)

Cause:

No active connection on that database even though got same error finally we identified the reason is one of the orphaned replication job

Fix:

We removed that orphaned replication job from SQL agent, after that we are able to complete the restoration.

Monday, 13 May 2019

An existing connection was forcibly closed by the remote host

A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 – An existing connection was forcibly closed by the remote host.)

When you receive below error will show if a connection is drawn from the remove client connection and the connection to the server has been lost.

Msg 10054, Level 20, State 0, Line 0
A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)


There is no way for a connection from the client to host to know that the connection has been severed.

CAUSE

1.The cause is environmental and can be a number of things regarding the SQL server and configuration.

It can be a deadlock in the database and time outs need to be increased.

It can also be a network connectivity issue.---My Scenario

In this scenario this issue mainly when we worked with one of my customer, if the customer is running any query the connection is establishing successfully between the client and server, when the query execution complete , during the query out put getting connection loss error. We are able to find the issue, the customer side network packets are dropping. Customer is worked with their network engineer they fixed the network issue, after that customer is able to get the query results.

2: It can also be caused when the Orion server is unable to resolve the IP address from the SQL server's host-name.

Resolution:

1:Enable Remote Connection, right click on the server node and select Properties:
a. Go to the left tab of Connections and select “Allow remote connections to this server”.
b. Make sure that MSDTC is enabled in the firewall.
Enable SQL Server Browser Service:
a. Go to All Programs > Microsoft SQL Server 2008 > Configuration Tools > SQL Server Configuration Manager > SQL Server Browser.
b. Right click on SQL Server Browser and click Enable.
Create an exception of sqlbrowser.exe in Firewall, Windows Firewall may prevent sqlbrowser.exe to execute.
a. Search for sqlbrowser.exe on your local drive where SQL Server is installed.
b. Copy the path of the sqlbrowser.exe like
C:\Program Files\Microsoft SQL Server\90\Shared\sqlbrowser.exe(Note:If SQL 2008\8R2\12\14\16\17 paths should be C:\Program Files\Microsoft SQL Server\100\110\120\130\140\Shared\sqlbrowser.exe) and create the exception of the file in Firewall.
Go to All Programs > Microsoft SQL Server 2008 > Configuration Tools > SQL Server Configuration Manager > Select TCP/IP.
a. Right Click on TCP/IP and click Enable.
b. Restart SQL Server Services for all the changes to take effect.
c. Right click and go to the menu properties to select a location where the default port of SQL server can be changed.
Set the commandtimeout property of the command object to an appropriate value. Use a value of zero to wait without an exception being thrown. (Increased the "connect timeout"  in connectionstring of app.config).
Check the network cabling, the NIC for any driver updates or physical issues, or any network equipment that  the SQL server connects to such as the ports.

2:Run Configuration Wizard, and instead of indicating the hostname or FQDN of the SQL server, input the IP address


A transport-level error has occurred when sending the request to the server (Msg 233)

The error “A transport-level error has occurred when sending the request to the server”  can occur when SQL Server client cannot connect to the server. The reason for the lack of connection can become a wrong configured remote connections. In this scenario, SQL Server will send the following message:

Msg 233, Level 20, State 0, Line 0
A transport-level error has occurred when sending the request to the server. (provider: Shared Memory Provider, error: 0 - No process is on the other end of the pipe.)


To solve this problem try to allow SQL Server to accept remote connections or check your firewall settings. It is possible to enable remote connections with the help of the SQL Server Configuration Manager tool.





Disable CDC at table level

 How to Disable CDC( Change Data Capture) for tables Change Data Capture (cdc) property is disabled as default.  here I will Query you how t...