September 13, 2016 at 6:57 am
Hi,
I want to use Windows Server Task Scheduler to run a SQL command once a day
So I setup the below 2 files (1 batch, 1 sql) and set them up in Task Scheduler.
The Task runs and completes, but the SQL is not deleting the records....
If I manually run the Batch file, it works.
I've set the job up in task scheduler using the logged in admin account and also tried ticking use higher priveledges etc, but still not working.
There are no errors. When I run the batch script manually, a dos command window appears, but when you use scheduler, you don't get a command window.
These are the 2 files:
deleterecords.bat - this contains this line below:
sqlcmd -S myserver -E -i DeleteRecordsTest.sql
DeleteRecordsTest.sql
USE mydb
DELETE FROM dbo.Delete_Records_Test
WHERE TimeStamp <= DateAdd(d,-365,GETDate())
GO
Can anyone tell me how I can achieve this using Task Scheduler please?
I'm using SQLExpress, so no Server Agents etc and I don't want to use SSIS etc, just something to run locally on the local server db and tables.
Thanks
September 13, 2016 at 7:09 am
Withdrawn, sorry about that.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
September 13, 2016 at 7:13 am
Hi,
As mentioned, don't have Server Agent, using SQL Express
September 13, 2016 at 7:18 am
I suspect the batch file is most likely working (the Task Scheduler when calling a batch file will run it, even if you don't see the CMD window) and instead that the SQLCMD is failing to login to your SQL instance.
The task will run generally with the NT Service\System account, which likely has little or no access to your SQL instance. A possible solution (which has its own problems) is to use the -U and -P switches for sqlcmd (login id and password) The obvious problem with this solution is, you'll have a password in the clear in the batch file.
September 13, 2016 at 7:28 am
Already tried using the -U and -P switches (just to try it) and even that doesn't work either
September 13, 2016 at 7:40 am
Besides running the script with an impersonated credential, which solves the SQL problem,i would think you need an explicit path to your sql file:
sqlcmd -S myserver -E -i "C:\Data\AutomatedScripts\DeleteRecordsTest.sql"
Lowell
September 13, 2016 at 7:43 am
jasona.work (9/13/2016)
I suspect the batch file is most likely working (the Task Scheduler when calling a batch file will run it, even if you don't see the CMD window) and instead that the SQLCMD is failing to login to your SQL instance.The task will run generally with the NT Service\System account, which likely has little or no access to your SQL instance. A possible solution (which has its own problems) is to use the -U and -P switches for sqlcmd (login id and password) The obvious problem with this solution is, you'll have a password in the clear in the batch file.
When it runs through Task Scheduler, is there anything generated in the SQL Server logs?
Also check in the Windows Event logs (Application, Security, and System) for anything around the time the task executes.
September 13, 2016 at 8:15 am
Thanks Lowell, that worked.
I didn't need to add the userid or password, I just added the path to the file like u said and scheduled it.
tested and works great.
Thanks again and also everyone else who chimed in.
P.S... one last thing, I want to delete records which are older than 2 years old. I am using the d,-365 parameter, is there a way of just saying 2 years instead of xxx 730 days?
September 13, 2016 at 8:59 am
rkelly58 (9/13/2016)
Thanks Lowell, that worked.I didn't need to add the userid or password, I just added the path to the file like u said and scheduled it.
tested and works great.
Thanks again and also everyone else who chimed in.
P.S... one last thing, I want to delete records which are older than 2 years old. I am using the d,-365 parameter, is there a way of just saying 2 years instead of xxx 730 days?
This should do it:
WHERE TimeStamp <= DateAdd(yy,-2,GETDate())
Of course, test, test, test before using.:-D
/edit: Bloody SQL code wrapper kept eating the <= sign...
September 14, 2016 at 7:26 am
Thanks Jasona, that worked as well.
All sorted, thanks again to everyone involved.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply