473,549 Members | 3,048 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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'12345 67890123456'
-----------------------------------------------
So when an insert statment from our crappy application tries to pass a
bad insert such as

Insert into tab1 values ('1234567890123 456')

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 '12345678901234 56' 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'12345 67890123456'

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'12345 67890123456';
end if;
END
-----------------------------------------------

But same story, SQL0433N value '12345678901234 56' 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'12345 67890123456';
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(At tr1 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 6425
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'12345 67890123456'
-----------------------------------------------
So when an insert statment from our crappy application tries to pass a
bad insert such as

Insert into tab1 values ('1234567890123 456')

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 '12345678901234 56' is too long

<snip>
x'1234567890123 456' is only 8 long....
'12345678901234 56' 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'12345 67890123456'

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'12345 67890123456'
-----------------------------------------------
So when an insert statment from our crappy application tries to pass a
bad insert such as

Insert into tab1 values ('1234567890123 456')

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 '12345678901234 56' is too long

<snip>
x'1234567890123 456' is only 8 long....
'12345678901234 56' 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'12345 67890123456'

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
22188
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 MYTABLE begin :new.DT := SYSDATE; if :new.NM is NULL then :new.NM := USER; end if; end myTRIGGER;
1
15392
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
41571
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 do my insert into 3 different table all using the same uniqueID. I can't use the @@identity function because my application uses a connection pool...
3
3449
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 updates but if I raise an Error the update get rolled back. What do I have to define in the Trigger after the updates?
1
4028
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
21032
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 noticed that th eaccess plan for the 2 situations is quite different. For the case where the trigger is in place, the optimizer applies 2 extra residual...
1
2272
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 this table, everything work fine means, when I manual insert data into table the last row of inserting is what I am doing but after I add trigger on...
2
4194
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 after an insert:
2
3734
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 cross reference table to my CATALOG Table based on key words.
0
7718
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. ...
0
7956
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...
1
7470
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...
0
6041
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...
1
5368
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...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1058
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
763
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.