Sql server Azure Automation中用于运行SQL Server存储过程的Powershell脚本-可能存在超时问题
我们使用Azure自动化按计划执行Azure SQL Server存储过程 以下是其中一个脚本:Sql server Azure Automation中用于运行SQL Server存储过程的Powershell脚本-可能存在超时问题,sql-server,powershell,azure-automation,Sql Server,Powershell,Azure Automation,我们使用Azure自动化按计划执行Azure SQL Server存储过程 以下是其中一个脚本: workflow ExecuteSP1 { Write-Output "JOB START BEFORE INLINESCRIPT" inlinescript { Write-Output "JOB START" # Create connection to Master DB $MasterDatabaseConnectio
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 Automation每次都会通知我们此脚本已成功运行。但是,SQL Server中产生的影响并没有反映为已经发生。所以我只能想象这意味着存储过程正在超时。我已经确认,当我直接在SQL Server上运行SP时,它运行正常,但运行确实需要很长时间
我是否可以在上述脚本中添加以下内容:
你可以做几件事。首先,您可以指定您的: 接下来,您可以使用try-catch,尽管我不确定Azure Automation将如何处理它:
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
}
只需将超时0添加到连接字符串
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
}