Azure devops powershell 在同一查询中给出与本地 powershell 不同的结果
Azure devops powershell gives different result than local powershell on the same query
我在项目中有这个 postDacPac 文件,在部署过程中 运行s。它有一个存储过程,有时会抛出错误。我创建了一个模拟存储过程,其结构如下:
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 文件中,我使用这样的存储过程:
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 中的所有管道设置,但没有发现差异。在这个范围内我可能遗漏了什么?
我在项目中有这个 postDacPac 文件,在部署过程中 运行s。它有一个存储过程,有时会抛出错误。我创建了一个模拟存储过程,其结构如下:
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 文件中,我使用这样的存储过程:
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 中的所有管道设置,但没有发现差异。在这个范围内我可能遗漏了什么?