Category: SQL Server

  • KILL SQL Server 2000 Blocking SPID and Records it details

    Problem
    Today I got an email from one of my blog reader; they have an application developed with VB6.0 and SQL Server 2000. Application was developed long back and now their database size is increased as well. Due un-efficient coding they are getting blocking issue and stuck all their application transactions. He asked for writing a query which will execute by SQL Server Agent at every 1 minute and will KILL the culprit SPID. He also wants me to store the KILL transaction history as well.

    As a solution I have written below query for him and which working fine now.

    -- Create below table in master database
     create table blkHistory
    (
    	SPID int,
    	blocked int,
    	killedSPID int,
    	date datetime default getdate(),
    	querytext varchar(8000)
    )
    
    
    -- add below code in to job command text box
    declare @SPID as int,
            @blocked as int,
            @KilledSPID as int
    
    declare @querytext as varchar(8000), @sql nvarchar(400)
    
    select @SPID = spid,@blocked = blocked from sysprocesses where blocked <> 0
    
    --select spid,blocked from sysprocesses where blocked <> 0
    
    select @sql = 'KILL ' + cast (@blocked as nvarchar(100))
    
    DECLARE @Handle binary(20)
    SELECT @Handle = sql_handle FROM sysprocesses WHERE spid = @blocked
    
    SELECT @querytext = text FROM ::fn_get_sql(@Handle)
    
    EXECUTE sp_executesql @SQL
    
    If @SPID > 0
    begin
    insert into blkHistory(SPID,blocked,KilledSPID,querytext)
    values (@SPID,@blocked,@blocked,@querytext)
    end
    
    --you can use below query to retrieve datafrom blocking history
    select * from master..blkHistory
    
  • Steps to insert error log records into temporary table

    You can follow the below steps to enter error log records into temporary table and query it.

    -- Command will create the temporary table in tempdb
    CREATE TABLE [dbo].[#TmpErrorLog]
    ([LogDate] DATETIME NULL,
     [ProcessInfo] VARCHAR(20) NULL,
     [Text] VARCHAR(MAX) NULL ) ;
    
    -- Command will insert the errorlog data into temporary table
    INSERT INTO #TmpErrorLog ([LogDate], [ProcessInfo], [Text])
    EXEC [master].[dbo].[xp_readerrorlog] 0 ;
    
    -- retrieves the data from temporary table
    SELECT * FROM #TmpErrorLog
  • Stop successfull backup loging messages in SQL Server Error Log?

    Whenever backup peformed on SQL Server, it records the backup entry in the SQL Server. Because of that error log file grows and sometimes we are missing important information from there.

    For example,

    use master
    backup database jshah to disk = 'c:\jshah.bak'

    Above command will log the below message in the SQL Server error log.
    Backup Message:
    Database backed up. Database: jshah, creation date(time): 2010/12/31(09:55:22), pages dumped: 178, first LSN: 28:60:170, last LSN: 28:130:1, number of dump devices: 1, device information: (FILE=1, TYPE=DISK: {‘c:\jshah.bak’}). This is an informational message only. No user action is required.

    Solution
    As a solution we can turn on the trace flag 3226 to stop loging of sucessfull backup message.

    You can turn it on either using SQL Server Service Starup Parameter (-T 3226) or using DBCC TRACEON command.

    -- To turn on the trace flag at global level
    DBCC TRACEON (3226,-1)
    -- To turn off the trace flag at global level
    DBCC TRACEOFF (3226,-1)
  • Wish You All Very Happy and Prosperous New Year 2011

     

    Wish You all Very Happy and Prosperous New Year
  • Download SQL Server Interview Question in PDF

    SQL Server Interview Questions for DBAs and Devlopers