SQLCMD error in Batch file. Invalid Filename - sql

echo off
if "%4" == "" goto usage
cd %3
mkdir Logs
echo Logs can be found in %3\Logs
echo
goto %4
:smmdsp
echo Updating %2..xyz table on %1
sqlcmd -S %1 -E -p -e -d %2 -i "%3\xyz.SQL" > %3\Logs\xyz.SQL.log
goto done
:usage
echo.
echo Usage
echo.
echo where SqlServer = SQL Server name
echo Database = configuration database
echo InstallPath = path to the software update files
echo Feature = Feature to update
echo.
:done
This script works if i have the batch file in C drive C:\XYZ , but if there are spaces in the path or if its in program files 86 folder C:\Program Files (x86) .It Gives out a error that says
How to handle this scenario... ?? Thank you

OK, This is the answer.
In my multiple decades of experience as a professional programmer, I have learned that most bugs that are not easily solved are not difficult because the problem itself is difficult, but because of human nature we make assumptions about the elements of our code that are simply incorrect, but we cannot "see" it because of that innate assumption. To compound that error in our own behavior, once we discover the error in our thought process we tend to be too embarrassed to tell others what the source of the issue was.
At this point in my career... I have nothing left to prove, and am more concerned with saving others the pain and waste of valuable coding time.
This is one of those insidious things that is due mostly to Microsoft being presumptuous, and removing file extensions from view by default. ...grrr I left my xanax at home today.
What I have done here, by my assuming that the file name I was trying to use as a parameter to the sqlcmd call was ... -i c:\temp\myscript.sql
and I was completely wrong. The file I was trying to reference was
c:\temp\myscript.sql.txt
but Microsoft in their attempt to try to help me removed the actual extension from view in file explorer and fooled me into thinking I had the correct file name. I didn't. Once I realized that I corrected the file name, which then made my code reference correct, and everything worked perfectly.

Try with strings :
sqlcmd -S "%1" -E -p -e -d %2 -i "%3\xyz.SQL" > %3\Logs\xyz.SQL.log

Related

Running sqlcmd in batch file works but running the same batch file as a scheduled task works and does nothing

