INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

SQL Server 2014 Express making a bat file to run a SQL script to backup at night

SQL Server 2014 Express making a bat file to run a SQL script to backup at night

SQL Server 2014 Express making a bat file to run a SQL script to backup at night

(OP)
This is a SQL Express install so there is no Agent listed, so I can't create a job.
When I run the SQL script below either in the batch file or at sqlcmd prompt it just opens the .sql file in SSMS and does not run it. I want this to run unattended at night. What do I need to do to make this work?
Shown below are my two files: the first is my sql script, second is the command prompt
The SQL script creates a backup file just fine when I run it manually in SSMS, however the purpose of this is unattended.
Or any other suggestions would be great:)

TIA

CODE

-- get todays date
Declare @TodaysDate as nvarchar(10) = Replace(CONVERT(nvarchar(10), getdate(), 103),'/', '')
Declare @FileName as nvarchar(100) = N'D:\SQLBackup\StockTrack_' + @TodaysDate + '.bak' 

BACKUP DATABASE [StockTrack] TO  DISK = @FileName
WITH NOFORMAT, 
NOINIT,  NAME = N'StockTrack-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO 

CODE

sqlcmd -S ServerName\SQLExpress -i C:\mybackup.sql 

RE: SQL Server 2014 Express making a bat file to run a SQL script to backup at night

As an alternative - SQL Server 2014 Developer Edition DOES have the Agent AND is now FREE (as is SQL Server 2016 Developer Edition - but this is a much more stressfull install!!)

RE: SQL Server 2014 Express making a bat file to run a SQL script to backup at night

I've done this a hundred times for my customers. What I usually do is to create a stored procedure in the database, and then use SQLCMD to execute that stored procedure.

Regardless, you're missing something very important on the command line. Try this:

CODE

sqlcmd -S ServerName\SQLExpress -i C:\mybackup.sql -Q 

According to the documentation, -Q means "cmdline query" and exit.

-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom

RE: SQL Server 2014 Express making a bat file to run a SQL script to backup at night

In order to schedule the job, you could always use Windows Scheduler. You just need to work out how you do the error checking, as I am not too familiar with options around failed jobs.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close