Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:10:48+00:00 2026-06-14T05:10:48+00:00

I can rollback a transaction if an error occurs: CREATE PROCEDURE [dbo].[MySproc] ( @Param1

  • 0

I can rollback a transaction if an error occurs:

CREATE PROCEDURE [dbo].[MySproc]
(
   @Param1  [int] 
)
AS
BEGIN TRAN
SET NOCOUNT ON;

SELECT @Param1
UPDATE [dbo].[Table1]
SET    Col2 = 'something'
WHERE Col1 = @Param1

IF @@ERROR <> 0
BEGIN
    ROLLBACK TRAN
    RETURN -12
END
...

But if there are no entries in the Table1 with the Col1 = @Param1 transaction commits successfully, which is bad for me. I’d like to check if Col2 = 'something' is really done. If not, then rollback with a particular return code.
If I try to insert @@rowcount check after @@error check, like the following:

CREATE PROCEDURE [dbo].[MySproc]
(
   @Param1  [int] 
)
AS
BEGIN TRAN
SET NOCOUNT ON;

SELECT @Param1
UPDATE [dbo].[Table1]
SET    Col2 = 'something'
WHERE Col1 = @Param1

IF @@ERROR <> 0
BEGIN
    ROLLBACK TRAN
    RETURN -12
END

IF @@ROWCOUNT = 0
BEGIN
    ROLLBACK TRAN
    RETURN -27
END
...

it always rollbacks at this point, because @@rowcount evaluates the very last statement so it always equals 0.

How in such a case to check both the @@error and the number of rows affected?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:10:49+00:00Added an answer on June 14, 2026 at 5:10 am

    Store both into your own variables in a single query, then check:

    DECLARE @rc int
    DECLARE @err int
    SELECT @Param1
    UPDATE [dbo].[Table1]
    SET    Col2 = 'something'
    WHERE Col1 = @Param1
    SELECT @rc = @@ROWCOUNT,@err = @@ERROR
    
    IF @err <> 0
    BEGIN
        ROLLBACK TRAN
        RETURN -12
    END
    
    IF @rc = 0
    BEGIN
        ROLLBACK TRAN
        RETURN -27
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

CREATE PROCEDURE [dbo].[PL_GEN_PROVN_NO1] @GAD_COMP_CODE VARCHAR(2) =NULL, @@voucher_no numeric =null output AS BEGIN DECLARE @NUM
Essence: How can I auto-rollback my hibernate transaction in a JUnit Test run with
The error shows this code can not rollback: class AddCountToTag < ActiveRecord::Migration def change
for some reason I'm experiencing the Operational Error with can't rollback message when I
I am writing stored procedure in following way.. CREATE PROCEDURE spTest @intCompId int, @varCompName
How can I set a transaction isolation level using Squeryl? For instance, right now
I have a stored procedure that has a BEGIN TRANSACTION and COMMIT TRANSACTION statement.
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Quick version: We're looking for a way to force a transaction to rollback when
I have never used a Transaction, Commit and Rollback before and now I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.