如何使用db2中的update和case语句更新多列

时间:2017-04-12 03:40:40

标签: sql db2

我想用case语句更新两列,请告诉我如何实现这一目标。

我在下面的代码片段中尝试了但却出现语法错误:

UPDATE SASDB.TEMP_VALIDATE_FIN_ENTRIES 
            (CASE  WHEN  (LENGTH(TRIM(ACCOUNT_ID)) = 16 
                        AND NVL(LENGTH(TRIM(TRANSLATE(UPPER(ACCOUNT_ID),'0123456789',' '))),0)=0
                        AND  LENGTH(TRIM(SOL_ID)) = 4
                        AND NVL(LENGTH(TRIM(TRANSLATE(UPPER(SOL_ID),'0123456789',' '))),0)=0
                        AND NVL(LENGTH(TRIM(TRANSLATE(UPPER(DEBIT_TRANS_AMT),'0123456789,.',' '))),0)=0
                        AND NVL(LENGTH(TRIM(TRANSLATE(UPPER(CREDIT_TRANS_AMT),'0123456789,.',' '))),0)=0) 
                            THEN
                                            SET VALIDATION_REMARKS='ACCOUNTS ARE VALIDATED SUCCESSFULLY'
                                            SET VALIDATION_STATUS='YES'
                WHEN (LENGTH(TRIM(ACCOUNT_ID)) <> 16)      
                                THEN 
                                                SET VALIDATION_REMARKS='ACCOUNT_ID SHOULD BE LENGTH  16'
                                                SET VALIDATION_STATUS='NO'
                WHEN LENGTH(TRIM(SOL_ID)) <> 4
                                THEN 
                                                SET VALIDATION_REMARKS='SOL_ID HAS NOT LENGTH  4'
                                                SET VALIDATION_STATUS='NO'
                WHEN NVL(LENGTH(TRIM(TRANSLATE(UPPER(SOL_ID),'0123456789',' '))),0)>0
                                THEN 
                                            SET VALIDATION_REMARKS='SOL_ID HAS ALPHABET OR SPECAIL CHARACTER'
                                            SET VALIDATION_STATUS='NO'
                WHEN NVL(LENGTH(TRIM(TRANSLATE(UPPER(ACCOUNT_ID),'0123456789',' '))),0)>0                           
                                    THEN 
                                        SET VALIDATION_REMARKS='ACCOUNT_ID HAS ALPHABET OR SPECAIL CHARACTER'
                                            SET VALIDATION_STATUS='NO'
                WHEN NVL(LENGTH(TRIM(TRANSLATE(UPPER(CREDIT_TRANS_AMT),'0123456789,.',' '))),0)>0
                                    THEN 
                                        SET VALIDATION_REMARKS='CREDIT TRANASTION AMOUNT HAS SPECIAL CHARACTER'
                                            SET VALIDATION_STATUS='NO'
                WHEN   NVL(LENGTH(TRIM(TRANSLATE(UPPER(DEBIT_TRANS_AMT),'0123456789,.',' '))),0)>0
                                    THEN 
                                    SET VALIDATION_REMARKS='DEBIT TRANASTION AMOUNT HAS SPECIAL CHARACTER'
                                        SET VALIDATION_STATUS='NO'
                WHEN ACCOUNT_EXIT_FLAG IS NULL 
                                    THEN SET VALIDATION_REMARKS='ACCOUNT DOESNOT EXIST IN DATABASE'
                                        SET ACCOUNT_EXIT_FLAG=NVL(ACCOUNT_EXIT_FLAG,'NO')
                END);

以上代码失败并出现语法错误,请告诉我如何解决这些错误。

2 个答案:

答案 0 :(得分:0)

你必须改变你的结构。

UPDATE SASDB.TEMP_VALIDATE_FIN_ENTRIES SET
VALIDATION_REMARKS = CASE WHEN @columnname THEN ELSE END,
VALIDATION_STATUS = CASE WHEN @columnname THEN ELSE END

答案 1 :(得分:0)

试试这个简化的解决方案:

Date