Solved

Validate a column value before doing Update ?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

744 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

12 Experts available now in Live!

Get 1:1 Help Now