Solved

How would I write this stored procedure?

Posted on 2014-03-07
3
212 Views
Last Modified: 2014-03-07
Here's my current stored procedure:

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

ALTER PROCEDURE [dbo].[cer_CapxUpdateTemp] --Parms for stored procedure
            @cercapxid as int,
            @allocatedamount as int
      AS
      BEGIN
            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
                  begin
                        set @key = 0
                        set @ErrorMessage = 'Failed to update the record (' + cast(@ErrorNo as varchar) + '}'
end

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;
else
@allocatedamount = @allocatedamount;
where cercapxid=@cercapxid...

But how do I code that correctly?


      
            
            
            select @key as ID, @ErrorMessage as ErrorMessage
            
      END
0
Comment
Question by:brucegust
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Jerry Miller earned 500 total points
ID: 39913143
Something along these lines should work:

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

     
                  Update cer_CapX
                 set allocatedamount =
                    SELECT  CASE
                    WHEN @allocatedamount > original_totalcost THEN original_allocatedamount
                   ELSE
                   @allocatedamount
                   END
              where cercapxid = @cercapxid
              set @ErrorNo = @@Error
                  if @ErrorNo <> 0
                  begin
                        set @key = 0
                        set @ErrorMessage = 'Failed to update the record (' + cast(@ErrorNo as varchar) + '}'
end
0
 

Author Comment

by:brucegust
ID: 39913157
Yo, JMiller!

This looks great!

The original_totalcost variable. I was planning on establishing that via a select statement based on the capxid. What I came up with was:

select totalcost as original_totalcost, allocatedamount from cer_Capx where cercapxid=@cercapxid
                  if (original_totalcost>allocatedamount)
                  Update cer_CapX set allocatedamount = original_totalcost where cercapxid=@cercapxid
                  else
                  Update cer_Capx set allocatedamount = @allocatedamount where cercapxid = @cercapxid

It doesn't work. I'm getting errors that read "Invalid column name, 'original_totalcost."

I'm sitting here pondering what I could do differently and then I see your post and I'm thinking "Awesome!" But how are you defining "original_totalcost?" Is there a select statement in your syntax that I'm blowing past? Should I try to pass that into my function and have it in place like that?

What do you think?
0
 

Author Comment

by:brucegust
ID: 39913236
I got it!

if @allocatedamount > (select totalcost from cer_Capx where cercapxid=@cercapxid)
                  Update cer_CapX set allocatedamount = (select totalcost from cer_Capx where cercapxid=@cercapxid) where cercapxid=@cercapxid
                  else
                  Update cer_Capx set allocatedamount = @allocatedamount where cercapxid = @cercapxid

Thanks for your input!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question