简体   繁体   English

具有多个更新的SQL Server(t-sql)存储过程

[英]SQL Server (t-sql) stored procedure with multiple updates

I have been writing a stored procedures. 我一直在写一个存储过程。 But now I'm unsure whether I'm doing it properly. 但现在我不确定我是否正确地做这件事。 I have this code: 我有以下代码:

BEGIN
    UPDATE tblPro 
    SET Email = @p_Email 
    WHERE ProID = @p_proId

    UPDATE tblVisits 
    SET VisitBrief = 'CONFIRMED' 
    WHERE VisitID = @p_visitId

    UPDATE Bookings 
    SET JobConfirmation = 1 
    WHERE BookingID = @p_bookingId

    IF @@ROWCOUNT > 0
    BEGIN
        INSERT INTO tblView (ViewLogType)
        VALUES ('Visit')
    END
    ELSE
        PRINT 'WARNING: Insert Failed' 
END

Is it better to have @@ROWCOUNT > 0 after every update statement just to make sure that all the statements are executed? 为了确保所有语句都已执行,在每个更新语句之后使用@@ROWCOUNT > 0更好吗?

Thanks for advice 谢谢你的建议

If you want to catch errors, you could use TRY/CATCH. 如果要捕获错误,可以使用TRY / CATCH。

If you want to save the rowcounts for later followup, you should log them for the statements that you are interested in saving. 如果要保存行数以备以后使用,则应将它们记录在有兴趣保存的语句中。 Rowcount could still be zero even if the statement is executed correctly (0 rows were affected). 即使正确执行了该语句,行数仍可能为零(受影响的行数为0)。

You can also use a transaction if you want to be able to rollback if one of the statements fails. 如果希望其中一条语句失败,则可以回滚,也可以使用事务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM