5

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

3
  • 1
    Are you looking for this mssqltips.com/sqlservertip/1574/… Commented Jun 2, 2017 at 18:03
  • 1
    Probably the local copy of SQL has not finished initializing, but you are trying to access the database before they are on-line. Look at the event viewer and see when you get the error, and when the SQL server is on-line. You might have to insert a 30 second delay before writing to the database. Commented Jun 2, 2017 at 18:17
  • @Chuck: Yes this does the trick indeed. I built in a 2 min delay using a timeout statement. Thank you for this. Commented Jun 5, 2017 at 14:16

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.