Use sqlcmd cli tool

sqlcmd utility (2019)

Installed with Visual Studio 2019. (Apparently also with Sql Server 2019, but they dont’s explain WHERE it actually is :S)

Add to path

sqlcmd.exe is located in:

Run a script

sqlcmd -S [SERVER] -i C:\setupscripts\createStockUser.sql -o C:\setupscripts\createStockUser.log
# e.g.
sqlcmd -S localhost -i C:\setupscripts\createUser.sql -o C:\setupscripts\createUser.log

Open CLI for queries

sqlcmd -S localhost -U sa -P password -d database
> select * from transactions
> go

Run sqlcmd from docker/kubernetes and connecto to host’s localhost sql server

docker run --rm -it mcr.microsoft.com/mssql-tools
sqlcmd -S host.docker.internal -U [USER] -P [PASSWORD] -d [DATABASE]
>select * from transactions
> go

-S host.docker.internal => sets the server to host.docker.internal, where host’s localhost can be accessed from inside the container.

Or in kubernetes

kubectl run disposable-sql --image=mcr.microsoft.com/mssql-tools -it --rm -- bash
sqlcmd -S [IP],[PORT] -U [USER] -P [PASSWORD] -d [DATABASE]
>select * from transactions
> go