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 3977888
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:58:39+00:00 2026-05-20T04:58:39+00:00

I am trying to write a sproc with a transaction. Can anybody tell me

  • 0

I am trying to write a sproc with a transaction. Can anybody tell me if there would be any issues with code below, or if it will work as intended?

ALTER procedure [dbo].[DeleteMetricMeter]

(
    @SectionID int,
    @MetricMeterID int,
    @Result bit output
)

as

declare @MetricMeterCount int
declare @err int
declare @rowcount int

set xact_abort on

begin tran

select @MetricMeterCount = count(*) from luMetricMeters
where fkSectionID = @SectionID

select @err = @@error, @rowcount = @@rowcount 

if (@err <> 0) or (@rowcount = 0)
begin
    goto on_error           
end

delete from luMetricMeterList
where pkMetricMeterID = @MetricMeterID

select @err = @@error, @rowcount = @@rowcount 

if (@err <> 0) or (@rowcount = 0)
begin
    goto on_error           
end 

delete from luMetricMeters
where pkMetricMeterID = @MetricMeterID

select @err = @@error, @rowcount = @@rowcount 

if (@err <> 0) or (@rowcount = 0)
begin
    goto on_error           
end

if (@MetricMeterCount = 1)
begin
    delete from luMetricSections
    where pkSectionID = @SectionID

    select @err = @@error, @rowcount = @@rowcount 

    if (@err <> 0) or (@rowcount = 0)
    begin
        goto on_error           
    end
end

commit tran
set @result = 1
return @result

on_error:
    rollback tran
    set @result = 0
    return @result
  • 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-05-20T04:58:40+00:00Added an answer on May 20, 2026 at 4:58 am

    There are a few issues with the procedure:

    • You should not evaluate rowcount unless it really indicates an error. When there is no data deleted there is no need to rollback.
    • Your code is not thread safe. The MetricMeterCount query should be changed to this to prevent other threads from performing the delete in between your select & the execution of the delete:

      select @MetricMeterCount = count(*) 
        from luMetricMeters with (xlock, serializable)
       where fkSectionID = @SectionID  
      

    I would write the code like this:

    ALTER procedure [dbo].[DeleteMetricMeter]
    
    (
        @SectionID int,
        @MetricMeterID int,
        @Result bit output
    )
    
    as
    
    DECLARE @MetricMeterCount int
    DECLARE @err int
    DECLARE @rowcount int
    
    SET xact_abort ON
    
    BEGIN TRAN
    
    DELETE FROM luMetricMeterList 
     WHERE pkMetricMeterID = @MetricMeterID
    
    SELECT @err = @@error
    
    IF (@err <> 0) 
        GOTO on_error           
    
    DELETE FROM luMetricMeters
     WHERE pkMetricMeterID = @MetricMeterID
    
    SELECT @err = @@error
    
    IF (@err <> 0) 
        GOTO on_error           
    
    IF EXISTS (SELECT * 
                 FROM luMetricMeters WITH (xlock, serializable) 
                WHERE fkSectionID = @SectionID)
    BEGIN
        DELETE FROM luMetricSections 
         WHERE pkSectionID = @SectionID
    
        SELECT @err = @@error
        IF (@err <> 0) 
            GOTO on_error           
    END
    
    COMMIT TRAN
    
    RETURN (0)
    
    on_error:
        ROLLBACK TRAN
        RETURN (-1)
    GO
    

    Note: The return values should be 0 for success and a negative number for failure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying write code that will maintain the sorting preference while changing page
Trying to write simple program on Fraction operations. Can't compile the code. Getting an
I'm trying write a code that can the set property value through the lambda
Im trying to write the results from a SProc to a textfile. I have
I m trying write code that after reset set up rrpmax as 3000. It
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a chat, like on facebook, I wondered if two clients can
Trying to write a code at the moment that basically tests to see if
Trying to write a code that searches hash values for specific string's (input by

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.