[英]How to run an Azure SQL Server Stored Procedure using an Automation Account Runbook with Managed Identity?
[英]Powershell Script in Azure Automation to run SQL Server stored procedure - possible timeout issue
我們使用 Azure 自動化按計划執行 Azure SQL Server 存儲過程。
這是其中一個腳本:
workflow ExecuteSP1
{
Write-Output "JOB START BEFORE INLINESCRIPT"
inlinescript
{
Write-Output "JOB START"
# Create connection to Master DB
$MasterDatabaseConnection = New-Object System.Data.SqlClient.SqlConnection
$MasterDatabaseConnection.ConnectionString = "Connection String"
$MasterDatabaseConnection.Open()
Write-Output "CONNECTION OPEN"
# Create command
$MasterDatabaseCommand = New-Object System.Data.SqlClient.SqlCommand
$MasterDatabaseCommand.Connection = $MasterDatabaseConnection
$MasterDatabaseCommand.CommandText = "dbo.StoredProcedure1"
Write-Output "DATABASE COMMAND TEXT ASSIGNED"
# Execute the query
$MasterDatabaseCommand.ExecuteNonQuery()
Write-Output "EXECUTING QUERY"
# Close connection to Master DB
$MasterDatabaseConnection.Close()
Write-Output "CONNECTION CLOSED"
}
Write-Output "WORK END - AFTER INLINESCRIPT"
檢查成功日志時,Azure 自動化會在每次運行時通知我們此腳本成功運行。 但是,在我們的 SQL Server 中產生的影響並未反映為已經發生。 所以我只能想象這意味着存儲過程超時了。 我已經確認,當我直接在 SQL Server 上運行 SP 時,它可以正確運行,但它確實需要很長時間才能運行。
有什么我可以添加到上述腳本中的內容:
在腳本中或在 SQL Server 端將任何超時期限增加到無限或至少 4 小時?
如果存儲過程沒有成功完成執行,是否會在 AA 中實際拋出錯誤?
你可以做幾件事。 首先,您可以指定命令超時:
$MasterDatabaseCommand.CommandTimeout = 18000
接下來,您可以使用 try catch 進行包裝,但我不確定 Azure 自動化將如何處理它:
try {
# Execute the query
$MasterDatabaseCommand.ExecuteNonQuery()
Write-Output "EXECUTING QUERY"
}
catch {
Write-Error $Error[0].Exception.Message
# or write to your log file or do what you need to here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.