简体   繁体   中英

Run MS SQL server script on startup

I am trying to run an SQL script when I start (or restart) my windows 2012 R2 server instance (Google Cloud Server). I am doing so using an SQL script, a Batch-file and the task-scheduler.

For the sake of testing I have created a simple SQL-script that adds a datestamp to a table:

USE <Databasename>
GO

INSERT INTO testingTable(time_Stamp)
VALUES (GETDATE())

SELECT * FROM testingTable

(where Databasename obviously contains the name of the specific database)

The batch-file looks as follows:

sqlcmd -S <servername> -i "C:\Temp\testQuery.sql" > C:\Temp\output.txt

I am outputting everything to a text-file. When I run the Batch-file the output looks fine: it prints a list with all the times I have run this SQL-query and saves it in the text-file.

I have scheduled this task to run on startup (following the steps here: https://www.sevenforums.com/tutorials/67503-task-create-run-program-startup-log.html ). I have tried a whole range of settings here but nothing seems to work, including the exact settings as highlighted in the forum.

When I now restart the server the output file shows the following error message:

Msg 904, Level 16, State 3, Server <servername>, Line 1
Database 7 cannot be autostarted during server shutdown or startup.
Msg 208, Level 16, State 1, Server <servername>, Line 2
Invalid object name 'testingTable'.

It seems like MS SQL does not allow scripts to be run before you log-in to one of the user accounts.

The problem really is that the actual SQL tasks that I want to run have to be run very early in the morning such that they are done when everyone arrives at the office. I have managed to automate the startup of the server using VMPower, but I can not automate logging in to one of the accounts.

I was hoping someone could give me some feedback on how to resolve this issue. Preferably I would want my current setup to work, but if anyone has an idea on how to automate logging in to an account on an existing google cloud server instance that would be really helpful as well.

Thank you, Joost

SQL Server offers the system stored procedure sp_procoption which can be used to designate one or more stored procedures to automatically execute when the SQL Server service is started.

For instance, you may have an expensive query in your database which takes some time to run at first execution. Using sp_procoption , you could run this query at server startup to pre-compile the execution plan so one of your users does not become the unfortunate soul of being first to run this particular query. I've used this feature to set up the automatic execution of a Profiler server side trace which I've scripted. The scripted trace was made part of a stored procedure that was set to auto execute at server start up.

exec sp_procoption @ProcName = ['stored procedure name'], 
@OptionName = 'STARTUP', 
@OptionValue = [on|off]

Read more: Automatically Running Stored Procedures at SQL Server Startup .


Docker

For solution to MSSQL Docker image, see: SQL Server Docker container is stopping after setup .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM