Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How would I write this stored procedure?

Posted on 2014-03-07
3
Medium Priority
?
221 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 2000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

670 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