Strip unneeded info from the exported CSV file - sql-server

I'm using this batch script to export the result of SQL-script in a tab-delimited CSV.
sqlcmd -S PRISHA-CRM -i "C:\Users\Administrator\Desktop\test.sql" -U essl1 -P essl -o C:\outputfile.csv -s " "
How to prevent the second row ---------- from appearing in the exported file?
How to prevent the text in B4 cell from appearing in the exported file?
Tried multiple things but it's not working.

If you want to remove the dashed lines then pipe your output to the FINDSTR command and then redirect to the output file.
sqlcmd -S PRISHA-CRM -i "C:\Users\Administrator\Desktop\test.sql" -U essl1 -P essl -s " " | findstr /R /C:"^[^-]*$" >C:\outputfile.csv
Don't know anything about SQLCMD so I don't know why you are getting data in B4.

Related

Save CMD log to file

Is there any way to save all the console output from command.exe or powershell to a file at the end of a session rather than piping each individual output?
I'm trying to save the output of an application that I run from a batch script that crashes when redirected but works fine when printing to the terminal.
This is okay; all output prints to console:
C:\TestPlatform\executables> TestApp.exe -c off -d file -q otp 47f64
All of the below produce no output; Program returns after 1-2 seconds instead of the 15-20 it should:
PS C:\TestPlatform\executables> TestApp.exe -c off -d file -q otp 47f64 | tee LogFile.txt
PS C:\TestPlatform\executables> TestApp.exe -c off -d file -q otp 47f64 | out-file -append LogFile.txt
C:\TestPlatform\executables> TestApp.exe -c off -d file -q otp 47f64 > logFile.txt 2>&1
I couldn't reproduce however it is possible that redirection doesn't work for your exe because all text after the last switch is considered as arg to that switch so the exe consumes everything on the line till the last character - this would explain also why it doesn't run normally.
Try this alternative of logging in cmd:
C:\TestPlatform\executables>> logFile.txt 2>&1 TestApp.exe -c off -d file -q otp 47f64
Note the redirection character right after the prompt.

Whitespace differences with output

I have written a script to generate a .CSV file from an SQLCMD query, but when I open it in Notepad, there is unwanted spacing between data. When I manually paste the query results in the .csv file then there is no spacing when I open it in Notepad.
Please do let me know, what should I do to avoid the issue.
My minimal script example:
set destfolder="c:\Test"
SQLCMD -s "--Database name--" -d Audit -E -I -i "C:\Test\Mandates.sql" -s "," -o "c:\Test\tempfile.csv"
findstr /v /c:"---" "c:\Test\tempfile.csv" > "%destfolder%\%filename%"
del "%destfolder%\tempfile.csv"
Issue:
CustomerName,CustomerNumber,Value
Adam, 123456789, 0
Expected:
CustomerName,CustomerNumber,Value
Adam,123456789,0
I got the solution.
SQLCMD -s "--Database name--" -d Audit -E -I -W -i "C:\Test\Mandates.sql" -s "," -o "c:\Test\tempfile.csv"
I have used -W in the above script which resolves the issue. Thanks

Using sqlcmd in a batch script to run multiple scripts with output files

