batch file - sqlcmd failing in scheduled tasks (0x2331) - sql-server

I'm trying to use scheduled tasks to run a database back up once per day using sqlcmd that points to a SQL script.
If I open a command prompt and run the code in the batch file everything is successful but when task manager tries it gets a 0x2331 error.
A search reveals it's something to do with permissions - maybe I need to change the path to the sql file?
here is the script:
ECHO OFF
ECHO This is running to backup a database
sqlcmd -S COMPNAME -i C:\backup.sql
GO
ECHO Success!
EXIT
I'm new to batch files to tried to keep it as simple as possble.

Related

Batch file that runes SQL commands

I want to create a batch file that runs some SQL commands for example:
I'm signing in using these commands:
Sqlcmd -S <Server Location> -U <user> -P <password>
Next is going to be a backup for some database
BACKUP DATABASE <data base> TO DISK=<Location>
GO
EXIT
But after starting the .bat file, log in is successfully but the backup database is not working, I still need to type the commands.
There is a way to actually run those commands without typing in the commands?
Or should I try using something else, like Windows PowerShell?

Using SSIS execute process task to run a batch file

I was tasked to execute a batch file using SSIS "execute process task".
FYI,The batch file(named test.bat) uses PsExec command to call a runthisboy.bat file on a remote webserver to refresh the appPool. Here is what I wrote in the test.bat:
psexec -d -i -s \\webserver1 -u domainName\myID -p myPassword "C:\Documents\runthisboy.bat"
After I run the SSIS package, there is an error message shows up:
[Execute Process Task] Error: In Executing "C:\Windows\System32\cmd.exe" "/c C:\Users\myName\Desktop\test.bat" at "", The process exit code was "1" while the expected was "0".
But if I use this SSIS package to run other .bat file, like:
xcopy /d /y "C:\Users\myName\Desktop\sample.txt" "C:\documents"
It runs without any problems.
If anyone can give me some clue about what is happening here, I will be so thankful!

SQL Server agent for scheduling SFTP using WinSCP under SSIS

I have a batch script which generates a WinSCP upload script to upload a file to an SFTP location. Now when I run the batch file via command prompt - it runs successfully and loads it. I called the same thru SSIS Execute process task - it runs successfully and loads it. Now when I put the same on SQL Agent - I tried the following two options:
Using Operating System (CmdExec) - cmd.exe /c "\.bat"
Added the SSIS package to SSISDB and added it as a job step.
With both the above options the job showed a successful run. However the file is not uploaded! Any ideas on what is happening?
Here's my batch script:
echo off
SET winscp=C:\"Program Files (x86)"\WinSCP\WinSCP.com
SET stagingDirectory=\\<staging path>\
SET scriptPath=\\<ScriptPath>\UploadScript.txt
SET ftpHost=xx.xx.xx.xx
SET ftpUser=user
SET ftpPass=password
SET fileName=Test.xlsx
SET ftpFlags=
#REM ftpFlags: -explicit
echo deleting uploadScript if it already exists
IF EXIST %scriptPath% del /F %scriptPath%
IF EXIST %scriptPath% exit 1
echo Generating WINSCP Upload Script
>>%scriptPath% echo option batch abort
>>%scriptPath% echo option confirm off
>>%scriptPath% echo open sftp://%ftpUser%:%ftpPass%#%ftpHost% %ftpFlags%
>>%scriptPath% echo option transfer binary
>>%scriptPath% echo put %stagingDirectory%%fileName% /
>>%scriptPath% echo close
>>%scriptPath% echo exit
echo Launching WINSCP upload
start /wait %winscp% /console /script=%scriptPath%
As you start the WinSCP via the start (why?), the exit code is not propagated to the SSIS. So, you never learn, if the script fails. And it most probably fails.
You also should enable logging, so that you can see what's wrong.
You should use this code to propagate the WinSCP exit code to SSIS and to enable logging:
%winscp% /log=\\<ScriptPath>\UploadScript.log /script=%scriptPath%
exit /b %ERRORLEVEL%
(Note that the winscp.com does not have the /console parameter)
Anyway, one clear problem is that you do not specify an expected SSH host key in the script. When you run the script manually, you probably have the key cached in the registry of your Windows account. But under the SSIS a different account is used, and its host key cache is likely empty. You should add the -hostkey switch to the open command in the script to make the script independent on the cache. See Where do I get SSH host key fingerprint to authorize the server?
When testing the script, add the /ini=nul parameter to isolate the script from your configuration.
For this and other hints, when debugging WinSCP running under SSIS, see My script works fine when executed manually, but fails or hangs when run by Windows Scheduler, SSIS or other automation service. What am I doing wrong?
And finally, see WinSCP SFTP Task for SSIS.
Your variable seems set incorrectly. To manage with a space in the path and into the variable you have to put in quotes the whole path or the whole variable.
i.e.
set "winscp=C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start "%winscp%"
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
or
set winscp="C:\Program Files (x86)\WinSCP\WinSCP.com"
echo start %winscp%
:: output: start "C:\Program Files (x86)\WinSCP\WinSCP.com"
Another point, you have to check this file: UploadScript.txt because your script adds new lines rather than remake the file.
change this line to >%scriptPath% echo option batch abort instead of >>%...
Ah, I did not pay attention to the IF EXIST.

mysqldump batch script runs but leaves empty file

I have made a simple batch script to back up a database, however when it runs it creates a empty file. This batch script is located in the same folder where mysqldump.exe is located. Following code:
#ECHO off
start /wait mysqldump.exe -u user -p "password" database > "Z:\sql-backup\"database-backup.sql
Any help would really help. New to Batch scripting.
I had used the below commands in batch file and it has worked properly.
First of all you should goto bin directory and issue the below mentioned mysqldump command. The same you can mention in the batch file as it is.
cd C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin
mysqldump -uroot -ppassword database > "C:\ProgramData\MySQL\MySQL Server 5.6\Data\"Database_bkp.sql

Running SQLCMD from a batch file after SQLServer Install

I've come across an odd problem, which I'm sure is easily explained and fixed.
I'm using a .bat file to install a lot of programs, one of which is SQL server 2012 (if it matters, the other installations are just .net framework, OPOS drivers and POS software which uses SQL server).
I can get the batch file to silently instal everything, including SQL (with SSMS), which is great. But once SQL Server is installed I'd like to run a couple of SQL scripts to create/attach databases.
I know that the install works (if I dont' try to doanything other than the installs), and I know that the SQLCMD works (if I run it seperately, after the install), but if I try to run the SQLCMD after the install, in the same batch file it fails with the standard 'SQLCMD is not recognised as an internal or external command...'
I've put this down to I need to restart CMD to get it to recoginise the new command (i.e. the SQLCMD), so I figured I'd split out the SQLCMD commands into a separate batch file and call it, but it still doesn't work. I have to physically close my original batch file down before CMD.exe picks up the new commands.
So... is it possible to 'refresh' cmd.exe so that the newly installed SQLCMD commands are useable from the original batch file??
Here is (some of) my script. (note that I've removed all of the other install before SQL Server)
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
"%~dp0SQLServer.exe" /QS /INDICATEPROGRESS /ACTION=install /FEATURES=SQL,SSMS /INSTANCENAME=Datasym /SECURITYMODE=SQL /SAPWD=Welcome21ST /IACCEPTSQLSERVERLICENSETERMS
echo.
echo SQL Server installed
sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
The %script% variable changes (like I said, there are multiple scripts I'd like to run). And I know that the scripts themselves work.
Why does CMD not recognise SQLCMD as a command??
I tried to replace the line:
sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
with
call sqlscript.bat
as I thought that would open another CMD,exe (which it does), so I assumed that the second CMD would see the ew command (SQLCMD), but it doesn't seem to .
Any help on how I can get the SQLCMD to work in the original batch file would be greatly appreciated.
As you've already found, the current CMD window won't pick up the new changes to the system PATH environment variable. If for some reason you don't want to put the full path to sqlcmd (which I think is the best solution), you might try doing this instead.
start /wait cmd /c sqlcmd -S %computername%\DATASYM -U sa -P Welcome21ST -i %script%
This will spawn a new window and wait for the sqlcmd call to finish before returning to your original script.

Resources