Tuesday, November 6, 2012

Kill Inactive Session using Cursor

 I had a requirement to select all the inactive session with more than 24 hours and kill the process where status is "Sleeping".

Below is the small Cursor which will select all inactive session where status is "sleeping" and kill the process based on the spid.



DECLARE @SPID VARCHAR(20)
DECLARE @STATUS VARCHAR(20)
DECLARE @QUERY VARCHAR(20)
DECLARE CUR CURSOR FOR
SELECT spid FROM sys.sysprocesses WHERE db_name(dbid)='Ashish' and STATUS='sleeping' and login_time<GETDATE()-1
OPEN CUR
FETCH NEXT FROM CUR INTO @SPID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @QUERY= 'KILL ' + @SPID
print (@QUERY)
EXEC (@QUERY)
FETCH NEXT FROM CUR INTO @SPID
END
CLOSE CUR
DEALLOCATE CUR

Service Account Permission Issue in Active Directory

Recently I faced an issue with Service account after enabling the service broker for one of the database.

Scenario: As per Client requirement I enabled the service broker for one of the database. After that there was huge Logs generating in SQL error Logs.

Error: The activated proc '[IdentityServerPolicy].[SqlQueryNotificationStoredProcedure-cdad7b08-c759-4d81-b4ef-c583cb6d45ca]' running on queue 'IdentityServerPolicy.SqlQueryNotificationService-cdad7b08-c759-4d81-b4ef-c583cb6d45ca' output the following:  'Could not obtain information about Windows NT group/user XXX\ABC, error code 0x5.'


I tried to run below command to get the details of service account.

Xp_Logininfo 'domain\account name'

But i got below error








Solution: The service account needs "Read permissions"in the active directory.

Open Active directory. Search for the login-> Right Click->Go to the properties->Select Security->Select "Authenticated Users"-> In bottom select "Read Permission" and click on the 'Allow' check box->
Apply->.Ok

After giving the permission I ran the same command and now i got the details about the service account.

Database Backup using Cursor

Some times it requires to take a backup of all the databases or more than one database which exist on your SQLinstance. There is a small cursor which will take a backup of all the database at a time to the specified location.
In the below Cursor you have to provide the path as where do you want to keep the backup files and the database name as per your requirement.
In the below example I am taking backup of all the user database to 'D:\Backup\' location.


DECLARE @name VARCHAR(50) -- database name
DECLARE @path VARCHAR(256) -- path for backup files
DECLARE @fileName VARCHAR(256) -- filename for backup
DECLARE @fileDate VARCHAR(20) -- used for file name
SET @path = 'D:\Backup\'
SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
DECLARE db_cursor CURSOR FOR
SELECT name FROM master.dbo.sysdatabases WHERE name NOT IN ('master','model','msdb','tempdb')
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @name
WHILE @@FETCH_STATUS = 0
BEGIN
SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
BACKUP DATABASE @name TO DISK = @fileName
FETCH NEXT FROM db_cursor INTO @name
END
CLOSE db_cursor
 DEALLOCATE db_cursor