I have looked at many SO questions/answers and though some seem similar to my issue they do not seem to be. The answers given fix issues the questions were asking about but will not solve my issue.
I have a batch file...
#ECHO ON
ECHO Disabling the following... >> C:\App\Debug.log
ECHO - V1 >> C:\Apps\Debug.log
FOR /F "tokens=* USEBACKQ" %%F IN (`sqlcmd -j -S DOMAIN\SQLSERVER -U username -P password -d DBNAME -Q "UPDATE [DBNAME].[dbo].[table1] SET ColOne='V1_OFF' WHERE ColOne='V1'"`) DO (
Echo %%F >> C:\Apps\Debug.log
)
EXIT /B
When I run this file at the command prompt it works perfectly fine. When I run it as a scheduled task it show me the echos but nothing for the for loop as expected.
Yes I have made sure the username (using whoami) is the same for the scheduled task set up as the manual run that I do.
Yes I know the user running the script has rights to everything (file access as well as DB access) because it works fine running it from the command prompt.
Scheduled task is set to run wither user is logged on or not.
Any ideas what might be wrong or what I can try for debugging purposes?
Thanks!
sqlcmd is perhaps not enough. cmd.exe in environment of scheduled task may fail to find the executable using local PATHEXT and local PATH environment variables. The executable should be specified with full qualified file name, i.e. drive + path + name + extension. Then the batch file does not anymore depend on the environment variables PATH and PATHEXT because of all files are referenced with full qualified file name.
for executes the specified command line with starting in background one more command process with %ComSpec% /c and the specified command line appended. This means executed is following with Windows installed on drive C::
C:\Windows\System32\cmd.exe /c sqlcmd -j -S DOMAIN\SQLSERVER -U username -P password -d DBNAME -Q "UPDATE [DBNAME].[dbo].[table1] SET ColOne='V1_OFF' WHERE ColOne='V1'"
for captures everything written to handle STDOUT of started command process. The lines of captured output are processed line by line by for after started cmd.exe terminated itself. Error messages output by started cmd.exe or the commands/executables executed by Windows command processor in background to handle STDERR are redirected to handle STDERR of command process processing the batch file and printed to console. But there is no console window on running a batch file as scheduled task. So error messages cannot be seen in this case.
The for command line can be modified easily here to get also error messages written into the C:\Apps\Debug.log.
FOR /F "tokens=* USEBACKQ" %%F IN (`sqlcmd -j -S DOMAIN\SQLSERVER -U username -P password -d DBNAME -Q "UPDATE [DBNAME].[dbo].[table1] SET ColOne='V1_OFF' WHERE ColOne='V1' 2^>^&1"`) DO (
The Microsoft article Using command redirection operators explains 2>&1. The two operators > and & must be escaped with ^ to be interpreted as literal characters on Windows command processor parsing the for command line before executing finally for which executes next %ComSpec% /c with the specified command line on which 2^>^&1 is changed already to 2>&1.
Does the log file C:\App\Debug.log contain with this modification following two lines?
'sqlcmd' is not recognized as an internal or external command,
operable program or batch file.
Yes, then no executable with file name sqlcmd is found by started cmd.exe. The best solution is referencing this executable with full qualified file name. See also: What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
Otherwise sqlcmd outputs perhaps an error message which should be now also in the log file C:\App\Debug.log.
It would be also possible to use following command line to let background cmd.exe write the error messages into a separate error log file C:\App\Error.log:
FOR /F "tokens=* USEBACKQ" %%F IN (`sqlcmd -j -S DOMAIN\SQLSERVER -U username -P password -d DBNAME -Q "UPDATE [DBNAME].[dbo].[table1] SET ColOne='V1_OFF' WHERE ColOne='V1'" 2^>C:\App\Error.log`) DO (
"tokens=* usebackq" results in first deleting all leading horizontal tabs and normal spaces on non-empty lines by for, then checking if the remaining line starts with ; in which case the line is also ignored and finally assigning the captured line not starting with ; and with leading tabs/spaces removed to loop variable F for further processing.
Better would be using the options usebackq^ delims^=^ eol^= not enclosed in double quotes which requires escaping the two spaces and the two equal signs with caret character ^ to be interpreted as literal characters by cmd.exe on parsing the command line before executing for. The line splitting behavior is disabled completed with delims= because of the definition of an empty list of delimiters. And no line except an empty line is ignored anymore because of end of line character modified from default ; to no character.
Finally a space on an echo line left to redirection operator >> is also output by echo and for that reason written as trailing space into the log file. Therefore no space should be used left to > or >> on printing a line with echo redirected into a file. But care must be taken on omitting the space character left to the redirection operator. The word left to redirection operator should not be 1, 2, ..., 9 as this would result in redirecting the output to these numbered handles into the specified file instead of the character 1, 2, etc. So if unknown text should be written into a file, it is better to specify first the redirection operator > or >> and the full qualified file name and next the echo command with the text to output. See also: Why does ECHO command print some extra trailing space into the file?
The three command lines with echo would be for this batch file:
ECHO Disabling the following...>> C:\App\Debug.log
ECHO - V1>> C:\Apps\Debug.log
>>C:\Apps\Debug.log ECHO %%F
following... is safe for being correct written into the file as also V1. %%F could be just 1 or a string ending with a space and a single digit and so it is better to specify the redirection first on the last echo command line to get finally executed by cmd.exe the command line ECHO %%F 1>>C:\Apps\Debug.log.

Unable to run a postgresql script from bash

I am learning the shell language. I have creating a shell script whose function is to login into the DB and run a .sql file. Following are the contents of the script -
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
echo "Running SQL Dump - auto_qa_db_sync"
\\i auto_qa_db_sync.sql
After running the above script, I get the following error
./autoqa_script.sh: 39: ./autoqa_script.sh: /i: not found
Following one article, I tried reversing the slash but it didn't worked.
I don't understand why this is happening. Because when I try manually running the sql file, it works properly. Can anyone help?
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production and run script"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT -f auto_qa_db_sync.sql
The lines you put in a shell script are (moreless, let's say so for now) equivalent to what you would put right to the Bash prompt (the one ending with '$' or '#' if you're a root). When you execute a script (a list of commands), one command will be run after the previous terminates.
What you wanted to do is to run the client and issue a "\i ./autoqa_script.sh" comand in it.
What you did was to run the client, and after the client terminated, issue that command in Bash.
You should read about Bash pipelines - these are the way to run programs and input text inside them. Following your original idea to solving the problem, you'd write something like:
echo '\i auto_qa_db_sync.sql' | $DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
Hope that helps to understand.

How can I cat back exact formatting regardless of shell?

While trying to write a script, I found an interesting issue with cat today. If I do the following at the command line, everything works properly:
var=$(ssh user#server "cat /directory/myfile.sh")
echo $var > ~/newfile.sh
This works and I have a script file with all the proper formatting and can run it. However, if I do the EXACT same thing in a script:
#!/bin/sh
var=$(ssh user#server "cat /directory/myfile.sh")
echo $var > ~/newfile.sh
The file is mangled with carriage returns and weird formatting.
Does anyone know why this is happening? My goal is to ultimately cat a script from a server and run it locally on my machine.
EDIT
I now know that this is happening because of my invoking #!/bin/sh in my shell script. The command line works because I'm using zsh and it is preserving the formatting.
Is there a way to cat back the results regardless of the shell?
As you seem to have figured out, word splitting is off by default on zsh, but on in sh, bash, etc. You can prevent word splitting in all shells by quoting the variable:
echo "$var" > ~/newfile.sh
Note that echo appends a newline to its output by default, which you can suppress (on most echo implementations and builtins) with -n.

Need help to write bat file that execute sql scripts in (sql server 2008 and another 3 files.?

I am sure these has been asked before but cannot find clear instruction how to create a
batch file lets call it "Update Database" this batch file should
Execute sql scripts located in different folders
Execute another 3 bat files.
Any quick examples how to do it?Never done it before
thanks a lot
EDITED
Can I do this?
:On Error exit
:r C:\myPath\MasterUpdateDatabase.bat
GO
SQLCMD -S (Local) -i C:\myPath\InsertUsername.sql
I get an error:
"GO" is not recognized as internal external command
Thanks for any input
It looks like you're trying to use DOS commands to create a batch file that either (a) executes other batch files or (b) executes SQLCMD to run sql or a sql script.
Here are a couple examples all rolled into one. I'm using the DOS command START with the /WAIT switch, which will keep your original "master" batch file running in one window and execute the subsequent file or commands in a new window. That new window stays open until the script finished AND exits.
Some of the ECHOs probably aren't required, but the script will talk back to you for now, a little.
#echo off
So, this is pretty simple in the sense that you're just running the script. If you're script1.bat has break points, you can return an error back to the main script and have it end immediately. I wasn't clear if that was what you needed the master script to do.
echo Starting Database Update.
echo.
echo Excuting Script 1
echo.
start /wait C:\path\to\your\script1.bat
echo If there was a problem, break here.
Pause
echo Excuting Script 2
echo.
start /wait C:\path\to\your\script2.bat
echo If there was a problem, break here.
Pause
Here is where did used the same START /WAIT to run SQLCMD, which in this case just returns results from the query. One thing to note here is that the -Q (uppercase) runs the query and quits. If you use -q (lowercase) it will run the query and sit open in SQLCMD waiting for another query.
echo.
echo Running SQLCMD: "select top 100 * from sys.objects"
start /wait sqlcmd -S (local) -Q "select top 100 * from sys.objects"
And this is how you can run a sql script, which is what the -i denotes, but I also didn't run this in the START /WAIT as earlier. Not that you have to, but I wanted to show both examples. What this also shows is the -b will end the batch process if your script returns an error, which is useful if you're running multiple scripts that depend on success of the former(s).
echo.
echo Running SQLCMD from an (-i)nput file:
sqlcmd -S (local) -i C:\path\to\your\script.sql -b
echo.
echo Update Complete.
pause
End
So, I assumed you were looking for a .bat or .cmd file that utilized SQLCMD. The example I provided is pretty basic, but hopefully it sets you on the right path.
OH! And remember that CTRL+C breaks a batch script in process.
The actual error you're seeing is that the command line interpreter does not recognize 'GO', so you could just remove that line.
Hope this helps you :
sqlplus UserName/Password#DataBase #C:\myPath\InsertUsername.sql
P.S : Don't forget to add the command "commit;" at the end of sql file (InsertUsername.sql), this command order Oracle to save performed changes in darabase
This answer definitely works for your purposes:
sqlcmd -S localhost -U fdmsusr -P fdmsamho -i "E:\brantst\BranchAtt.sql" -o "E:\brantst\branchlog.txt"

sqlcmd runs script but script does not affect database

So we have a .bat file that runs SQL scripts, e.g.
#ECHO --- 03_Case6395_Publication.sql --- >> dbupt.log
sqlcmd -U %1 -P %2 -S %3 -d %4 -i 03_Case6395_Publication.sql -k -b >> dbupt.log
IF ERRORLEVEL 1 GOTO ErrorTag
The script runs and gives no errors, but the script doesn't actually affect the database. In the example above, here is what is being run:
IF NOT EXISTS (SELECT 1 FROM [dbo].[syscolumns] WHERE [NAME] = N'MandatoryInList' AND [ID] = object_id(N'Pub_Type'))
BEGIN
ALTER TABLE [dbo].[Pub_Type] ADD [MandatoryInList] bit NULL CONSTRAINT [DF_PubType_MandatoryInList] DEFAULT (0)
END
ELSE
BEGIN
ALTER TABLE [dbo].[Pub_Type] ALTER COLUMN [MandatoryInList] bit NULL
END
GO
The script is pretty simple and you would expect it to add a column called MandatoryInList, however it doesn't. The odd thing is there are no syntax errors in the script and when I run it from SQL Server Management Studio, it runs fine. There's no issues with permissions or which user is running the script because there are other scripts that run fine in the batch file.
I should also mention that this runs from a GUI by creating a new process and running it but not showing the command window. But regardless, I've been running the tool like this for months without any issues.
Any ideas?
So it works from Management Studio but not from your batch file that calls sqlcmd. Strange...
I don't know what could cause this, but here are some ideas:
If you run the same "sqlcmd" command manually from the command prompt, rather than from your batch file, does it work?
If you run the batch file from the command prompt, rather than invoking it from the GUI app, does it work?
What's it writing to your dbupt.log file? Does it have the header "--- 03_Case6395_Publication.sql --- ", and it's empty after that?
Can you add some PRINT statements to the script, so you can see whether it's executing the IF or the ELSE part?
Can you try redirecting the error stream to your log file as well, using the "2>&1" operator? E.g.:
sqlcmd -U %1 -P %2 -S %3 -d %4 -i 03_Case6395_Publication.sql -k -b >> dbupt.log 2>&1
Can you try changing "IF ERRORLEVEL 1" to "IF NOT ERRORLEVEL 0"?
Have you tried using SQL Profiler to see what SQL is actually executing? Does anything show up?