• 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
    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
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2