Solved

Validate a column value before doing Update ?

Posted on 2014-03-11
2
238 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 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