Solved

Trigger on DB2

Posted on 2010-11-16
11
720 Views
Last Modified: 2012-05-10
DB2 on V5R4M0.

I have a table1.
with 2 fields:
NAME char(20), RIGHTNAME char(20).
I'd like that after I insert/update the Key Field NAME,
it places in RIGHTNAME the trim(name) with
a quantity of space (or other character like '=') in order to have the trim(name) alligned
on the right.
So, if I insert/update the name ARNOLD in NAME,
I will have in RIGHTNAME: '              ARNOLD'

How to create a trigger which does it?

0
Comment
Question by:bobdylan75
  • 7
  • 4
11 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 34145329
try something like

 CREATE TRIGGER set_name
      AFTER INSERT, UPDATE ON table1
REFERENCING NEW AS new_row
      FOR EACH ROW MODE DB2SQL
      BEGIN ATOMIC
        UPDATE table1 SET rightname REPEAT(' ', 20-length(trim(new_row.name)) || new_row.name
     where name = new_row.name;
      END
0
 

Author Comment

by:bobdylan75
ID: 34145456
There is something diffent from your and mine syntax..
It says that after INSERT there are a wrong character (,)
and if I delete ", update" he says che after rightname there will be '='
and thata REPEAT in unknown..
I have V5R4M0.
0
 

Author Comment

by:bobdylan75
ID: 34145461
and that REPEAT is unexpected..
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 37

Expert Comment

by:momi_sabag
ID: 34145576
maybe it does not support this syntax and you will have to create two triggers, one after insert and one after update

try this

 CREATE TRIGGER set_name_after_insert
      AFTER INSERT ON table1
REFERENCING NEW AS new_row
      FOR EACH ROW MODE DB2SQL
      BEGIN ATOMIC
        UPDATE table1 SET rightname = REPEAT(' ', 20-length(trim(new_row.name)) || new_row.name
     where name = new_row.name;
      END
0
 

Author Comment

by:bobdylan75
ID: 34145683
yES BUT NOW i HAVE ONLY ON SYNTAX ERROR:
It says that || is not valid.
and if I use + hi give my a runtime error on inserting.
0
 

Author Comment

by:bobdylan75
ID: 34145689
sorry: only one syntax error
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 34145715
sorry, left out a )

try

CREATE TRIGGER set_name_after_insert
      AFTER INSERT ON table1
REFERENCING NEW AS new_row
      FOR EACH ROW MODE DB2SQL
      BEGIN ATOMIC
        UPDATE table1 SET rightname = REPEAT(' ', 20-length(trim(new_row.name))) || trim(new_row.name)
     where name = new_row.name;
      END
0
 

Author Comment

by:bobdylan75
ID: 34145767
OK IT WORKS IN THIS WAY:
 CREATE TRIGGER SETNAME
      AFTER INSERT ON TABLE1                          
REFERENCING NEW AS new_row                                      
      FOR EACH ROW MODE DB2SQL                                  
      BEGIN ATOMIC                                              
UPDATE TABLE1 T1 SET T1.RIGHTNAME = CONCAT ( REPEAT('X',  
20-length(trim(new_row.NAME)) ) , TRIM(NEW_ROW.NAME) )        
      where T1.NAME = new_row.NAME;                              
      END                                                        
0
 

Author Comment

by:bobdylan75
ID: 34145772
But Momi,
if now I want to create the update trigger one?
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 34145840
do the same with a different name

 CREATE TRIGGER SETNAME_update
      AFTER update ON TABLE1                          
REFERENCING NEW AS new_row                                      
      FOR EACH ROW MODE DB2SQL                                  
      BEGIN ATOMIC                                              
UPDATE TABLE1 T1 SET T1.RIGHTNAME = CONCAT ( REPEAT('X',  
20-length(trim(new_row.NAME)) ) , TRIM(NEW_ROW.NAME) )        
      where T1.NAME = new_row.NAME;                              
      END        
0
 

Author Closing Comment

by:bobdylan75
ID: 34145866
thank you very much!!!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

November 2009 Recently, a question came up in the DB2 forum regarding the date format in DB2 UDB for AS/400.  Apparently in UDB LUW (Linux/Unix/Windows), the date format is a system-wide setting, and is not controlled at the session level.  I'm n…
Recursive SQL in UDB/LUW (you can use 'recursive' and 'SQL' in the same sentence) A growing number of database queries lend themselves to recursive solutions.  It's not always easy to spot when recursion is called for, especially for people una…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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