The ability to run procedures or SQL Agent jobs on startup provides a great tool for database administration. Running procedures on startup gives the ability to run cleanup routines, maintenance, auditing, business rules, or run a SQL trace.

The SQL Server Service

The SQL Server service (MSSQLSERVER) is the Windows service that runs the SQL Server database engine. This service provides the ability to execute stored procedures when the service is first started. You can specify this startup procedure by using the sp_procoption system stored procedure. In the following script, I use the code from a previous article where I outlined how to define your own custom trace scripts.

use master

GO
CREATE PROCEDURE usp_StartTrace
AS
BEGIN
DECLARE @TraceID INT
DECLARE @MaxSize BIGINT

SET @MaxSize = 15

EXECUTE sp_trace_create @TraceID output,
@options = 2, --//rollover the file when max size is reached
@tracefile = N'C:Tracefile.trc', --//trace file name
@maxfilesize = @MaxSize --//maximum file size

SELECT @TraceID --//output the traceid create for viewing

DECLARE @On BIT
SET @On = 1
EXECUTE sp_trace_setevent @TraceID, 10, 15, @On
EXECUTE sp_trace_setevent @TraceID, 10, 16, @On
EXECUTE sp_trace_setevent @TraceID, 10, 1, @On
EXECUTE sp_trace_setevent @TraceID, 10, 9, @On
EXECUTE sp_trace_setevent @TraceID, 10, 17, @On
EXECUTE sp_trace_setevent @TraceID, 10, 6, @On
EXECUTE sp_trace_setevent @TraceID, 10, 10, @On
EXECUTE sp_trace_setevent @TraceID, 10, 14, @On
EXECUTE sp_trace_setevent @TraceID, 10, 18, @On
EXECUTE sp_trace_setevent @TraceID, 10, 11, @On
EXECUTE sp_trace_setevent @TraceID, 10, 12, @On
EXECUTE sp_trace_setevent @TraceID, 10, 13, @On

EXECUTE sp_trace_setevent @TraceID, 12, 15, @On
EXECUTE sp_trace_setevent @TraceID, 12, 16, @On
EXECUTE sp_trace_setevent @TraceID, 12, 1, @On
EXECUTE sp_trace_setevent @TraceID, 12, 9, @On
EXECUTE sp_trace_setevent @TraceID, 12, 17, @On
EXECUTE sp_trace_setevent @TraceID, 12, 6, @On
EXECUTE sp_trace_setevent @TraceID, 12, 10, @On
EXECUTE sp_trace_setevent @TraceID, 12, 14, @On
EXECUTE sp_trace_setevent @TraceID, 12, 18, @On
EXECUTE sp_trace_setevent @TraceID, 12, 11, @On
EXECUTE sp_trace_setevent @TraceID, 12, 12, @On
EXECUTE sp_trace_setevent @TraceID, 12, 13, @On

EXECUTE sp_trace_setstatus @TraceID, 1

END

Go

I can then call the sp_procoption system stored procedure to execute the procedure defined above when the SQL Server service starts.

EXECUTE sp_procoption
@procname = 'usp_StartTrace',
@optionname = 'startup',
@optionvalue = 'on'

Conclusion

The ability to execute stored procedures at service startup is a powerful one. You can use this functionality to ensure Sarbanes Oxley compliance for SQL Server logins, ensuring all logins are written to a file, or to ensure that system cleanup routines are ran when the system starts. Try out this extremely useful functionality.

Database Jumping This was published in Database Jumping, check every Thursday for more stories

Comments

1

Arie Koster - 23/12/08

I appreciate your article very much. I am working on the similar subject for auditting users. I have one comment for when you start up again, the sp_create_trace will fail for there is already a trace file N'C:Tracefile.trc' and should there not be a backslash?

Kind regards,
Arie Koster.

» Report offensive content

2

Rajan - 24/12/08

Nice article. But i can run only in master db only.is it possible to use this in user defined Database.

With Regards,
Rajan

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

2

Rajan - 24/12/08

Nice article. But i can run only in master db only.is it possible to use this in user defined Database. With Regards, Rajan ... more

1

Arie Koster - 23/12/08

I appreciate your article very much. I am working on the similar subject for auditting users. I have one comment for ... more

Log in


Sign up | Forgot your password?

  • Staff Microsoft shows off IE9 preview

    This week, highlights from Microsoft's MIX10 conference and more in the Roundup. Read more »

    -- posted by Staff

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!