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

Dates not in the future

My client wants to disallow a birth date in the future. First I tried
the obvious:

ALTER TABLE IS3.ANIMALS
ADD CONSTRAINT b_d_f
CHECK (birth_date<=current_date)

It failed because special registers (current_date) are not allowed in
check conditions. Then I tried the trigger below:

create trigger b_d_f
no cascade before
update of birth_date
on is3.animals
referencing
old as o
new as n
for each row
mode db2sql
when (n.birth_date>current_date)
signal sqlstate '99001' set message_text = 'Date must not be
in the future'

It created OK, but does not do what I expected, disallow the update and
return the error. Obviously I don't understand something, but what? Any
help would be greatly appreciated.
Nov 12 '05 #1
8 1950
"Bob Stearns" <rs**********@charter.net> wrote in message
news:oc******************@fe05.lga...
My client wants to disallow a birth date in the future. First I tried the
obvious:

ALTER TABLE IS3.ANIMALS
ADD CONSTRAINT b_d_f
CHECK (birth_date<=current_date)

It failed because special registers (current_date) are not allowed in
check conditions. Then I tried the trigger below:

create trigger b_d_f
no cascade before
update of birth_date
on is3.animals
referencing
old as o
new as n
for each row
mode db2sql
when (n.birth_date>current_date)
signal sqlstate '99001' set message_text = 'Date must not be in
the future'

It created OK, but does not do what I expected, disallow the update and
return the error. Obviously I don't understand something, but what? Any
help would be greatly appreciated.


Can you issue a rollback when you get the error back in the program?
Nov 12 '05 #2
Bob Stearns wrote:
My client wants to disallow a birth date in the future. First I tried
the obvious:

ALTER TABLE IS3.ANIMALS
ADD CONSTRAINT b_d_f
CHECK (birth_date<=current_date)

It failed because special registers (current_date) are not allowed in
check conditions. Then I tried the trigger below:

create trigger b_d_f
no cascade before
update of birth_date
on is3.animals
referencing
old as o
new as n
for each row
mode db2sql
when (n.birth_date>current_date)
signal sqlstate '99001' set message_text = 'Date must not be
in the future'

It created OK, but does not do what I expected, disallow the update and
return the error. Obviously I don't understand something, but what? Any
help would be greatly appreciated.

On first blush teh trigger looks all right.
I presume you have the matching INSERT trigger of course.
Can you post a complete repro?

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #3
Serge Rielau wrote:
Bob Stearns wrote:
My client wants to disallow a birth date in the future. First I tried
the obvious:

ALTER TABLE IS3.ANIMALS
ADD CONSTRAINT b_d_f
CHECK (birth_date<=current_date)

It failed because special registers (current_date) are not allowed in
check conditions. Then I tried the trigger below:

create trigger b_d_f
no cascade before
update of birth_date
on is3.animals
referencing
old as o
new as n
for each row
mode db2sql
when (n.birth_date>current_date)
signal sqlstate '99001' set message_text = 'Date must not
be in the future'

It created OK, but does not do what I expected, disallow the update
and return the error. Obviously I don't understand something, but
what? Any help would be greatly appreciated.


On first blush teh trigger looks all right.
I presume you have the matching INSERT trigger of course.
Can you post a complete repro?

Cheers
Serge

The insert trigger would be the same except changing update to insert,
correct? To bad there is no syntax for 'update or insert'; surely that
is the commonest case of data validation.

NULLs are handled properly, since any comparison with NULL is false,
correct?

That would be hard since we're dealing with a php app. I was expecting
to get an error back like '22007--[IBM][CLI Driver][DB2/LINUX] SQL0180N
The syntax of the string representation of a datetime value is
incorrect. SQLSTATE=22007' which the program catches properly. Perhaps
there is some 'magic' value of sqlstate to differentiate various error
levels and I've chosen an 'Information' one rather than an 'Error' one?
Nov 12 '05 #4
Bob Stearns wrote:
Serge Rielau wrote:
Bob Stearns wrote:
My client wants to disallow a birth date in the future. First I tried
the obvious:

ALTER TABLE IS3.ANIMALS
ADD CONSTRAINT b_d_f
CHECK (birth_date<=current_date)

It failed because special registers (current_date) are not allowed in
check conditions. Then I tried the trigger below:

create trigger b_d_f
no cascade before
update of birth_date
on is3.animals
referencing
old as o
new as n
for each row
mode db2sql
when (n.birth_date>current_date)
signal sqlstate '99001' set message_text = 'Date must not
be in the future'

It created OK, but does not do what I expected, disallow the update
and return the error. Obviously I don't understand something, but
what? Any help would be greatly appreciated.

