SQLCMD.exe

# query in command line
sqlcmd -d db_name -Q 'select * from table_name' -W
 
# connnect by hostname and port number (1433)
sqlcmd -S tcp:hostname,1433 -U username -P password
 
# connnect by hostname and port number (1433) and use db (dbname)
sqlcmd -S tcp:hostname,1433 -U username -P password -d dbname
 
# input scripts
sqlcmd -S tcp:hostname,1433 -U username -P password -v name="my_new_db" -i db-create.sql
 
    db-create.sql:
    CREATE DATABASE $(name);
    GO
 
# input queries
sqlcmd -S tcp:hostname,1433 -U username -P password -d "my_db" -Q "SELECT * FROM my_table"