Search code examples
sql-serversqlcmd

How to execute other file from script body in sqlcmd?


How I can execute other file in ms sql script for sqlcmd?

In sqlplus I can write something as:

@@DBS\other_scr1.sql
@@DBS\other_scr2.sql

promt OK

Solution

  • To run the script file

    Open a command prompt window. In the Command Prompt window, type:

            sqlcmd -S myServer\instanceName -i C:\myScript.sql
    

    Press ENTER. A list of Adventure Works employee names and addresses is written to the command prompt window.

    To save this output to a text file Open a command prompt window. In the Command Prompt window, type:

        sqlcmd -S myServer\instanceName -i C:\myScript.sql -o C:\EmpAdds.txt
    

    Press ENTER.

    If you want to execute that through SQL Script.

    Use xp_cmdshell which will ultimetly SQLCMD. Thanks