Azure自动化中的Powershell脚本以运行SQL Server存储过程-可能出现超时问题

我们使用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时,SP可以正常运行,但是确实需要很长时间才能运行。

我可以在上述脚本中添加以下内容吗?

  1. 在脚本中还是在SQL Server端上将任何超时期限增加到无限或至少4小时?

  2. 如果存储过程未成功完成执行,实际上会在AA中引发错误吗?

hubeihb 回答:Azure自动化中的Powershell脚本以运行SQL Server存储过程-可能出现超时问题

您可以做几件事。首先,您可以指定command timeout

$MasterDatabaseCommand.CommandTimeout = 18000

接下来,尽管我不确定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
}
本文链接:https://www.f2er.com/2889718.html

大家都在问