Solved

Validate a column value before doing Update ?

Posted on 2014-03-11
2
239 Views
Last Modified: 2014-03-31
Hi. I'm so-so with SQL so bear with me ...
My client has a bunch of old stored procedures which are all pretty similar, one of which is at the bottom of this question.  

We want to add some "validation" code before the UPDATE.  If the validation fails, then we want to raise an error with a description like "Already batched"
We would need to validate that the record we are about to update (where identity = @CCARD_TRANS_ID) does not have a [BatchStatus] = "B".   So, I was thinking of doing something like:
IF @New_Status = 'V' and 
    ( SELECT [BatchStatus] FROM [CCARD_TRANS] 
      WHERE [CCARD_TRANS_ID] = @CCARD_TRANS_ID) = 'B'		   
    BEGIN
         -- raise an error (somehow) here and skip the update --
         -- @errormsg = 'Already batched'
    END

Open in new window

Should I be doing this differently?  Given my client's typical procedure layout, what's the best way to raise an error with a custom @errormsg ?
Thanks!

Here's the complete procedure:

CREATE PROCEDURE [dbo].[prCCARD_CCARD_TRANS_UPDATE_STATUS](
    @CCARD_TRANS_ID int,
    @User_Name      varchar(50),
    @New_Status     char(1) = null 
) 	
	
As

BEGIN
    DECLARE @errno integer
    DECLARE @errmsg varchar(255)
    DECLARE @rowcount integer

    BEGIN TRANSACTION
        SAVE TRANSACTION S_CCARD_TRANS
        
        BEGIN

            *** INSERT VALIDATION CODE HERE ? ***
			
            UPDATE [CCARD_TRANS]
                SET [Last_Mod_UserName] = @User_Name,				
                    [TransactionStatus] = @New_Status			
                WHERE ([CCARD_TRANS_ID] = @CCARD_TRANS_ID)
        END
		
        -----------------------
        select @errno=@@Error, @rowcount=@@Rowcount
        IF @errno<>0
            BEGIN
                goto error
            END
	      
        IF @ROWCOUNT = 0 
            BEGIN
                SELECT @errno=20001,@errmsg='Cannot find row for update.'
                goto error
            END

    COMMIT TRANSACTION
  
    RETURN 0
	
    -----------------------	
    -----------------------
    error: 
        rollback transaction S_CCARD_TRANS 
        commit transaction
        if @errno >= 20001 raiserror @errno  @errmsg
        return @errno
		
END

Open in new window

0
Comment
Question by:Rob Rudloff
[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 Comments
 
LVL 27

Accepted Solution

by:
Zberteoc earned 213 total points
ID: 39921509
If you want to prevent the update for that row if the Status is already B then why not a simple addition in where clause:

...
            UPDATE [CCARD_TRANS]
                SET [Last_Mod_UserName] = @User_Name,				
                    [TransactionStatus] = @New_Status			
                WHERE ([CCARD_TRANS_ID] = @CCARD_TRANS_ID
					AND [TransactionStatus]<>'B')

Open in new window

You don't need to raise errors just don't do the update if the existing [TransactionStatus]='B', update only if <>'B'.
0
 
LVL 40

Assisted Solution

by:lcohan
lcohan earned 212 total points
ID: 39921842
If the above comment does not suit your needs you could use a BEGIN/TRY/CATCH sequence in your stored procedure.

http://technet.microsoft.com/en-us/library/ms179296(v=sql.105).aspx
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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.
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.

691 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