Skip to main content

SQL Server | sqlcmd cli

An optional way to connect into SQL Server is using the sqlcmd client. It's possible to create a interactive session or execute one-off commands.

Connection Configuration

NameTypeDescription
SERVER_ADDRESSenv-varThe Server Address of the SQL Server to connect into
USERenv-varThe user to connect in the SQL server
PASSenv-varThe password to connect in the SQL server
DBenv-varThe name of the database to connect into

Connection Command

sqlcmd -b -r -S$SERVER_ADDRESS -U$USER -P$PASS -d$DB

How to Use

Start an interactive session with sqlcmd client

hoop connect mssql

In the same connection, one-off process can be run as well

hoop exec mssql <<EOF
SELECT @@VERSION
GO
EOF
hoop exec mssql -f /tmp/myquery.sql
hoop exec mssql -i 'SELECT @@VERSION'
# pass arguments to sqlcmd
hoop exec mssql -- -s ";" -h -1 -W <<EOF
SELECT * FROM master.INFORMATION_SCHEMA.SCHEMATA
EOF

Be aware that allowing passing arguments grants capabilities to override flags defined in the connection. E.g.: hoop exec <conn> -- -Uoverrideuser