問題描述
我是 SQL 新手,嘗試使用 PowerShell 的 Invoke-SQLCmd
查詢 Azure SQL 數據庫(Adventureworks 示例).
I'm new to SQL, trying to query an Azure SQL Database (Adventureworks sample) using PowerShell's Invoke-SQLCmd
.
當我在 Powershell 中嘗試以下命令時:
When I try the following command in Powershell:
Invoke-Sqlcmd -Query "SELECT * FROM SalesLT.Customer;" -ServerInstance
"<servername>.database.windows.net" -Username <username> -Password <password>
我明白了:
Invoke-Sqlcmd : Invalid object name 'SalesLT.Customer'.
我是否需要指定該表所在的數據庫?即,SQLDatabse"而不是master"?但是當我將 Use SQLDatabase
添加到我的查詢時,它會引發另一個錯誤:
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.
注意:從 SQL Server Management Studio 執行這些查詢會得到沒有錯誤的表.
Note: Executing these queries from SQL Server Management Studio gets me the table with no errors.
謝謝!
推薦答案
在命令參數中添加一個-Database [dbname]
:
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
這篇關于Azure SQL:使用 Powershell 的“Invoke-sqlcmd"的無效對象名稱在 Adventureworks 上的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!