473,387 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Seeking simple after insert example

I'm using DB2 8.1.

Suppose table foo has columns name and lname:

create table foo
(name as varchar(200),
lname as varchar(200));

Write a trigger that inserts the lower case value of name after an
insert:

So

insert (name) into foo values ('Hello World");

Then:

select * from foo;

Results in:

"Hello World","hello world"

Thanks
Jun 27 '08 #1
2 4182
On May 17, 3:43 am, "gimme_this_gimme_t...@yahoo.com"
<gimme_this_gimme_t...@yahoo.comwrote:
I'm using DB2 8.1.

Suppose table foo has columns name and lname:

create table foo
(name as varchar(200),
lname as varchar(200));

Write a trigger that inserts the lower case value of name after an
insert:

So

insert (name) into foo values ('Hello World");

Then:

select * from foo;

Results in:

"Hello World","hello world"

Thanks
I assume you don't want an "after insert" trigger but a "before
insert" trigger. Here is an example:

CREATE TRIGGER T
NO CASCADE BEFORE INSERT ON foo
REFERENCING NEW AS N
FOR EACH ROW
MODE DB2SQL
set lname = lcase(n.name);

[db2inst1@wb-01 ~/nya/trigger]$ db2 "insert into foo (name) values
('Hello World')"
DB20000I The SQL command completed successfully.
[db2inst1@wb-01 ~/nya/trigger]$ db2 "select * from foo"

NAME
LNAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hello
World
hello world

In general it is a good idea to have a check constraint that
guarantees this:

[db2inst1@wb-01 ~/nya/trigger]$ db2 "alter table foo add constraint C
check (lname = lcase(name))"

You might also be interested in a generated always variant (no trigger
needed, and the check constraint is generated for you under the hood):

[db2inst1@wb-01 ~/nya/trigger]$ db2 "create table foo2 (name
varchar(200) not null, lname varchar(200) not null generated always as
(lcase(name)))"
DB20000I The SQL command completed successfully.
[db2inst1@wb-01 ~/nya/trigger]$ db2 "insert into foo2 (name) values
('Hello World')"
DB20000I The SQL command completed successfully.
[db2inst1@wb-01 ~/nya/trigger]$ db2 "select * from foo2"

NAME
LNAME
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Hello
World
hello
world

1 record(s) selected.
HTH
/Lennart
Jun 27 '08 #2
gi*******************@yahoo.com wrote:
I'm using DB2 8.1.

Suppose table foo has columns name and lname:

create table foo
(name as varchar(200),
lname as varchar(200));

Write a trigger that inserts the lower case value of name after an
insert:

So

insert (name) into foo values ('Hello World");

Then:

select * from foo;

Results in:

"Hello World","hello world"

Thanks
Any particular reason you don't want to use a generated column?

CREATE TABLE FOO (
NAME VARCHAR(200),
LNAME VARCHAR(200) GENERATED ALWAYS AS (LOWER(NAME))
);
Cheers,

Dave.
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: rdshultz | last post by:
I'm a complete newbie. Need to insert a Company logo into a database column to use later on in a check printing application. Read how to insert the pointer instead of the object into the column. ...
7
by: Richard Maher | last post by:
Hi, I am seeking the help of volunteers to test some software that I've developed which facilitates distributed two-phase commit transactions, encompassing any resource manager (e.g. SQL/Server...
2
by: dogu | last post by:
I've worked the last number of years using an object oriented database tool that comes with a complete IDE, built in wigets for things like forms, buttons, hot spots, propietary client but apps are...
0
by: Tal Sharfi | last post by:
Hi everyone I recently had the need for StringGrid object same as the one that Delphi has. An object that helps show lists of other objects in a simple grid. I searched the news groups and...
10
by: Susan Baker | last post by:
Hi Guys (and girls), I am in a bit of a bind. I'm looking for a simple "proof of concept" C# app (WinForm OR console) that sends a web request to a simple PHP script and receives BINARY data...
3
by: Dave | last post by:
I am trying to create a couple of event and a memory mapped file for interprocess communication that have security settings such that they can be used between any two users. I have found some...
0
by: Jeff Rush | last post by:
In working up a response to the survey being conducted by Forrester Research on dynamic languages, there is a section wherein they want to see code samples. The samples must include all code...
7
by: Adrian | last post by:
What is a stringsteam supposed to do when you seek past the end of existing buffer. I can seek past the end of a file stream (my implementation fills the space will nulls but I cannot find if this...
6
by: jim | last post by:
I'd like to build my own drag and drop web page editor. Are there any components out there that already handle CSS, PHP, XHTML and Javascript validation? And, are there any components that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.