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

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?

Related

Run SQL Server generate script with data from a batch file

My goal is to script out a database with data like you used to with the sql publishing tool that older versions of SQL Server had
I am using SQL Server Express 2017, and I know how to perform this task manually by right clicking on the database->tasks->generate scripts
Is there a .exe being called here, in older versions of SQL Server this would call sqlpubwiz.exe, and you could call this .exe from a cli, and put into a batch file. I am looking to replace this behavior. Can someone let me know the exe that is being called, and let me know how to script against it.
you can use SQL Dumper, easiest way to export the script data (with Insert query)
for more information :
https://support.microsoft.com/en-in/help/917825/use-the-sqldumper-exe-utility-to-generate-a-dump-file-in-sql-server
to download the utility
https://download.cnet.com/SQL-Dumper/3000-10254_4-10514574.html

Run batch file from SQL

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.

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.

Copy .bak file to another Windows server using job MS SQL Server agent

I am trying to copy a .bak file nightly from Server A to Server B.
Can I do that using SQL server Job Agent to run this every night?
I am thinking of adding the copy command as a statement within a step of a job.
Something like: 'copy "G:\source\folder\" "\target\folder\"'
inside the step and setting the type to Operating System(CmdExec).
Is there a way to do it?
is this question about the command to copy the files?
If you want to copy entire folder use robocopy instead of copy
You can make a SSIS package to do that, and then run it from the SQL agent.
However, don't use logical drives, such as G: -- if the server doesn't have the same mapping, it won't work. Use the actual named servers: \serverA\source\folder to \serverB\target\folder.
Short answer is yes. You can try SSIS package as described here or here. Another option is to use windows task scheduler (vs using SQL Server Agent) and a simple bat script to do the same thing.

Batch file to open excel works on sql server agent , but sql server job never completes and goes in a loop

I have a batch file to open a spreasheet and run the auto open macro. This work . Putting the batch file on a sql server agent job, again it works but the job never seems to end . Any ideas why ?
Code for batch file
call C:\Imports\Account.xlsb
exit
code for sql server agent
C:\Windows\System32\cmd.exe /c "C:\Imports\Test\OpenExcelFile.bat"
I'm not sure why you would need to run cmd.exe to run a batch file. You should only need to specify the file name name in quotes while using an "Operating system (CmdExec) job step. The step should use the following code:
"C:\Imports\Test\OpenExcelFile.bat"
If this doesn't work, then try running the batch file from the xp_cmdshell stored procedure. Here's the code you would need to execute:
EXEC master.dbo.xp_cmdshell 'C:\Imports\Test\OpenExcelFile.bat';
GO
This could be called by a Transact-SQL script (T-SQL) job step.
Are you calling this in a SSIS package? The post was tagged as SSIS, but you never mentioned in the post that you tried to call this from a SSIS package. If this is a SSIS package, then are you able to run the SSIS package successfully in BIDS? If you are running this from BIDS, then you shouldn't need to call the cmd.exe file. There is an Execute Process Control Flow task that you could use that does not require running a batch file from cmd.exe. If you are not using a SSIS package, then can you remove the SSIS tag?

Resources