sql-servercommand-linesqlcmd

How to I run a single sql command from the sqlcmd prompt?


In SQL Server, if I want to run a script from the command line, I can do this

/opt/mssql-tools/bin/sqlcmd -S $DB_HOST -U $DB_USER -P $DB_PASS -d $DB_NAME -i myscript.sql

Is it possible to run just a single command without a script and get the results? For instance, if I just wanted to run

SELECT 1

How would I do that from the command line?


Solution

  • I think you want the -q switch:

    sqlcmd -S localhost -U MyUser -P MyPass -d MyDb -q "SELECT 1"
    

    Documentation is here