Execute SQL script from command prompt

We are often required to run certain scripts or deploy scripts to sql server using command prompt; here how we can do it seemlessly.

Open command prompt and type in the below sqlcmd mentioning the relevant servername /database name and sql script location.

C:> sqlcmd -E -S<Servername> -d<DatabaseName> -i<SQLScript.sql>

-E : windows authentication

-S : To specify Server Name

-d : To specify Default database name

-i : To Specify the .sql script file location

Alternately, you can execute this from SSMS in a new query window as :

xp_cmdshell ‘sqlcmd -E -S<Servername> -d<DatabaseName> -i<SQLScript.sql>’

note: xp_cmdshell is disabled by default on sql server 2008 for security lock down.

This entry was posted in SQL Server, T-SQL Scripts, Tricks and tagged , . Bookmark the permalink.

Leave a Reply