Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trigger on DB2

Posted on 2010-11-16
11
Medium Priority
?
735 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

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 video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

609 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