473,804 Members | 3,225 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4202
On May 17, 3:43 am, "gimme_this_gim me_t...@yahoo.c om"
<gimme_this_gim me_t...@yahoo.c omwrote:
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.co m 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
1983
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. Below is what I did: SET QUOTED_IDENTIFIER OFF GO INSERT INTO BankInfo (CoLogo) VALUES(0xFFFFFFFF)
7
2387
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 or Oracle) controlled by Microsoft's Distributed Transaction Coordinator in a Windows2000 environment, with any resource manager under the control of DECdtm (e.g. Rdb (or Oracle via the XA Veneer)) in a VMS environment.
2
1468
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 also automatically rendered to html (Lotus Notes). I've lately been thinking about trying to create a relatively simple app using tools other than Notes just to see A) how difficult it it and B) stretch my mind into new shapes. I thought...
0
7572
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 found none, so, I wrote one and decided to share it with you. It's a very simple one with few functions. I derived a DataGrid and added to it a DataTable to hold the data. The object itself is handling the synchronization between them, because...
10
1763
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 back from the PHP script. Binary data is necessary because I want to be able to send files as well as compressed (zipped) data. The example PHP script can ofcourse, simply make up some dummy data and send it to the C# app. I have tried desperately...
3
1906
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 horribly complicated code on the web and have got totally bogged down in scals and dacls. Can someone point me at a nice simple example of how to set up security attributes for unrestricted access? -- Dave
0
915
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 written for the example, and URLs to any frameworks or modules used. Their objective is to see how efficient/elegant the language is for developers. This is one area in which Python should excel. 1) Render a simple Web page containing text, data,...
7
3109
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 is guaranteed either) Below code fails for a stringstream. Any good ways of dealing with this. Reason for doing this is that I am modify old C code for screen output that positions fields based on a row. I wanted to use a stringstream
6
1080
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 would make the drag and drop canvas any easier to code? Thx!
0
9588
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10589
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5663
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.