Solved

How would I write this stored procedure?

Posted on 2014-03-07
3
210 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now