locked
Run multiple .sql files from a Folder simultaneously. RRS feed

  • Question

  • Hi there,

    I have the .sql files in a folder in my system. I need to run multiple .sql files from a Folder simultaneously into SSMS(Sql Server Management Studio), rather than opening each file and executing it.

    Thanks,

    KHAN

    Thursday, December 26, 2013 9:56 AM

Answers

  • Unfortunately no, If you want to run them parallel. If you want them in serial, you could run a For LOOP inside a .bat file and call sqlcmd

    Something like this should work

    for /r %%i in (*.sql) do {
    echo %%i
    REM CALL sqlcmd HERE
    }


    Satheesh
    My Blog


    • Marked as answer by Sofiya Li Thursday, December 26, 2013 11:46 AM
    Thursday, December 26, 2013 10:26 AM
  • You can try sqlcmd then

    http://technet.microsoft.com/en-us/library/ms162773.aspx

    You may need to pass these parameters 

    -S [protocol:]server[\instance_name][,port

    -d db_name

    -U login_id (you could use -E for trusted connections )

    -P password

    -i input_file[,input_file2...]


    Satheesh
    My Blog


    • Marked as answer by KHAN_IRK Thursday, December 26, 2013 10:51 AM
    Thursday, December 26, 2013 10:12 AM

All replies

  • Do you want to run them using SSMS? SQLCMD will be an alternative option

    Satheesh
    My Blog


    Thursday, December 26, 2013 10:04 AM
  • Anything would be fine Satheesh. I just need to execute multiple files simultaneously.

    KHAN.

    Thursday, December 26, 2013 10:08 AM
  • You can try sqlcmd then

    http://technet.microsoft.com/en-us/library/ms162773.aspx

    You may need to pass these parameters 

    -S [protocol:]server[\instance_name][,port

    -d db_name

    -U login_id (you could use -E for trusted connections )

    -P password

    -i input_file[,input_file2...]


    Satheesh
    My Blog


    • Marked as answer by KHAN_IRK Thursday, December 26, 2013 10:51 AM
    Thursday, December 26, 2013 10:12 AM
  • Thanks for the reply Satheesh.

    But do I have an option where it will pick the .sql files from the folder?(Bcoz, I have around 100 sql files to be run).

    OR do I have to place each of the file name like sql1.sql,sql2.sql,sql3.sql,....

    And how do we specify the file path also? something like this...

    sqlcmd -S "qhos-csce-win2" -d "QHOSClient1" -U "htracs" -P "htracs" -i "E:\Workspace_QHOS_Master\hos\latest\sql\proceduresisp_CarrierHistory_Purge.sql"

    I got the invalid filename error for the above command.

    Thanks,

    KHAN



    • Edited by KHAN_IRK Thursday, December 26, 2013 10:27 AM
    Thursday, December 26, 2013 10:21 AM
  • Unfortunately no, If you want to run them parallel. If you want them in serial, you could run a For LOOP inside a .bat file and call sqlcmd

    Something like this should work

    for /r %%i in (*.sql) do {
    echo %%i
    REM CALL sqlcmd HERE
    }


    Satheesh
    My Blog


    • Marked as answer by Sofiya Li Thursday, December 26, 2013 11:46 AM
    Thursday, December 26, 2013 10:26 AM
  • Thanks a lot Satheesh. You ROCK!

    KHAN!

    Thursday, December 26, 2013 10:51 AM