Run batch file from SQL - sql-server

I want to run batch file from SQL Job without using exec xp_cmdshell.
Any idea?
Thanks

You could use a SQL Server Job, otherwise i cannot think of a way you could without xp_cmdshell.
Take a look at this

I want to run batch file from SQL Job without using exec xp_cmdshell.
Any idea?
Worth to mention that you can also leverage SQLCLR.
Example: CLR Stored procedure to execute command
Some other googlable threads:
How to execute a DOS command when xp_cmdshell is disabled in SQL Server
Executing an external process() in SQLCLR Project
Such approach introduces severe risks like memory leaks, crashing of underlying .net app pool etc
Therefore another link: Security in the CLR World Inside SQL Server

Instead of running batch file, i have created power shell and ran it from SQL job. It satisfy my requirement and resolved my issue.

Do it like the picture: like this image.
The drive containing the batch file should be other than the C drive, to avoid trouble.
Add execute, read and write permissions for the user, which you are using to run the batch file, to get the username run this query: EXEC master..xp_cmdshell 'whoami', get the name after the \ sign. For example "nt service\mssqlserver". Add permission for this user: mssqlserver
Finally make sure you put the batch file on the same server as where you execute your Job.

Related

SQL Server to check for files in directory

I was wondering if it is possible for SQL Server to check a directory for files and run a stored procedure. I did some research and found this, but I am wondering if there is a way to do what I want WITHOUT SSIS.
EDIT: After reading my post, I realized I should have been more specific. Is there a way to AUTOMATICALLY or set SQL Server to check for files in a directory and run a stored procedure?
You can use xp_cmdshell to run file related commands. To get a directly listing:
exec xp_cmdshell 'dir *.csv';
You can also use bulk insert to load a file from disk into a table and take actions based on the loaded contents.
Normally you'd use the File Watcher Task with SSIS. But you can also use SQL Server Agent to schedule a task for periodic execution, schedule a task with Windows Task Scheduler, or configure a stored procedure to runs at startup with sp_procoption that pauses (using waitfor) between processing times.

Is it possible to upload ftp files from SQL server without xp_cmdshell?

Ideally would like to run something from a SQL query or SQL agent job to FTP upload a file to an external site, but cannot use xp_cmdshell.
Yes. You need to split your work into two separate tasks:
How to run executable or a batch program from within SQL Server without resorting to xp_cmdshell.
An example of how to do it can be found in:
https://www.mssqltips.com/sqlservertip/2014/replace-xpcmdshell-command-line-use-with-sql-server-agent/.
You should modify this example to suit your particular needs. Suggested stored procedure would:
run command passed as a parameter in created on-the-fly SQL job (indicate CmdExec subsystem)
wait for SQL job completion (query msdb.dbo.sysjobactivity) or kill the job if predefined timeout value has been reached
return results of job execution (query msdb.dbo.sysjobhistory)
delete the job
Note: Full version of SQL Server required. If you are using express version, you would have to manually define a windows scheduled task.
How to send a file via ftp using a batch program.
Please see:
How to ftp with a batch file?

How to run a stored procedure as Siebel Server Job

Can I define a Server Job as Oracle Database Procedure?
I will need to schedule running a database procedure using Siebel Server Jobs.
Thanks in Advance.
No you cannot do that. If you are asking something like triggers that is a separate thing.
There is currently no Direct Method to run a DB stored procedure. The common way of implementing this is through the command line and calling SQL Plus. In this tutorial it's explained in a step-by-step way. This will allow you to create a business service and use it in a workflow that can then be used to power a Siebel Job.
You can do this way on Unix servers:
1.Create a SQL script file on Siebel server(s) depending on your component definition on a particular server.
exec my_package.MY_STORED_PRC;
2.Call the file inside your BS:
Clib.system(“sqlplus $my_user/$my_password#dbname #//SBA_81/siebsrvr/bin/my_stored_proc_caller.sql”);
3.Define an RCR template, calling the BS/WF RunProcess:
4.Create RCR with scheduling time & start it.
You can also call the sql file inside a shell script file & invoke shell script file
Shell script file: my_shell_file.sh
sqlplus -S $my_username/$pwd#db_instance "#"my_stored_proc_caller.sql"
Give execute permission to the file.
Then execute it inside BS:
Clib.system(“/sieb/server/path/my_shell_file.sh")
Hi Please try this option by executing OOB BS "EAI ODBC Service" method "Execute Procedure". Hope it will help you

Use of xp_cmdshell in SQL Server

I am trying to execute a perl script from xp_cmdshell.
The output of the perl script is a csv file, but when I run
EXEC master..xp_cmdshell N'perl G:\script\perl.pl';
I can't find the csv file created, though the xp_cmdshell command seems to run fine, the output is the name of file that has to be created.
I am using xp_cmdshell to create a job step to execute the perl script.
Any help would be appreciated.
Since you're running this via a SQL Agent job, it'll be much safer to disable the use of xp_cmdshell via sp_configure (ref1 | ref2) and use a CmdExec job step instead.
When configuring the job step, be sure to go to the advanced page and enable job step logging to a table.
This will allow you to better troubleshoot the issues you're having with the perl job in general, as the issue could be related to something entirely outside the context of the database engine.

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