Executing a stored procedure using Windows task Scheduler - sql-server

I've been trying to set up a schedule to run a stored procedure every hour in Windows Task Scheduler (as I'm using SQL Express and can't install 3rd party tools) but after trying various methods such as running a .bat file from task scheduler, opening SqlCmd utility from task scheduler and passing either the command line syntax or a .sql script file I'm having no luck.
I know this can be done and therefore I'm sure it's something I've missed but if anyone can share their experience of this I'd very much appreciate it.
The following command is in the batch file...
sqlcmd -E -i"C:\Users\Administrator\Desktop\test.sql" -o"C:\Users\Administrator\Desktop\dump.txt"
Thanks a lot

If you are an admin on the sql instance (Since you are using SQLExpress I bet you are trying to do this on your own computer so there is a high chance your user is an admin of the sql instance) you should not use -E at all, just ignore it.
Second, specify the server even if you are working on local.
Start with a simple sql command like below:
sqlcmd.exe -S "." -d MY_DATABASE -Q "SELECT * FROM MY_TABLE"
Replace MY_DATABASE and MY_TABLE with your dbname and table name. Make sure you can run it from command line. It should return the data from your table. (Beware command line options are case-sensitive so -s is not same as -S)
Last, do not try to feed parameters through task scheduler. Put the command with all parameters in a .bat file and just run the batch from task scheduler.

I have recently had a similar issue and my experience may assist you. I was calling a small app i.e. EXE from a batch file. I was scheduling the batch file to run from the Windows Task Scheduler. The app was accessing the SQL data using Windows Authentication.
I could run the app directly i.e. click on the EXE to run it.
I could run the app from the batch file.
But if I tried to run the scheduled task it seemed to start but did nothing and posted no errors that I could find.
I found if I changed the app to run with SQL Authentication it could be run from the Task Scheduler.
I suspect there is something about the context of the Windows Authentication when it is run from Task Scheduler that is not recognised by SQL.

Related

Scheduled Task Runs .bat File with sqlcmd, but Doesn't Update the Output File

My scheduled task (on Windows Server 2008 R2) is "successfully" running each morning, but the output of sqlcmd does not get updated in the output file. The log file changes to show that the batch file ran, but the output file remains the same.
When I run a .bat file with the lines of code below, it works just fine. The .csv file gets updated with the most recent query's results.
sqlcmd -S SERVERNAME -i my_query.sql -s "," -o c:\scripts\My_query\query_results.csv -W -h-1
ftp.exe -s:"c:\scripts\My_query\file_upload.ftp"
Useful Information:
I am running the scheduled task as my user which is a server admin and has access to the server in question
I have tried giving explicit access to the user for full control of all files and folders involved
I have tried running C:\Windows\System32\cmd.exe as the action with the argument /c C:\scripts\My_query\my_process.bat to make sure the entire process runs
Task scheduler is working fine for other tasks, but it has this same problem with five similar processes
Are the tasks configured to run whether the user is logged on or not? If so, it will never find your SQL file because the default working directory is SYSTEM32 when you configure a scheduled task that way. You will need to provide the full path to the SQL file.

Automatically run .bat file after EC2 reboot without having to remote connect

I am running an AWS Windows 2012 EC2 instance that has to run 24/7. On this instance, I run a Python 3.6 scraper script and to prevent me from having to regularly check up on the server whether the file is running, I have a .bat file in the shell:startup folder of my instance, that automatically restarts it on a daily base. The .bat file works as it will run the Python script and set a timer to restart/reboot the instance after (t=86400). The .bat file runs on the EC2 instance itself.
However, what the file does not do is run automatically after the reboot. I now first have to remote connect to the server before the .bat file will run. What I want it to do is run without me having to first remote connect into the server. How can I achieve this?
I use the following code in my .bat file. Located on my EC2 instance.
#ECHO OFF
START CMD /K (
CD C:/Users/Administrator/Documents/
python scraper.py
)
START CMD /K SHUTDOWN -t 86400 -r -f
I have tried looking into using AWS' Automations and other schedule based methods but couldn't get that to work.
If you want to use something native to Windows Server 2012, look at Schtasks -- this is more or less the Windows equivalent of cron.
I found the answer to my question by using Task Scheduler and looking at the following article: Run a batch file with Windows task scheduler
An important note here is that for my batch file to run I had to create a task that started CMD and run the batch file from there. Asking Task Scheduler to run the batch file directly doesn't work on Windows Server 2012. I ran the task with the following details:
Administrator account
"Run whether user is logged on or not"
"Run with the highest privileges"
"Start on system start-up"
Action: Start a program -> CMD
Add arguments (optional): /c start "" "C:\Users\Administrator\Desktop\file.bat"
More information on how to do this can be found in this answer: https://stackoverflow.com/a/27055435/7736676

PowerShell script run using Windows Task Scheduler is not running a SQL Server SSIS DTEXEC.EXE job

