Solved

Validate a column value before doing Update ?

Posted on 2014-03-11
2
229 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
2 Comments
 
LVL 26

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 39

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
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…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now