0

I use docker-compose.yml to load my SQL Server image inside a container. After it's up and running, I create a command.sh shell and try to run it to create a database.

# command.sh
echo 'creating database from ->' $ModuleName
export query="'create database $ModuleName'"
echo $query
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P my_strong_password -Q $query

And it gives me this error:

Sqlcmd: 'database': Unknown Option. Enter '-?' for help.

Please note that I can't use -i switch to use an input .sql file, because I'm creating my queries programmatically in shell based on environment variables.

3
  • Does output of sqlcmd -? (on linux) state that you should use double quotes? If so, try to replace the single quotes (') for escaped double quotes (\"). query="\"create database $ModuleName\"" Commented Jun 25, 2021 at 17:18
  • 1
    second option could be to create a temporary sql-file (i.e. /tmp/piepeloi.sql) with the commands in it that you need to run. Commented Jun 25, 2021 at 17:20
  • @Luuk, both of your comments helped me. Can you post them as answer so that I can accept them? Commented Jun 26, 2021 at 4:35

1 Answer 1

1

The output of sqlcmd -? shows how to use then -Q option. On windows this says [-Q "cmdline query" and exit].

But Windows and Linux differ (or are not consistent) in the use of single- or double quotes.

First option is to try: sqlcmd -Q "\"create database $ModuleName\""

Second option is:

  • Create a temporary file (i.e. /tmp/tmp.sql), and put the SQL statement in that script.
  • Use -i /tmp/tmp.sql to execute that script.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.