Hopefully this question is unique enough not to be a duplicate. I have a PowerShell script which does two things.
Inserts records into a SQL Server table
Writes text to a text file
For the purpose of this post, I have simplified the script. On my computer, the script is located at C:\Temp\ssis.ps1. Following is the contents of the script.
DTEXEC.EXE /F "C:\Temp\ssisjob.dtsx"
$date = Get-Date
Write-Output "This PowerShell script file was last run on $date" >> C:\Temp\test.txt
When I manually run this PowerShell script, records are inserted into the SQL Server table, and a line of text is written to the test.txt file. If I schedule this script to run using Windows Task Scheduler, a new line of text is written to the text file, but the records are not inserted into the SQL Server table. This tells me that Windows Task Scheduler is able to run the PowerShell script. However, for some unknown reason, Windows Task Scheduler seems to not want to run the SSIS job (DTEXEC.EXE) part of the script. Event Viewer confirms there is an issue with the SSIS job. I am running Microsoft SQL Server 2014, Developer Version.
In my task, on the Actions tab, the Add arguments field has the following reference: C:\Temp\ssis.ps1. Task Scheduler is configured to run with the highest privileges.
I have tried all of the following Execution Policies in PowerShell. Regardless of the Execution Policy I select, my experience does not change.
Bypass
Unrestricted
RemoteSigned
The History tab in Task Scheduler has information events, but no error events.
I do not have the permission to view the SQL Server logs (this is a production server).
I have been debugging this issue for a few weeks, and I have read numerous posts here on Stack Overflow, yet I still cannot seem to find the answer, so hopefully I have done my due diligence before making a new post here. I could add some additional observations, but I do not want my post here to get extensively long. If anyone has any hints or tips or insight that might lead me down the right path, it would be greatly appreciated.
Here is the solution I came up with. Instead of exporting the file to Excel, I exported to a flat file (txt file). Also, using Nick McDermaids excellent recommendations, instead of using PowerShell in Task Scheduler, I started the dtexec.exe file in Task Scheduler.
Task Sheduler Actions Tab
Keep the action as Start a program
In Program/script, type dtexec.exe
In Add arguments, type /f "C:\path\to\example.dtsx
Leave the Start In box empty

create Scheduler task to invoke SQLCMD

I have a Requirement where i have to run a SQL Script in SQL server on a weekly basis .Result of this script has to be mailed.I want to automate this process.I don't want to go with SSIS Jobs. I have searched i have found few options like Creating a Windows Scheduler task to invoke a SQLCMD.Can Someone Assist on how to create Scheduler task to invoke SQLCMD
Your best bet would be to create a Console Application which executes the SQL Command and performs the email.
You can then use setup a task under Task Scheduler to use the Start a program option and run it on the schedule you prefer.
sqlcmd is just a command line tool. Put your SQL in a script file, and call it from the tool with right DB server and credentials. We can help you to figure how to make it work if you have a specific problem, but you should make your own tries before and tell us what goes wrong.
You will easily find examples on how to run a script :
http://msdn.microsoft.com/en-us/library/ms170572.aspx
More details on parameters :
http://msdn.microsoft.com/en-us/library/ms162773.aspx
example :
sqlcmd -S myServer\instanceName -i C:\myScript.sql -o C:\EmpAdds.txt
For task scheduler, it's easy to use but beyond SO scope, and it depends heavily of your version of windows, so (again), try yourself first.
Please note that you can also use SQL Server Agent to schedule your jobs.

is it possible execute a perl script with admin rights or as a specific user?

I'm writing a perl script in which I've to shutdown my mssql server ,do some operation and then I've to restart it.I know 1 way is to use netstat to stopt the service but I cann't use that. So I tried installing DBI and DBD::ODBC module.
More info here :Shutdown MSSQL server from perl script DBI
But when I trying to shutdown my server using this command
$dbh->prepare("SHUTDOWN WITH NOWAIT ");
It's not working for me :
I got this response from the community
SHUTDOWN permissions are assigned to members of the sysadmin and serveradmin fixed server roles, and they are not transferable. I'd consider it unlike(hopefully) that perl is run with this rights.
So please tell me is there a way to run the above command as these users ? or what can I do other than this . Note that I have a constraint tha tI cann't simply stop it as windows service.
If the scripts are executed through a web browser then the user executing the scripts will be defined by the web server. It will probably not be a good idea to fiddle with this user. Just leave things as they are.
What you can do is to create a Perl script that is being run by a privileged user on a consistent basis with CRON.
This script being run by CRON can check for specific content like a file which has been written by a script where the user executing the script has lesser privileges.
So the way it could work is as follows:
You execute browser.cgi through a browser to do a specific task.
browser.cgi writes instructions to a file.
Every 1 minute priveleged.cgi executes via CRON. (The root user could execute priveleged.cgi)
priveleged.cgi reads the file browser.cgi has written for instructions and starts and stops services according to the instructions.

Resources