Run custom script in SQL Server Express - sql-server

I am trying to run a script on Task Scheduler to update a column in my database every 1 hour.
I have table vw_invoices and a column ExportLock with a default value of 0 which is "unlocked".
I want to run a query to update ExportLock to 1. Currently I am running
update Worldwide.dbo.vw_Invoices
set ExportLocked = 1
I really appreciate your help.

USE SQLCMD along with windows task scheduler to mimic SQL Agent on SQL Server Express:
sqlcmd
-S <ComputerName>\<InstanceName>
-i <c:\MyScript.sql>
-o <c:myutput.txt
Keep the above in a notepad and save it as .BAT file and schedule through task scheduler

I found what I was looking for and here is what I did worked great, I appreciate everyone's help

Related

Run Teradata SQL Assistant via a batch script

I am trying to Run a teradata SQL script via a batch script and export the details.
I have managed to execute the whole thing in DOS which is great
Im just not sure how to package it up
Below is my very average attempt. COuld anyone point me in the right direction?
The SQL is SELECT DATE
#title Optional Title
#echo off
C:\'Program Files (x86)'\Teradata\Client\15.00\Teradata SQL Assistant\
Sqla -c DB_NAME -f "C:\Temp\test.sql" -e "c:\Temp\test_output.log"
Thanks
I found the answer on another different question on Stack Overflow
How do I execute cmd commands through a batch file?
This basically does what i need or at least points me in the correct direction
Thank you for your time

Automate Running SQL Queries in SSMS

I have a SQL Server database set up that I manage using SQL Server Management Studio 17.
In that database, I have 27 tables that I maintain by running pretty simple OPENQUERY scripts every morning, something to the effect of:
DROP TABLE IF EXISTS [databasename].[dbo].[table27]
SELECT * INTO [databasename].[dbo].[table27] FROM OPENQUERY(OracleInstance, '
SELECT
table27.*
FROM
table27
INNER JOIN table26 ON table27.criteria = table26.criteria
WHERE
< filter >
< filter >
');
And this works great! But, it is cumbersome to every morning, sign into SSMS, and right click on my database and hit "New Query" and copy in 27 individual SQL scripts and run them. I am looking for a way to automate that. My directory that holds these scripts looks like this:
I don't know if this is achievable in SSMS or in like a batch script. I would imagine for the latter, some pseudocode looking like:
connect to sql server instance
given instance:
for each sql_script in directory:
sql_script.execute
I have tried creating a script in SSMS, by following:
Tasks -> Script Database ->
But there is no option to execute a .sql file on the tables in question.
I have tried looking at the following resources on using T-SQL to schedule nightly jobs, but have not had any luck conceiving of how to do so:
https://learn.microsoft.com/en-us/sql/ssms/agent/schedule-a-job?view=sql-server-2017
Scheduled run of stored procedure on SQL server
The expected result would be the ability to automatically run the 27 sql queries in the directory above to update the tables in SQL Server, once a day, preferably at 6:00 AM EST. My primary issue is that I cannot access anything but SQL Server Management Studio; I can't access the configuration manager to use things like SQL Server Agent. So if I am scheduling a task, I need to do so through SSMS.
You actually can't access the SQL Server Agent via Object Explorer?
This is located below "Integration Services Catalog"
See highlighted below:
You describe not being able to access that in the question for some reason. If you can't access that then something is wrong with SQL Server or perhaps you don't have admin rights to do things like schedule jobs (a guess there).
In SSMS you would wnat to use Execute T-SQL Statement Task and write your delete statement in the SQL Statement field in the General Tab.
However, I would look at sqlcmd. Simply make a batch script and schedule it in Task Scheduler (if you're using windows). Or you could use
for %%G in (*.sql) do sqlcmd /S servername /d databaseName -E -i"%%G"
pause
From this post. Run all SQL files in a directory
So basically you have to create a Powershell script that calls and execute the sql scripts.
After that you can add your Powrshell script to the Task Scheduler.
I suggest you add these scripts as jobs for the SQL Server Agent.

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.

auto run batch file containing db2 statements in windows

I want Task Scheduler in Window run DailyJob.bat daily at a certain time, this file having content like this:
#echo off
DB2 CONNECT TO dbName USER usrName USING password
DB2 .........
DB2 ..........
Moreover, Task Scheduler will run this file by cmd.exe automatically, but cmd doesn't understand DB2 command. Please help me, thanks !
You need to call your db2 statements with the db2cmd command (http://www-01.ibm.com/support/knowledgecenter/SSEPGG_10.5.0/com.ibm.db2.luw.admin.cmd.doc/doc/r0002036.html?cp=SSEPGG_10.5.0%2F3-6-2-6-35)
From CMD.exe
db2cmd -i -c db2 list node directory
There are a lot of related questions in the Web about this problem.

Executing a stored procedure using Windows task Scheduler

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.

Resources