On first blush teh trigger looks all right.
I presume you have the matching INSERT trigger of course.
Can you post a complete repro?

Cheers
Serge

The insert trigger would be the same except changing update to insert,
correct?

Yep.
To bad there is no syntax for 'update or insert'; surely that
is the commonest case of data validation. I've heard of this request before....
NULLs are handled properly, since any comparison with NULL is false,
correct? NULLs are handled properly since very comparison to NULL is UNKNOWN....
That is not the same as FALSE.
The trigger as given would NOT fire with a NULL value since UNKNWON is
not the same as TRUE.
That would be hard since we're dealing with a php app. I was expecting
to get an error back like '22007--[IBM][CLI Driver][DB2/LINUX] SQL0180N
The syntax of the string representation of a datetime value is
incorrect. SQLSTATE=22007' which the program catches properly. Perhaps
there is some 'magic' value of sqlstate to differentiate various error
levels and I've chosen an 'Information' one rather than an 'Error' one?

Not quite clear what "that" might be, but the error you are getting is a
bind-in error. You never reached the trigger, or the statement execution
for that matter.
SIGNAL always raises an error, never a warning.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5
Serge Rielau wrote:

Not quite clear what "that" might be, but the error you are getting is a
bind-in error. You never reached the trigger, or the statement execution
for that matter.
SIGNAL always raises an error, never a warning.

Cheers
Serge

'That' was a reference to your request for a complete repro, sorry about
the poor antecedent.

Thanks for the suggestion of a bind-in problem. The whole problem
appears to be one of timing. Apparently I made some type of error
(perhaps reloading the page to cause the error) which caused the trigger
not to be invoked. It is all working as expected.

Once I have this vetted by my client, he'll want a lot of data elements
(in different tables) which are dates, but not all dates, to have such
triggers, all identical except for the trigger name, the table name, and
the variable name. Is there any form of syntactic "sugar" in the db2
product that would allow something like C's #define for such purpose? A
metametadata definition if you would? Or perhaps making a definition of
a PREVIOUS_DATE domain (based on DATE, so all the relevant date
operations would work) which includes the trigger?
Nov 12 '05 #6
Bob Stearns wrote:
Serge Rielau wrote:

Not quite clear what "that" might be, but the error you are getting is
a bind-in error. You never reached the trigger, or the statement
execution for that matter.
SIGNAL always raises an error, never a warning.

Cheers
Serge


'That' was a reference to your request for a complete repro, sorry about
the poor antecedent.

Thanks for the suggestion of a bind-in problem. The whole problem
appears to be one of timing. Apparently I made some type of error
(perhaps reloading the page to cause the error) which caused the trigger
not to be invoked. It is all working as expected.

Once I have this vetted by my client, he'll want a lot of data elements
(in different tables) which are dates, but not all dates, to have such
triggers, all identical except for the trigger name, the table name, and
the variable name. Is there any form of syntactic "sugar" in the db2
product that would allow something like C's #define for such purpose? A
metametadata definition if you would? Or perhaps making a definition of
a PREVIOUS_DATE domain (based on DATE, so all the relevant date
operations would work) which includes the trigger?

To script the DDL you can pick a language of your choice.
If you want to stay in SQL you could write an SQL Procedure that goes
fishing for all the date column in SYSCAT.TABLEs and glues together and
created the trigger using EXECUTE IMMEDIATE.
You can also use PHP, shell scripting....

You could overload the DATE() function generated by CREATE DISTINCT
TYPE. Something like:
CREATE DISTINCT TYPE UDT_SCHEMA.VALID_DATE FOR DATE WITH COMPARISONS;

CREATE FUNCTION MY_SCHEMA.DATE(arg DATE)
RETURNS UDT_SCEHMA.VALID_DATE
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
RETURN CASE WHEN arg <= CURRENT DATE
THEN UDT_SCHEMA.VALID_DATE(arg)
ELSE RAISE_ERROR('78000', 'Date in the future') END

CREATE FUNCTION MY_SCHEMA.DATE(arg UDT_SCHEMA.VALID_DATE)
RETURNS DATE
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
RETURN UDT_SCHEMA.DATE(arg);

Make sure UDT_SCHEMA is NOT in the function path while MY_SCHEMA is,
and/or REVOKE EXECUTE privileges from UDT_SCHEMA.DATE() and
UDT_SCHEMA.VALID_DATE()

I have never actually done this, so no guarantee :-)

Note that DB2 does not support user defined CASTs, so you need to use
the functional notation (Many folks use it anyway).
This trick should enable implicit conversion as well (which IMHO is a
good thing).

Have fun.
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #7
Serge Rielau wrote:
Bob Stearns wrote:
Serge Rielau wrote:

Not quite clear what "that" might be, but the error you are getting
is a bind-in error. You never reached the trigger, or the statement
execution for that matter.
SIGNAL always raises an error, never a warning.

Cheers
Serge

'That' was a reference to your request for a complete repro, sorry
about the poor antecedent.

Thanks for the suggestion of a bind-in problem. The whole problem
appears to be one of timing. Apparently I made some type of error
(perhaps reloading the page to cause the error) which caused the
trigger not to be invoked. It is all working as expected.

Once I have this vetted by my client, he'll want a lot of data
elements (in different tables) which are dates, but not all dates, to
have such triggers, all identical except for the trigger name, the
table name, and the variable name. Is there any form of syntactic
"sugar" in the db2 product that would allow something like C's #define
for such purpose? A metametadata definition if you would? Or perhaps
making a definition of a PREVIOUS_DATE domain (based on DATE, so all
the relevant date operations would work) which includes the trigger?


To script the DDL you can pick a language of your choice.
If you want to stay in SQL you could write an SQL Procedure that goes
fishing for all the date column in SYSCAT.TABLEs and glues together and
created the trigger using EXECUTE IMMEDIATE.
You can also use PHP, shell scripting....

You could overload the DATE() function generated by CREATE DISTINCT
TYPE. Something like:
CREATE DISTINCT TYPE UDT_SCHEMA.VALID_DATE FOR DATE WITH COMPARISONS;

CREATE FUNCTION MY_SCHEMA.DATE(arg DATE)
RETURNS UDT_SCEHMA.VALID_DATE
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
RETURN CASE WHEN arg <= CURRENT DATE
THEN UDT_SCHEMA.VALID_DATE(arg)
ELSE RAISE_ERROR('78000', 'Date in the future') END

CREATE FUNCTION MY_SCHEMA.DATE(arg UDT_SCHEMA.VALID_DATE)
RETURNS DATE
CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION
RETURN UDT_SCHEMA.DATE(arg);

Make sure UDT_SCHEMA is NOT in the function path while MY_SCHEMA is,
and/or REVOKE EXECUTE privileges from UDT_SCHEMA.DATE() and
UDT_SCHEMA.VALID_DATE()

I have never actually done this, so no guarantee :-)

Note that DB2 does not support user defined CASTs, so you need to use
the functional notation (Many folks use it anyway).
This trick should enable implicit conversion as well (which IMHO is a
good thing).

Have fun.
Serge

Thank you very much for the ideas.
Nov 12 '05 #8
Bob Stearns wrote:
Serge Rielau wrote:
This trick should enable implicit conversion as well (which IMHO is a
good thing).

DiSABLE that is (still a good thing).....

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #9

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

Similar topics

4
by: Dale | last post by:
Hi Everyone, I've got a form that provides a pop-up calendar for users to select dates for requesting jobs to be completed. The calendar works great, but it unfortunately allows users to select...
52
by: Andy Dingley | last post by:
I'm using this at present: <p title="Publication date" ></p> Works fine on screen, but Fangs/Jaws just reads it as "left bracket twenty-eight slash zero slash two thousand five fifteen colon...
1
by: David B | last post by:
In a reservations app I have a sub form (in datasheet view) that displays dates in desc order. As these dates are both in the past and present I would like current dates to appear when the form...
3
by: Lumpierbritches | last post by:
Can this be done in access by a lay person? Also, are there any tutorials for this type of addition to your applications? I'm trying to use a calendar control to set a date in the future for a to...
8
by: MLH | last post by:
Anybody's solution would be appreciated. Pls, do not pause to write anything for this. I'm not looking for that kind of a handout. I have an idea about how to do it, but I wanted to see if anyone...
14
by: jpr | last post by:
Friends, I have a form with four fields, date1, date2, date3 and date4. All these have all a mm/dd/yyyy format and have their source to a table. I need to add an unbound control (I will name...
5
by: AAJ | last post by:
Hi Does anyone know of any good publically available set of standards for managing dates when dealing with a database server (in my case SQL Server 2000 and c# VS2005). At the moment, if I...
18
by: mlcampeau | last post by:
I have a lengthy query that I am now trying to filter. The query calculates an employee's Anniversary Date in which they are eligible for the next level of Annual Vacation. (i.e. For 1-6 years of...
3
by: NDayave | last post by:
How Do, Is there any way to get a drop down menu to show all dates into the future, without entering them into a table beforehand? I really don't want to use the calendar control, but I need to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...
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
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...
0
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...

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.