首页 > 解决方案 > Azure devops powershell 在同一查询中给出与本地 powershell 不同的结果

问题描述

我在项目中有这个 postDacPac 文件,它在部署过程中运行。它有一个存储过程,有时会引发错误。我创建了一个模拟存储过程,它具有以下类似的结构:

CREATE PROCEDURE #tmpSproc
AS
BEGIN
    BEGIN TRANSACTION
        print 'Enter sproc';
        IF @@TRANCOUNT > 0
        BEGIN
            print 'Rollback in sproc.'
            ROLLBACK TRANSACTION
        END;
        THROW 60000, 'Temp error.', 0

END
GO

在 postDacPac 文件中,我像这样使用这个 sproc:

CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
BEGIN TRANSACTION
GO

IF 1=1 /* It is called in an if condition so I kept the structure same. */
BEGIN
    EXEC #tmpSproc;
END
GO

IF @@ERROR <> 0 AND @@TRANCOUNT > 0
BEGIN
    PRINT N'Rollback after sproc.'
    ROLLBACK TRANSACTION;
END

IF @@TRANCOUNT = 0
BEGIN
    PRINT N'Creating error entry'
    INSERT  INTO #tmpErrors (Error)
    VALUES                  (1);
    BEGIN TRANSACTION;
END
ELSE
    PRINT N'sproc succeeded or skipped.'
GO

IF EXISTS (SELECT * FROM #tmpErrors)
BEGIN
    PRINT N'Rollback after deployment.'
    ROLLBACK TRANSACTION
END
GO
IF @@TRANCOUNT>0 BEGIN
PRINT N'success'
COMMIT TRANSACTION
END
ELSE PRINT N'fail'
GO
DROP TABLE #tmpErrors
GO
DROP PROCEDURE #tempSproc
GO

我用这个脚本在powershell上运行它:

Invoke-Sqlcmd -ConnectionString "myConnectionString"  -Inputfile "C:\***\PostDacPac.sql" -QueryTimeout 6000 -Verbose -ErrorVariable errors 

在 powershell 上,它按我的预期工作。即使存储过程抛出错误并回滚,查询仍会持续到结束并且打印脚本失败。

但是,当我在 azure devops 管道 (vsts) 上尝试此部署时,如果 sproc 失败,部署也会失败。这是 azure powershell 调用此文件的方式:

Invoke-Sqlcmd -ServerInstance "***" -Database "***" -Username "***"  -Password ****  -Inputfile "D:\***\PostDacPac.sql" -QueryTimeout  6000 -ConnectionTimeout 120

我试图检查 azure devops 中的所有管道设置,但找不到差异。在这个范围内我可能会缺少什么?

标签: azurepowershellazure-devopsazure-pipelinesazure-pipelines-build-task

解决方案


事实证明,在查询超时后,脚本再次运行。显然,这是一个已知问题。还有几个与此问题相关的问题,如下所示:https ://social.msdn.microsoft.com/Forums/sqlserver/en-US/d4167226-2da7-49ec-a5c2-60e964785c2c/powershell-invokesqlcmd-calls-stored-查询超时后的过程第二次超时?forum=sqldatabaseengine


推荐阅读