We help IT Professionals succeed at work.

How do I exec SQL Query from Windows Task Scheduler

I am running an SQL Server Express 2008 on a Windows Server 2008 R2. I want to make a task on Windows Scheduler (express edition doasn't have SQL Agent) which will run an SQL Query every day. The Query just truncates a table and fill it again with refresh data
Comment
Watch Question

Commented:
Hi,

You could schedule sqlcmd to run your sqlscript.

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

sqlcmd -Sserver -Uusername -Ppassword -iscriptfile

/peter
Commented:
Or use parameters
-E for trusted login instead of -U/-P
and/or
-Q "sql-statement" instead of -i

Author

Commented:
Thanks Peter, I'm gonna give it a try
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
do you know Express Maint? Check it from http://expressmaint.codeplex.com/

Author

Commented:
It opens a message: "T-SQL execution command line utility has stopped working"

Commented:
How does your command line and sql script look like?

Author

Commented:
I tried it in both ways:
sqlcmd -S .\SQLExpress -i c:\Refresh_RepoForBulkMails.sql
sqlcmd -S .\SQLExpress  -U <username> -P <password> -i c:\Refresh_RepoForBulkMails.sql

Author

Commented:
The original SQL Script is huge, so I use the following for my tests:

USE [Asf_PROD]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

insert into OdikiGR
select 12345,'test','123','123','01-01-2000','01-01-2000','01-01-2000',100.25,150.52,1

Commented:
I never seen that error message. Googling it, the only solution mentioned is re-installing.

The sql and the 2nd commandline, you need login, looks ok. Have you tried a real simple case, like

sqlcmd -S .\SQLExpress  -U <username> -P <password> -Q "select 'test';"

Author

Commented:
I tried that, same error message.

I'm not going toy reinstall SQL Server. I have several DBs on it...
I will execute the SP from an ASP page and then schedule IE to call the page