Sql-server – Encrypt connection when using sqlcmd

azure-sql-databasedbachecksdbatoolssql serversqlcmd

Trying to connect database with an encrypted connection, tried below sql queries:

sqlcmd -N -E -S tcp:SQLSERVER,1234 -U username -P password -d Database -Q "INSERT INTO table1 VALUES(1,2,2)

ERROR: Sqlcmd: The -E and the -U/-P options are mutually exclusive.

sqlcmd -N -S tcp:SQLSERVER,1234 -U username -P password -d Database -Q "INSERT INTO table1 VALUES(1,2,2)

ERROR Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : SSL Provider: The target principal name is incorrect. Sqlcmd: Error:
Microsoft ODBC Driver 13 for SQL Server : Client unable to establish
connection

sqlcmd -S tcp:SQLSERVER,1234 -U username -P password -d Database -Q -E -N "INSERT INTO table1 VALUES(1,2,2)

Sqlcmd: '-Q': Missing argument. Enter '-?' for help.

Am I missing something here?

Best Answer

The first one failed because the -E flag is for Trusted Connections (Windows Auth) and -U/-P flags are for SQL Authentication. These are mutually exclusive and cannot be combined.

The third one failed because -Q must be followed immediately by the query you wish to execute. In your provided code, you've included other flags between -Q and the actual query.

The second one should work syntactically, so you need to investigate the cause of the

The target principal name is incorrect

error. Take a look at some of the answers to this question and also validate that you can connect via SSMS, or you get the same error in SSMS.