We help IT Professionals succeed at work.
Get Started

How would I write this stored procedure?

Bruce Gust
Bruce Gust asked
Last Modified: 2014-03-07
Here's my current stored procedure:

USE [BIProd]
/****** Object:  StoredProcedure [dbo].[cer_CapxUpdateTemp]    Script Date: 3/7/2014 10:07:11 AM ******/

ALTER PROCEDURE [dbo].[cer_CapxUpdateTemp] --Parms for stored procedure
            @cercapxid as int,
            @allocatedamount as int
            declare @key as int
            declare @ErrorMessage as varchar(300)
            declare @ErrorNo as int
            set @ErrorMessage = ''
            set @ErrorNo = 0

                  Update cer_CapX set allocatedamount = @allocatedamount
                  where cercapxid = @cercapxid
                  set @ErrorNo = @@Error
                  if @ErrorNo <> 0
                        set @key = 0
                        set @ErrorMessage = 'Failed to update the record (' + cast(@ErrorNo as varchar) + '}'

The crux of the proc being

Update cer_CapX set allocatedamount = @allocatedamount
where cercapxid = @cercapxid

Here's my challenge:

I've got look out for allocated amounts that are great than what's actually been budgeted. I have access to that info which is located in the "totalcost" column of the very table I'm updating.

Here's where I'm a pig on rollerskates. How do I check to see if my user's allocated amount is greater than the total cost, and if so, how do I update that amount to the total cost value, rather than what they've inputted AND still update the other rows where the data is acceptable (meaning the amount allocated is either equal to or less than the total cost value)?

I'm thinking the logic would look something like this:

update cer_Capx set allocatedamount =
//rather than @allocatedamount, I would need to do a select statement / comparison
select allocatedamount as original_allocatedamount, totalcost as original_totalcost from cer_Capx where cercapxid=@cercapxid

if (@allocatedamount>original_totalcost)
@allocatedamount = original_allocatedamount;
@allocatedamount = @allocatedamount;
where cercapxid=@cercapxid...

But how do I code that correctly?

            select @key as ID, @ErrorMessage as ErrorMessage
Watch Question
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE