Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Append output from SQLCMD in batch file Expand / Collapse
Author
Message
Posted Friday, June 12, 2009 4:06 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, May 17, 2012 3:32 PM
Points: 4, 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"

Post #734169
Posted Friday, June 12, 2009 4:20 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:40 AM
Points: 6,600, Visits: 8,901
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
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
Post #734172
Posted Friday, June 12, 2009 10:47 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:19 PM
Points: 37,075, Visits: 31,636
Leave the first "-o" alone. Change all of the other "-o" to ">>".

Old DOS warrior trick.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #734251
Posted Friday, June 12, 2009 11:42 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:40 AM
Points: 6,600, Visits: 8,901
Jeff Moden (6/12/2009)
Leave the first "-o" alone. Change all of the other "-o" to ">>".

Old DOS warrior trick.


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
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
Post #734260
Posted Saturday, June 13, 2009 11:40 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:19 PM
Points: 37,075, Visits: 31,636
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. Didn't know which way the OP wanted to go and I probably should have said so.


--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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #734396
Posted Saturday, June 13, 2009 6:03 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:40 AM
Points: 6,600, Visits: 8,901
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
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
Post #734483
Posted Sunday, June 14, 2009 9:23 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:19 PM
Points: 37,075, Visits: 31,636
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #734611
Posted Sunday, June 14, 2009 9:54 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 10:40 AM
Points: 6,600, Visits: 8,901
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
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
Post #734619
Posted Sunday, June 14, 2009 10:59 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 11:19 PM
Points: 37,075, Visits: 31,636
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #734634
Posted Sunday, June 14, 2009 12:28 PM
Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Yesterday @ 6:34 PM
Points: 750, Visits: 3,159
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.
Post #734663
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse