sqlcmd goto 不太工作—结束脚本错误

发布于 2024-08-03 03:20:37 字数 869 浏览 9 评论 0原文

我正在使用 sqlcmd 工具和 SQL Server 来执行脚本。脚本添加/更新记录。如果脚本抛出错误,我需要 sqlcmd 停止执行并给出非 0 返回值。我认为以下内容会起作用,但事实并非如此。

    DECLARE @intErrorCode INT
BEGIN TRAN T1;

SET IDENTITY_INSERT dbo.SomeTable ON 

INSERT INTO dbo.SomeTable(some_type_id,some_type,code_definition,use_some_lookup,created_by,created_on,modified_by,modified_on,org_some)
VALUES(0,'yadayada','None','N','system',GETDATE(),'system',GETDATE(),'N')

 SELECT @intErrorCode = @@ERROR
    IF (@intErrorCode <> 0) GOTO PROBLEM

SET IDENTITY_INSERT dbo.SomeTable OFF


UPDATE dbo.SomeTable 
SET some_type = 'Contract Analytical'
WHERE some_type_id = 2

 SELECT @intErrorCode = @@ERROR
    IF (@intErrorCode <> 0) GOTO PROBLEM



PROBLEM:
IF (@intErrorCode <> 0) BEGIN
PRINT 'Unexpected error occurred!'
    ROLLBACK TRAN
    SELECT @intErrorCode
    RETURN
END
COMMIT TRAN T1;

I am using the sqlcmd tool with SQL Server to execute scripts. The scripts add/update records. I need sqlcmd to stop executing and give a non 0 return value if the script throws an error. I thought the following would work but it does not.

    DECLARE @intErrorCode INT
BEGIN TRAN T1;

SET IDENTITY_INSERT dbo.SomeTable ON 

INSERT INTO dbo.SomeTable(some_type_id,some_type,code_definition,use_some_lookup,created_by,created_on,modified_by,modified_on,org_some)
VALUES(0,'yadayada','None','N','system',GETDATE(),'system',GETDATE(),'N')

 SELECT @intErrorCode = @@ERROR
    IF (@intErrorCode <> 0) GOTO PROBLEM

SET IDENTITY_INSERT dbo.SomeTable OFF


UPDATE dbo.SomeTable 
SET some_type = 'Contract Analytical'
WHERE some_type_id = 2

 SELECT @intErrorCode = @@ERROR
    IF (@intErrorCode <> 0) GOTO PROBLEM



PROBLEM:
IF (@intErrorCode <> 0) BEGIN
PRINT 'Unexpected error occurred!'
    ROLLBACK TRAN
    SELECT @intErrorCode
    RETURN
END
COMMIT TRAN T1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恋竹姑娘 2024-08-10 03:20:37

在错误批量中止时使用 -b 启动 sqlcmd 错误时停止的选项。

you start sqlcmd with the -b on error batch abort option to stop on error.

浮云落日 2024-08-10 03:20:37

如果您想要一个非零返回值,您可以在 @intErrorCode

ala

RETURN @intErrorCode

中返回该值,选择它会在结果集中返回该值而不是返回值。

If you want a non-zero return value you can return the value in @intErrorCode

ala

RETURN @intErrorCode

Selecting it returns it in a result set rather than return value.

雄赳赳气昂昂 2024-08-10 03:20:37

尝试使用 try-catch:

DECLARE @intErrorCode INT
BEGIN TRY
    BEGIN TRAN T1;

    SET IDENTITY_INSERT dbo.SomeTable ON 

    INSERT INTO dbo.SomeTable (some_type_id,some_type,code_definition,use_some_lookup,created_by,created_on,modified_by,modified_on,org_some)
        VALUES(0,'yadayada','None','N','system',GETDATE(),'system',GETDATE(),'N')

    SET IDENTITY_INSERT dbo.SomeTable OFF


    UPDATE dbo.SomeTable 
        SET some_type = 'Contract Analytical'
        WHERE some_type_id = 2

END TRY
BEGIN CATCH

    IF XACT_STATE()!=0
    BEGIN
        ROLLBACK TRANSACTION
    END

    SELECT
        ,ERROR_NUMBER() AS ErrorNumber --same as @@ERROR
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage

    PRINT 'Unexpected error occurred!'
    RETURN 999

END CATCH
COMMIT TRAN T1;
RETURN 0

try using try-catch:

DECLARE @intErrorCode INT
BEGIN TRY
    BEGIN TRAN T1;

    SET IDENTITY_INSERT dbo.SomeTable ON 

    INSERT INTO dbo.SomeTable (some_type_id,some_type,code_definition,use_some_lookup,created_by,created_on,modified_by,modified_on,org_some)
        VALUES(0,'yadayada','None','N','system',GETDATE(),'system',GETDATE(),'N')

    SET IDENTITY_INSERT dbo.SomeTable OFF


    UPDATE dbo.SomeTable 
        SET some_type = 'Contract Analytical'
        WHERE some_type_id = 2

END TRY
BEGIN CATCH

    IF XACT_STATE()!=0
    BEGIN
        ROLLBACK TRANSACTION
    END

    SELECT
        ,ERROR_NUMBER() AS ErrorNumber --same as @@ERROR
        ,ERROR_SEVERITY() AS ErrorSeverity
        ,ERROR_STATE() AS ErrorState
        ,ERROR_PROCEDURE() AS ErrorProcedure
        ,ERROR_LINE() AS ErrorLine
        ,ERROR_MESSAGE() AS ErrorMessage

    PRINT 'Unexpected error occurred!'
    RETURN 999

END CATCH
COMMIT TRAN T1;
RETURN 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文