Solved

Trigger on DB2

Posted on 2010-11-16
11
726 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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