You can use SQLCMD to do that.
As per BOL
Many sqlcmd options can be controlled in a script by using the setvar command. In the following example, the script test.sql is created in which the SQLCMDLOGINTIMEOUT variable is set to 60 seconds and another scripting variable, server, is set to testserver. The following code is in test.sql.
:setvar SQLCMDLOGINTIMEOUT 60
:setvar server "testserver"
:connect $(server) -l $(SQLCMDLOGINTIMEOUT)
USE AdventureWorks;
SELECT FirstName, LastName
FROM Person.Contact;
The script is then called by using sqlcmd:
sqlcmd -i c:\test.sql
You can look at BOL for more details