简体   繁体   中英

SQL statement to insert record based on the Case result

I have internal system which uses parameters and front-end. So, here is the conundrum:

  1. When record is updated, the trigger is fired and SQL statement is executed.

  2. I need to check if the field contain word "BAD" or "GOOD" and my SQL statement must be different. And I can't use stored procedures. must be pure SQL

something like this

CASE
  WHEN  (TO_CHAR(?)=TO_CHAR('GOOD')) 
     THEN INSERT VALUES INTO GOOD_TABLE
  WHEN  (TO_CHAR(?)=TO_CHAR('BAD')) 
     THEN INSERT VALUES INTO BAD_TABLE
END

You can leverage the multi-table inserts functionality using INSERT FIRST / INSERT ALL command, as below:

   INSERT FIRST
   WHEN (TO_CHAR(?)='GOOD') THEN
      INTO GOOD_TABLE
         VALUES(values...)
   WHEN (TO_CHAR(?)='BAD') THEN
      INTO BAD_TABLE
         VALUES(values...)
   SELECT values, ?
   FROM mytable;

Reference :

Multi table Inserts on Oracle Examples blog (Very good examples)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM