SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Append output from SQLCMD in batch file


Append output from SQLCMD in batch file

Author
Message
carissa.chris
carissa.chris
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 18
I have a batch file that runs all the sql scripts in a specified folder. The output is written to output.txt. However, each time a sql script is executed, the output.txt file is overwritten. Is there a way to append to the output.txt file?

Here is my batch file:

for %%a in ("E:\Source\APP_30\SQL\Structure\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"

for %%a in ("E:\Source\APP_30\SQL\SP\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"

for %%a in ("E:\Source\APP_30\SQL\Triggers\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"


WayneS
WayneS
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9729 Visits: 10569
carissa.chris (6/12/2009)
I have a batch file that runs all the sql scripts in a specified folder. The output is written to output.txt. However, each time a sql script is executed, the output.txt file is overwritten. Is there a way to append to the output.txt file?

Here is my batch file:

for %%a in ("E:\Source\APP_30\SQL\Structure\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"

for %%a in ("E:\Source\APP_30\SQL\SP\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"

for %%a in ("E:\Source\APP_30\SQL\Triggers\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"



Well, BOL clearly states that the output file will be overwritten.
I suggest you make the output filename dynamic:

for %%a in ("E:\Source\APP_30\SQL\Structure\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\%%aoutput.txt"



Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84811 Visits: 41069
Leave the first "-o" alone. Change all of the other "-o" to ">>".

Old DOS warrior trick. :-P

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
WayneS
WayneS
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9729 Visits: 10569
Jeff Moden (6/12/2009)
Leave the first "-o" alone. Change all of the other "-o" to ">>".

Old DOS warrior trick. :-P


Ahem. It's not very often that someone gets to tell Jeff that he's wrong, AND can prove it...


for %%a in ("E:\Source\APP_30\SQL\Structure\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"
for %%a in ("E:\Source\APP_30\SQL\SP\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"
for %%a in ("E:\Source\APP_30\SQL\Triggers\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"



Jeff's suggestion is to change it to:

for %%a in ("E:\Source\APP_30\SQL\Structure\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a -o "E:\output.txt"
for %%a in ("E:\Source\APP_30\SQL\SP\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a >> "E:\output.txt"
for %%a in ("E:\Source\APP_30\SQL\Triggers\*.sql") do SQLCMD -S ServerName -d DatabaseName -E -i %%a >> "E:\output.txt"




The first line is processing each .sql file in the "Structure" directory. For each file, it runs the SQLCMD, which will have the -o parameter, overwriting the output file if it already exists. So if there are 3 .sql files in this directory, the output will only have the output from the third file.
It's not until you get to the second "for" line that Jeff's suggestion will start appending the results to the file.

Edit: added this suggestion:
if you were to start off with:
echo %date% %time% > E:\output.txt
you could then replace ALL of the -o with >>

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84811 Visits: 41069
WayneS (6/12/2009)
Ahem. It's not very often that someone gets to tell Jeff that he's wrong, AND can prove it...


Heh... wrong for what? If you want to keep the log forever, then yes, you need to replace the first "-o" with the ">>". But if you want the log to be only for the batch, the leave the first "-o" as it is. :-P Didn't know which way the OP wanted to go and I probably should have said so. Hehe

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
WayneS
WayneS
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9729 Visits: 10569
Okay, here's some code to demonstrate what I'm talking about.

Save this code as a batch file, and run it:

for %%a in ("%windir%\*.exe") do SQLCMD -S ServerName -d master -E -Q"set nocount on;select '%%a'" -o "%temp%\output.txt"
type %temp%\output.txt
pause
cls
echo %date% %time% > "%temp%\output.txt
for %%a in ("%windir%\*.exe") do SQLCMD -S ServerName -d master -E -Q"set nocount on;select '%%a'" >> "%temp%\output.txt"
type %temp%\output.txt
pause

del %temp%\output.txt



At the first pause, the contents of the output file are displayed. All that is in it is the last *.exe.
At the second pause, the contents of the output file are again displayed. The prior contents are erased, and all exe files are listed.
This demonstrates the point the OP was making.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84811 Visits: 41069
WayneS (6/13/2009)
Okay, here's some code to demonstrate what I'm talking about.

Save this code as a batch file, and run it:

for %%a in ("%windir%\*.exe") do SQLCMD -S ServerName -d master -E -Q"set nocount on;select '%%a'" -o "%temp%\output.txt"
type %temp%\output.txt
pause
cls
echo %date% %time% > "%temp%\output.txt
for %%a in ("%windir%\*.exe") do SQLCMD -S ServerName -d master -E -Q"set nocount on;select '%%a'" >> "%temp%\output.txt"
type %temp%\output.txt
pause

del %temp%\output.txt



At the first pause, the contents of the output file are displayed. All that is in it is the last *.exe.
At the second pause, the contents of the output file are again displayed. The prior contents are erased, and all exe files are listed.
This demonstrates the point the OP was making.


You've added a line of code the OP didn't have in the original problem and that new line is the one responsible for the incorrect operation.

echo %date% %time% > "%temp%\output.txt



You've neglected to use ">>" in that line of code and that's what resets file. ;-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
WayneS
WayneS
SSCrazy Eights
SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)SSCrazy Eights (9.7K reputation)

Group: General Forum Members
Points: 9729 Visits: 10569
Jeff Moden (6/14/2009)
You've added a line of code the OP didn't have in the original problem and that new line is the one responsible for the incorrect operation.

echo %date% %time% > "%temp%\output.txt



You've neglected to use ">>" in that line of code and that's what resets file. ;-)


Correct, I reset the file between the runs, to demonstrate the effects of the two different methods. If the OP wants the file reset between runs, then use this line. If not, then get rid of it.

But the point was that with using the "-o" in the first part, the file is reset for every file being processed. Which is the behavior that the OP was trying to get rid of.

Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
Links: For better assistance in answering your questions, How to ask a question, Performance Problems, Common date/time routines,
CROSS-TABS and PIVOT tables Part 1 & Part 2, Using APPLY Part 1 & Part 2, Splitting Delimited Strings

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)SSC Guru (84K reputation)

Group: General Forum Members
Points: 84811 Visits: 41069
WayneS (6/14/2009)
Jeff Moden (6/14/2009)
You've added a line of code the OP didn't have in the original problem and that new line is the one responsible for the incorrect operation.

echo %date% %time% > "%temp%\output.txt



You've neglected to use ">>" in that line of code and that's what resets file. ;-)


Correct, I reset the file between the runs, to demonstrate the effects of the two different methods. If the OP wants the file reset between runs, then use this line. If not, then get rid of it.

But the point was that with using the "-o" in the first part, the file is reset for every file being processed. Which is the behavior that the OP was trying to get rid of.


Understood. My point is that I wasn't sure if the OP was going for a continuous "forever" log or just wanted a single log with the results of all the individual steps for a single run of the batch. I posted the solution for the latter. It wasn't wrong, it just wasn't what you expected. :-)

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
matt stockham
matt stockham
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1128 Visits: 3178
Jeff Moden (6/14/2009)
Understood. My point is that I wasn't sure if the OP was going for a continuous "forever" log or just wanted a single log with the results of all the individual steps for a single run of the batch. I posted the solution for the latter. It wasn't wrong, it just wasn't what you expected. :-)


I agree with Wayne. Your method wouldn't give a "forever" log or the results of all steps ... if there were 2 files for example, the results of the first iteration of the first step would be lost as the second iteration would overwrite the log.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search