I need to run about 50 scripts in a folder using sqlcmd from a batch file. Each script's query results need to be sent to its own output file. I have a working batch file that just runs each from a separate line:
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%SCRIPTFOLDER%\master_departments.sql" -s "|" -o "%OUTPUTFOLDER%\master_departments.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%SCRIPTFOLDER%\master_companies.sql" -s "|" -o "%OUTPUTFOLDER%\master_companies.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P %PASSWORD% -i "%SCRIPTFOLDER%\bill_history.sql" -s "|" -o "%OUTPUTFOLDER%\bill_history.csv" -W
sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P %PASSWORD% -i "%SCRIPTFOLDER%\episodes.sql" -s "|" -o "%OUTPUTFOLDER%\episodes.csv" -W
Is there any way to run this in some kind of loop? I've seen examples that run a loop of all SQL scripts in a folder, but nothing that I've seen does it with an output file set.
Per #LotPings' suggestion I used the below code:
set INSTANCE=<someinstance>
set DATABASE=<somedb>
set USERNAME=<someuser>
set PASSWORD=<somepassword>
set "SCRIPTFOLDER=D:\<pathToScripts>\"
set "OUTPUTFOLDER=D:\<pathForOutput>\"
#Echo off
For /F "tokens=*" %%S in ('Dir /B "%SCRIPTFOLDER%*.sql" '
) do echo sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
#pause
I ran that in a batch file and when it paused, the last line said, "The system cannot find the file specified."
Thinking it was perhaps the backslashes in my paths, I removed them and put a slash before the .sql in the for line, but I got the same results.
Removing the backslash altogether resulted in a "File not found" message when I ran it like that.
In case your output file name matches the script name (without extension)
and your parameters are the same for all scripts
#Echo off
For /F "tokens=*" %%S in ('Dir /B "%SCRIPTFOLDER%*.sql" '
) do echo sqlcmd -S %INSTANCE% -d %DATABASE% -U %USERNAME% -P "%PASSWORD%" -i "%%~fS" -s "|" -o "%%~dpnS.csv" -W
The echo in front of sqlcmd prevents execution and allows to review the output. If all looks OK, remove the echo.
The for variable behaviour can be changed with ~ modifiers, see For /? or visit ss64.com/nt/for.html / syntax-args
To pass a folder to the batch you can input via set /P or hand over via command line arguments.

How to append batch file command to a strings(file names) using batch file

I'm in a process of creating a batch file to list names of all SQL scripts in a folder to a text file. My plan to convert this text file to a batch file so that I can use it to execute the scripts in the server. So I would like to have the following string appended before each file name while creating the initial text file
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
This is a batch file command and I would like to be appended before the each file names.
eg:
sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i 001_ALTER_PERSON.sql
The code I'm using is
set MSSQLSERVER_NAME = "%MSSQLSERVER_NAME%"
set %MSSQLSERVER_DBNAME = "%MSSQLSERVER_DBNAME%"
set myvar=sqlcmd -S %MSSQLSERVER_NAME% -d %MSSQLSERVER_DBNAME% -i
for /r . %%g in (*.sql) do echo %myvar% %%~nxg >> D:\test.txt
pause
Out put I'm getting
sqlcmd -S -d -i 015_ALTER_vBOARD_PAPERS.sql
Let me know how to tackle this
set myvar=sqlcmd -S %%MSSQLSERVER_NAME%% -d %%MSSQLSERVER_DBNAME%% -i
should cure your problem - % escapes %
note that spaces ARE significant in variable names, so set var = somethingwill set var[space] to [space]something
set %varname%=... is rare -it sets the variablename (contents of varname).
Even more rare to have unbalanced %... not sure that will work at all...

running a bunch of SQL files in a folder

I have a bunch of scripts in a folder which I have to run in order. I just open it up in SSMS and hit execute one by one. I have to wait for one of them to complete then run the second one.
Is there a way for me to create a batch script or a script in SSMS (if its possible) that will go through the files one by one? This would save me time as I can do something else while the scripts are running and prevent error (most of them rely on temp tables created in succession).
I think a batch script with sqlcmd is what I am looking for but no idea how to accomplish this task.
Thank you.
Very easy to put together a batch file for this. Put something like the following into a text file but end the file with .bat, eg 'ExecuteMyScripts.bat'
Sqlcmd takes various arguments but the main ones are -S for server instance, -d for database, -i for input file ie your SQL file, -U for user, -P for password, and -o for output file. Watch the case of the letters.
So open notepad and add the following:
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script1.sql" -o "c:\script1log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\script2.sql" -o "c:\script2log.txt"
IF ERRORLEVEL 1 goto :eof
sqlcmd -S mysqlserver -d mydb -U sa -P pass1 -i "c:\scripr3.sql" -o "c:\script3log.txt"
Save the file as ExecuteMyScripts.bat.
Double click the file and both script will be executed.
Adding 'IF ERRORLEVEL 1 goto :eof' after each sqlcmd call will jump to the end of the script if error found.
Full usage found here, http://msdn.microsoft.com/en-us/library/ms162773.aspx

Resources