views:

1535

answers:

5

How is it possible to run a stored procedure at a particular time every day in SQL Server Express Edition?

Notes:

  • This is needed to truncate an audit table
  • An alternative would be to modify the insert query but this is probably less efficient
  • SQL Server Express Edition does not have the SQL Server Agent

Related Questions:

+9  A: 

Since SQL Server express does not come with SQL Agent, you can use the Windows scheduler to run a SQLCMD with a stored proc or a SQL script.

http://msdn.microsoft.com/en-us/library/ms162773.aspx

Raj More
A: 

You could use Task Scheduler to fire a simple console app that would execute the Sql statement.

Craig Bart
+3  A: 

If you are using Express Edition, you will need to use the Windows Scheduler or the application connecting to the server in some way.

You would use the scheduler to run sqlcmd. Here are some instructions for getting the sqlcmd working with express edition.

Yishai
A: 

As you have correctly noted, without the agent process, you will need something else external to the server, perhaps a service you write and install or Windows scheduler.

Note that with an Express installation for a local application, it is possible that the machine may not be on at the time you want to truncate the table (say you set it to truncate every night at midnight, but the user never has his machine on).

So your scheduled task is never run and your audit log gets out of control (this is a problem with SQL Server Agent as well, but one would assume that a real server would be running non-stop). A better strategy if this situation fits yours might be to have the application do it on demand when it detects that it has been more than X days since truncation or whatever your operation is.

Another thing to look at is if you are talking about a Web Application, there might be time when the application is loaded, and the operation could be done when that event fires.

As mentioned in the comment, there is sp_procoption - this could allow your SP to run each time the engine is started - the drawbacks with this method are that for long-running instances, there might be a long time between calls, and it still has issues if the engine is not running at the times you need the operation to be done.

Cade Roux
sp_procoption will allow a stored procedure to run automatically.
Thomas Bratt
+2  A: 

I found the following mechanism worked for me.

USE Master
GO

IF  EXISTS( SELECT *
            FROM sys.objects
            WHERE object_id = OBJECT_ID(N'[dbo].[MyBackgroundTask]')
            AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[MyBackgroundTask]
GO

CREATE PROCEDURE MyBackgroundTask
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- The interval between cleanup attempts
    declare @timeToRun nvarchar(50)
    set @timeToRun = '03:33:33'

    while 1 = 1
    begin
        waitfor time @timeToRun
        begin
            execute [MyDatabaseName].[dbo].[MyDatabaseStoredProcedure];
        end
    end
END
GO

-- Run the procedure when the master database starts.
sp_procoption    @ProcName = 'MyBackgroundTask',
                @OptionName = 'startup',
                @OptionValue = 'on'
GO

Some notes:

  • It is worth writing an audit entry somewhere so that you can see that the query actually ran.
  • The server needs rebooting once to ensure that the script runs the first time.
Thomas Bratt
In conjunction with your audit trail, I would add to that stored proc a check of the process history, so that if it has not been run in 24 hours or whatever - to go ahead and run the process at startup. This would handle cases when the machine gets shut down over night or occasional nights or similar (a VM which is spun up on demand).
Cade Roux
Good point! For our case, the database runs continually as it is a customer system that is run 24x7. I wonder if the simplest thing to do is to run the 'MyDatabaseStoredProcedure' script before the wait? In our case this would work fine but it might not for a different application requirement.
Thomas Bratt