Executing Multiple Saved Queries at One Time

  • I am not sure if this is the right place for this question - but hopefully someone will be able to lead me in the right direction.

    I have a database which I am updating with new information daily - and as I go, I am saving the queries that I use to update.

    They are saved like: Update_tbl1.sql, Update_tbl2.sql, etc... and need to be run in a specific sequence.

    Is there some sort of wrapper I can write to execute them all sequentially even though I have them saved in separate files? It would be nice to execute one script and have it run ALL the updates.

    I found a blurb about writing a TRANSACTION, but was hoping I could call from the saved files rather than pasting in all the separate lines of code (as that would be quite cumbersome for editing purposes and if someone else wanted to decipher what was going on...)

    Thanks in advance!

  • You can add this in a batch file (.bat) to concatenate to a single file and use that for execution. For modification use the individual files.

  • use sqlcmd and a command file (batch file):

    sqlcmd -S myserver -d dbname -U user -P password -i file1.sql -o file1.log

    sqlcmd -S myserver -d dbname -U user -P password -i file2.sql -o file2.log

    sqlcmd -S myserver -d dbname -U user -P password -i file3.sql -o file3.log

  • Is there a way to enter command line arguments for the bat file at run time as well?

    Or is it possible to pass the result of one to the input of another?

    If so, what would the syntax be when running? Do you have to run from the command line, or is there a way to run from within the SQL Server Management Studio?

    Thanks again!

  • The typical batch issue

    .bat

    VBScript

    PowerShell

    Perl

    3rd party - RedGate Multi Script, xSQL Script Executor (free), etc..

    see this post from last week

    http://sqlblog.com/blogs/linchi_shea/archive/2008/12/04/executing-a-large-number-of-sql-scripts.aspx

    As far as I know, you need to customize it to make next script read the output from previous script

    SQLServerNewbieMCITP: Database Administrator SQL Server 2005

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply