3

I'm new to SQL, trying to query an Azure SQL Database (Adventureworks sample) using PowerShell's Invoke-SQLCmd.

When I try the following command in Powershell:

Invoke-Sqlcmd -Query "SELECT * FROM SalesLT.Customer;" -ServerInstance 
"<servername>.database.windows.net" -Username <username> -Password <password>

I get:

Invoke-Sqlcmd : Invalid object name 'SalesLT.Customer'.

Do I need to specify what Database that table is under? Namely, "SQLDatabse" instead of "master"? But when I add Use SQLDatabase to my query it throws another error:

USE statement is not supported to switch between databases. Use a new     
connection to connect to a different database.

Note: Executing these queries from SQL Server Management Studio gets me the table with no errors.

1 Answer 1

3

Add a -Database [dbname] to the command parameters:

Invoke-Sqlcmd -Query "SELECT * FROM SalesLT.Customer;" 
    -ServerInstance "<servername>.database.windows.net" 
    -Username <username> -Password <password> -Database <dbname>

https://msdn.microsoft.com/en-us/library/cc281720.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

No prob. I've never actually used it, but any time you connect to a server to query, it needs a reference to which database, and a quick look at the docs showed the param (servers can (and do by default) have multiple databases).

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.