Azure Automation中的Powershell脚本运行SQL Server存储过程-可能发生超时问题

问题描述 投票:0回答:1

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

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

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

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

sql-server powershell azure-automation
1个回答
0
投票

您可以做几件事。首先,您可以指定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
}
© www.soinside.com 2019 - 2024. All rights reserved.