删除记录的存储过程似乎不起作用

时间:2015-03-20 16:43:51

标签: sql-server stored-procedures access-vba

我遇到了访问表单的问题。我使用访问2013表单作为前端,并使用sql server 2014作为后端。表单有一个按钮,应该通过使用存储过程删除记录。但是,当我选择应该删除的记录时,当我按下按钮时,它似乎已经起作用了。存储过程必须删除2个不同表格中的1条记录,这些表格与“物品”链接在一起。

我可以调试整个过程(存储过程除外)。我猜测我的存储过程存在问题:

ALTER PROCEDURE [dbo].[spArticleDelete]
(
    @articlenr int
)
AS
BEGIN TRANSACTION
    IF (
    SELECT COUNT(*)
    FROM Article
    WHERE articlenr = @articlenr
    ) <> 0
    BEGIN
        DELETE FROM Articleprice WHERE articlenr = @articlenr
        DELETE FROM Article WHERE articlenr = @articlenr
    END

    If @@ERROR <> 0
        BEGIN
            COMMIT TRANSACTION
            Raiserror('The article has been deleted!', 16, 1)
        END
    ELSE
        ROLLBACK
        Raiserror('The article has not been deleted!', 16,1)

希望你们能在这里帮助我..

1 个答案:

答案 0 :(得分:1)

您可能希望将代码更改为以下内容:

  begin try
    BEGIN TRANSACTION

            DELETE FROM x2 where i1 = 5
            DELETE FROM x2 where i1 = 6

    commit
    Raiserror('The article has been deleted!', 16, 1)

  end try

   begin catch
       if @@TRANCOUNT > 0
       begin
                  ROLLBACK
       end
       Raiserror('The article has not been deleted!', 16,1)

   end catch