SQL更新不会引发错误

时间:2013-11-13 21:33:54

标签: sql sql-server sql-update

我有这个简单的SQL更新

IF(@MyID IS NOT NULL)
 BEGIN
  BEGIN TRY
   UPDATE DATATABLE
    SET Param1=@Param1, Data2=@Data2,...
    WHERE MyID=@MyID
   END TRY
   BEGIN CATCH
    SELECT ERROR_MESSAGE() AS 'Message' 
    RETURN -1
   END CATCH

   SELECT * FROM DATATABLE WHERE MyID= @@IDENTITY
   SET @ResultMessage = 'Succefully Inserted' 
   SELECT @ResultMessage AS 'Message' 
   RETURN 0
 END

问题是,当我提供一个无效的ID,一个不存在的ID时,不会抛出错误,我仍然会在成功插入消息时得到错误代码0。我还在捕获后添加了这个。仍然没有,我错过了一些基本的东西吗?

END CATCH
IF(@@ERROR != 0)
BEGIN
    SET @ResultMessage = 'Not Successful Inserted' 
    SELECT @ResultMessage AS 'Message' 
    RETURN -1
END
SELECT * FROM DATATABLE WHERE MyID= @@IDENTITY
SET @ResultMessage = 'Succefully Inserted' 
SELECT @ResultMessage AS 'Message' 
RETURN 0

我想要找一些特别的东西吗?

4 个答案:

答案 0 :(得分:3)

SQL将捕获错误,但是不更新任何行的UPDATE语句是有效的SQL语句,不应返回错误。您可以检查 @@ RowCount 以查看更新语句实际更新的行数

  IF(@MyID IS NOT NULL)
     BEGIN
      BEGIN TRY
       UPDATE DATATABLE
        SET Param1=@Param1, Data2=@Data2,...
        WHERE MyID=@MyID
        IF @@RowCount = 0
        BEGIN
           SELECT 'No record found...' AS Message
           RETURN -1
        END
       END TRY
       BEGIN CATCH
        SELECT ERROR_MESSAGE() AS 'Message' 
        RETURN -1
       END CATCH

       SELECT * FROM DATATABLE WHERE MyID= @@IDENTITY
       SET @ResultMessage = 'Succefully Inserted' 
       SELECT @ResultMessage AS 'Message' 
       RETURN 0
     END

答案 1 :(得分:0)

您可以替换

IF(@MyID IS NOT NULL)

IF(@MyID IS NOT NULL) OR NOT EXISTS (
    SELECT 1
    FROM DATATABLE
    WHERE MyID=@MyID
)

或者您可以采用Sparky的解决方案。取决于哪种逻辑更适合您。如果@MyID无效,是否应该抛出错误消息?

答案 2 :(得分:0)

我会这样的......在事务中WRAP更新语句并在失败时回滚

IF(@MyID IS NOT NULL)
BEGIN
   BEGIN TRY

       BEGIN TRANSACTION
           UPDATE DATATABLE
            SET Param1=@Param1, Data2=@Data2,...
            WHERE MyID=@MyID
            SET @ResultMessage = 'Succefully Inserted' 
            SELECT @ResultMessage AS 'Message' 
            RETURN 0
        COMMIT TRANSACTION 
   END TRY

   BEGIN CATCH
    IF @@TRANCOUNT > 0
      ROLLBACK TRANSACTION  

        SELECT ERROR_MESSAGE() AS 'ErrorMessage' 
        SET @ResultMessage = 'Not Successful Inserted' 
        SELECT @ResultMessage AS 'Message' 
        RETURN -1
    END CATCH
END

答案 3 :(得分:0)

IF @@ RowCount = 0 BEGIN选择1/0; END

我不知道这是否是一个好方法,但这就是我刚刚所做的。如果有0行受到影响,则会通知用户错误:)。

Update Table1 Set Table1_Sold = 1 Where table1_ID = '043f258B-8A0B-4CA1-87EC-CDCBD38EE9E1';

 IF @@RowCount = 0 BEGIN Select 1/0; END

如果影响0行,则会出现错误。可能不是最好的方法,但对我有用。我可能对此不满意。

在C#中,您会发现错误并显示自己的消息。

catch (SqlException e) 
            {


                switch(e.Number)
                 {
                    case 8134:// Divide by zero error
                        MessageBox.Show("ERROR Divide by ZERO, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;

                    default:
                  MessageBox.Show($"{e.Number.ToString()} \t {e.Message} \t {e.InnerException} \t {e.Data} \t {e.ToString()}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                }