473,396 Members | 2,029 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,396 software developers and data experts.

DB2 Before Insert Trigger not acting like before?

Perhaps my thinking is wrong but this is what I have:

1 table (Tab1) with 1 attribute (Attr1)

Attr1 char(16) for bit data

-----------------------------------------------
create trigger check
no cascade before insert on Tab1
referencing new as N
for each row mode DB2SQL
WHEN length(N.Attr1)>16 set N.Attr1=x'1234567890123456'
-----------------------------------------------
So when an insert statment from our crappy application tries to pass a
bad insert such as

Insert into tab1 values ('1234567890123456')

that would normally be too long because it's not prefixed with the x
which is why I want the trigger to intercept, but it doesn't
with the trigger created it just gives the same error as without the trigger
SQL0433N value '1234567890123456' is too long

If I say

create trigger check
no cascade before insert on Tab1
referencing new as N
for each row mode DB2SQL
set N.Attr1=x'1234567890123456'

It intercepts and works fine.

I've tried several other methods to rectify the situation, like
-----------------------------------------------
create trigger check
no cascade before insert on Tab1
referencing new as N
for each row mode DB2SQL
BEGIN ATOMIC
DECLARE TMPATTR integer;
set TMPATTR=length(N.Attr1);
if TMPATTR>16 then
set N.Attr1=x'1234567890123456';
end if;
END
-----------------------------------------------

But same story, SQL0433N value '1234567890123456' too long.

As an interesting test I ran this one:
-----------------------------------------------
create trigger check
no cascade before insert on Tab1
referencing new as N
for each row mode DB2SQL
BEGIN ATOMIC
DECLARE TMPATTR integer;
set TMPATTR=length(N.Attr1);
set N.Attr1=x'1234567890123456';
END
-----------------------------------------------
And I don't get the error. So the length part works, it's when I try to
evaluate the thing with and if or when that it craps out.
The deal is with certain functions in our application it produces the
correct insert statment, but in other functions, it does not, and that's
where I had an idea to have a trigger to correct the situation until the
application gets fixes (many months away).
Where am I going wrong? My set statments above aren't really the ones I
want to use either, meaning I don't want to set to a fixed value, but
rather have this: set N.Attr1=cast(Attr1 as char16 for bit data) (I
think that will work?), so I retain the original string.
I thought the whole idea of a before insert trigger was to get to the
values before there were inserted into the table. I thought that meant
before they were checked against the table constraints, but that doesn't
seem to be the case, the second I try to use the original value in
anything it just tells me it's too long

Thanks for any help!

Kenneth J. Snyder
Nov 12 '05 #1
2 6421
73blazer wrote:
Perhaps my thinking is wrong but this is what I have:

1 table (Tab1) with 1 attribute (Attr1)

Attr1 char(16) for bit data

-----------------------------------------------
create trigger check
no cascade before insert on Tab1
referencing new as N
for each row mode DB2SQL
WHEN length(N.Attr1)>16 set N.Attr1=x'1234567890123456'
-----------------------------------------------
So when an insert statment from our crappy application tries to pass a
bad insert such as

Insert into tab1 values ('1234567890123456')

that would normally be too long because it's not prefixed with the x
which is why I want the trigger to intercept, but it doesn't
with the trigger created it just gives the same error as without the
trigger
SQL0433N value '1234567890123456' is too long

<snip>
x'1234567890123456' is only 8 long....
'1234567890123456' is 16 long

Now, the transition variable has the same data type as the table column.
So the insert values will overflow the variable just the same as the
column.

To make a long story short.. if you are willing to stick with CHAR(16)
FOR BIT DATA in the table (8 bytes waste)
All you need to do is this:

create trigger check
no cascade before insert on Tab1
referencing new as N
for each row mode DB2SQL
WHEN length(N.Attr1)>8 set N.Attr1=x'1234567890123456'

But there is no way to intercept a column overflow with a trigger.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Serge Rielau wrote:
73blazer wrote:
Perhaps my thinking is wrong but this is what I have:

1 table (Tab1) with 1 attribute (Attr1)

Attr1 char(16) for bit data

-----------------------------------------------
create trigger check
no cascade before insert on Tab1
referencing new as N
for each row mode DB2SQL
WHEN length(N.Attr1)>16 set N.Attr1=x'1234567890123456'
-----------------------------------------------
So when an insert statment from our crappy application tries to pass a
bad insert such as

Insert into tab1 values ('1234567890123456')

that would normally be too long because it's not prefixed with the x
which is why I want the trigger to intercept, but it doesn't
with the trigger created it just gives the same error as without the
trigger
SQL0433N value '1234567890123456' is too long

<snip>
x'1234567890123456' is only 8 long....
'1234567890123456' is 16 long

Now, the transition variable has the same data type as the table column.
So the insert values will overflow the variable just the same as the
column.

To make a long story short.. if you are willing to stick with CHAR(16)
FOR BIT DATA in the table (8 bytes waste)
All you need to do is this:

create trigger check
no cascade before insert on Tab1
referencing new as N
for each row mode DB2SQL
WHEN length(N.Attr1)>8 set N.Attr1=x'1234567890123456'

But there is no way to intercept a column overflow with a trigger.

Cheers
Serge

Ok, I screwed up, the original declaration is char(8) for bit data, not
16, there isn't any waste. But what you say is interesting, I can't
intercept column overflow, but I could perhaps change that column to be
char(16) for bit data and then check as you mention. That might screw up
our application though, and this database is multisited in 8 other
places around the globe, but that looks like my only solution so far.
Thanks for the help, again!

Ken
Nov 12 '05 #3

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

Similar topics

4
by: DTB | last post by:
I am having trouble creating an INSTEAD OF trigger in SQL Server to replicate a BEFORE UPDATE trigger from ORACLE. Here is a sample of the ORACLE BEFORE UPDATE trigger: CREATE TRIGGER myTRIGGER ON...
1
by: shottarum | last post by:
I currently have 2 tables as follows: CREATE TABLE . ( mhan8 int, mhac02 varchar(5), mhmot varchar(5), mhupmj int )
4
by: brent.ryan | last post by:
How do I get the next int value for a column before I do an insert in MY SQL Server 2000? I'm currently using Oracle sequence and doing something like: select seq.nextval from dual; Then I...
3
by: tomtailor | last post by:
Hello! I have a before insert Trigger and I want to catch if there is a duplicate Key Error. If the Key already exists I'd like to update else insert the row. OK I am at the point I did the...
1
by: gauravupreti | last post by:
Hi All, I have a table with a col. that accepts number. CREATE TABLE A (ID NUMBER NOT NULL) and another table with two columns as shown: CREATE TABLE B (ID NUMBER NOT NULL,
1
by: filip1150 | last post by:
I'm trying to find if there is any performance diference between explicitly using a sequence in the insert statement to generate values for a column and doing this in an insert trigger. I...
1
by: veasnamuch | last post by:
I have a problem while I create a trigger to my table. My objective is getting any change made to my table and record it in to another table . My have thousands records before I add new trigger to...
2
by: gimme_this_gimme_that | last post by:
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...
2
by: lenygold via DBMonster.com | last post by:
Hi Everebody: I have a table: CREATE TABLE CROSS_REFERENCE (ROW# INTEGER NOT NULL ,KEY_WORD CHAR(16) NOT NULL ,QUERY_DESCR VARCHAR(330) NOT NULL ,PRIMARY KEY (ROW#,KEY_WORD)); It is a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.