Solved

Trigger on DB2

Posted on 2010-11-16
11
708 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
Comment Utility
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
Comment Utility
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
Comment Utility
and that REPEAT is unexpected..
0
 
LVL 37

Expert Comment

by:momi_sabag
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:bobdylan75
Comment Utility
sorry: only one syntax error
0
 
LVL 37

Expert Comment

by:momi_sabag
Comment Utility
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
Comment Utility
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
Comment Utility
But Momi,
if now I want to create the update trigger one?
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
Comment Utility
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
Comment Utility
thank you very much!!!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 (it really isn't that hard to do) Recursive SQL is most often used to convert columns to rows or rows to columns.  A previous article described the process of converting rows to columns.  This article will build off of th…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

11 Experts available now in Live!

Get 1:1 Help Now