Solved

How would I write this stored procedure?

Posted on 2014-03-07
3
216 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

734 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