Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Validate a column value before doing Update ?

Posted on 2014-03-11
2
Medium Priority
?
250 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 852 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 848 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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 utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

618 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