Tag Archives: Scripts - Page 2

Trigger to Check Job failures – SQL 2000

I was trying to find a alert which could fire immediately upon any of the job failure in the server to send an email. Unfortunately I couldn’t find any the alert related to this. So I thought of writing a DML trigger which should fire immediately when an entry is inserted in sysjobhistory table on msdb database. The next step is that the trigger should send a mail when there is failure in the job, hence I’ve used run_status column in the table, as all the DBA’s know when this column has a value 0 then it states the job has been failed. I’ve tried this script in SQL Server greater than version 2000 however it’s not working on it. This script works only on SQL Server 2000, still some of them are still working on SQL 2000 so I thought this script has still value. This DML trigger will send an email upon any of the job failure.

Applies To

  • SQL Server 2000
    Script
    In the script below make sure to change the mail address used in the recipients field. You also need SMTP mail to be configured in SQL Server to work however you can modify the script to include CDO messaging.
    /*
    Written By : Vidhya Sagar
    www.sql-articles.com
    */
    USE MSDB
    GO
    CREATE TRIGGER jobfailurenotify
    ON sysjobhistory
    FOR INSERT
    AS
    DECLARE @failed TINYINT, @subject VARCHAR(300), @body VARCHAR(500)
    SELECT @failed = COUNT(*) from inserted where run_status=0
    IF @failed > 0
    BEGIN 
        SELECT @subject=name from msdb.dbo.sysjobs where job_id in (select distinct(job_id) from inserted where run_status=0)
        SET @subject=@subject + ' job failed on ' + CONVERT(VARCHAR(25), GETDATE())
        SET @body=@subject + '. Please look in to this. Server Name : ' + CONVERT(VARCHAR(25),SERVERPROPERTY('SERVERNAME'))
        EXEC master.dbo.xp_sendmail
        @recipients     = N'kvs1983@gmail.com',
        @subject        = @subject,
        @message           = @body
    END
    ELSE 
    BEGIN
        PRINT 'No job failures found'
    END

Sample Mail Output

From:    testing@gmail.com

Sent:    Monday, March 02, 2009 6:50 PM

To:    kvs1983@gmail.com

Subject:    TESTING job failed on Mar  2 2009  6:50PM

TESTING job failed on Mar  2 2009  6:50PM. Please look in to this. Server Name : SAGARSYS

If you have any concerns or queries regarding this script. Just post it out in our Forums Section.

VN:F [1.9.17_1161]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.17_1161]
Rating: 0 (from 0 votes)

How to find TCP\IP port via T-SQL

In most of the forums I’ve seen that users are not able to find on which port does their sql server is listening. Hence I decided to write a script to check TCP\IP port on which sql server is listening. Checkout the below script.

Script

DECLARE @key VARCHAR(50), @RegistryPath VARCHAR(200)
IF (SERVERPROPERTY('INSTANCENAME')) IS NULL
BEGIN
SET @RegistryPath='Software\Microsoft\MSSQLServer\MSSQLServer\SuperSocketNetLib\Tcp\'
END
ELSE
BEGIN
SET @RegistryPath='Software\Microsoft\Microsoft SQL Server\'+CONVERT(VARCHAR(25),SERVERPROPERTY('INSTANCENAME')) + '\MSSQLServer\SuperSocketNetLib\Tcp\'
END
EXEC master.dbo.xp_regread 'HKEY_LOCAL_MACHINE', @RegistryPath, 'tcpPort'

Output

Value Data
tcpPort 1433
VN:F [1.9.17_1161]
Rating: 5.0/5 (2 votes cast)
VN:F [1.9.17_1161]
Rating: +1 (from 1 vote)

Script Default Database

Default Database is one of the most important parameter when we detach the database. If we don’t have the track of default db for logins then it might be a problem when you use an application. Consider you are planning to move a user database from X: drive to Y: drive and your application login has this database as default db. Without noticing this if you detach the database then the application login will point either to null, so you need to map the db again to the login, hence after attaching the database and if you work with application then you will be ended with the below error

Cannot open user default database. Login failed
Login failed for user ‘xxx’. (Microsoft SQL Server, Error:4064)

To avoid this we can script default db for all the logins using the below script which will provide the output as T-SQL statement. You need to execute the script before detaching the db and after attaching it successfully execute the output from the previous script execution which will again map default databases to the respective logins!!

Script

/*
Script to detach all user databases
Written by Vidhya Sagar
www.sql-articles.com
*/
declare @name varchar(100), @dname varchar(75), @all varchar(500)
declare dbname cursor for select name from syslogins
open dbname
fetch dbname into @name
while @@fetch_status=0
begin
select @dname=dbname from syslogins where name=@name
set @all='sp_defaultdb '+''''+@name+''','+''''+@dname+''''+char(10)+'go'
print @all
fetch dbname into @name
end
close dbname
deallocate dbname

Sample Output

sp_defaultdb ‘test’,'sagar_25′

go

If you have any suggestions or discussion in the script above, lets discuss on SQL-Articles Forums section

VN:F [1.9.17_1161]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.17_1161]
Rating: 0 (from 0 votes)

Detach User Databases

I started side by side upgrading SQL 2000 to SQL 2005, at that time I need to detach all the user databases from the source server and need to attach the same in destination server. Source server has almost 80 user databases and I feel bored to detach all the database one by one which is time consuming too.. Hence I thought of writing a script which will help me to detach the user databases in a single stretch. I know that someone will face this situation, hence I’m posting my script here which will help you!!

/*
Script to detach all user databases
Written by Vidhya Sagar
www.sql-articles.com
*/
set nocount on
declare @dbname as varchar(80)
declare @server_name as varchar(20)
select @server_name = @@servername
declare rs_cursor CURSOR for select name from master.dbo.sysdatabases where name not in ('model','master','msdb','tempdb','distribution','repldata')
open rs_cursor
Fetch next from rs_cursor into @dbname
IF @@FETCH_STATUS <> 0
PRINT 'No User databases found!!!'
WHILE @@FETCH_STATUS = 0
BEGIN
Declare @exec varchar(8000)
Set @exec= 'DECLARE @spidstr varchar(8000)
DECLARE @ConnKilled smallint
SET @ConnKilled=0
SET @spidstr = ''''
SELECT @spidstr=coalesce(@spidstr,'','' )+''kill ''+convert(varchar, spid)+ ''; ''
FROM master..sysprocesses WHERE dbid=db_id('''+@dbname+''')
IF LEN(@spidstr) > 0
BEGIN
EXEC(@spidstr)
SELECT @ConnKilled = COUNT(1)
FROM master..sysprocesses WHERE dbid=db_id('''+@dbname+''')
END'+char(10)+';'+char(10)+
'Exec sp_detach_db ' + @dbname
Exec (@exec)
print 'Detach of ' + upper(@dbname) + ' database successfully completed'
print ''
FETCH NEXT FROM rs_cursor INTO @dbname
END
CLOSE rs_cursor
deallocate rs_cursor
print ' '
print upper(@server_name) + '--> All User databases successfully detached'

Sample Output:

Detach of NORTHWIND database successfully completed

Detach of PUBS database successfully completed

SAGARSYS\SQL2K–> All User databases successfully detached

Usage:

Copy the above script and execute it, this will automatically detach all the user databases and you will receive the output as above!!

If you have any concurrence or discussion in the script above, lets discuss on SQL-Articles Forums section.

VN:F [1.9.17_1161]
Rating: 5.0/5 (1 vote cast)
VN:F [1.9.17_1161]
Rating: 0 (from 0 votes)
  • Page 2 of 2
  • <
  • 1
  • 2