On 17-Jan-2012 13:40 , Cole Gantz wrote:
We have a process that reads an XML document via Java and does a SQL
insert into our DB2 tables on our iSeries. The problem is that the
vendor that is providing the document hasn't been able to provide us
with their field lengths for the data. While we have maybe 90% or so
of the data coming thru okay, about once every 6 months we find one
that will bomb our code, usually on a text field. Is there some way
to have SQL when it sees a truncation error actually truncate the
field and not error the insert?
Instead of allowing the SQL to detect that the value\expression being
inserted is too long, tell the SQL, using an expression [versus just a
literal, field, or variable], that the value to be inserted must first
be cast to the truncated result. Define the "truncated result" with a
type\length that matches the attributes of the column in the existing
TABLEs, into which the value\expression is being inserted.
Not knowing what is the particular syntax of the actual failing
INSERT statement being used, review each of the following pairs of
INSERT statements for possible similarity. If these examples do not
apply directly, then perhaps more details about what is the actual
INSERT being performed would help others to better understand and
assist. The first of each pair of INSERT requests will fail and the
second successfully inserts a truncated result, thus\while avoiding an
error. The CREATE TABLE just [loosely] defines an example column for
the TABLE as a target of the INSERT INTO:
<code>
create table qtemp/txtfield (t char(35))
;
-- syntax: INSERT INTO ... SELECT ...
-- CAST scalar
insert into qtemp/txtfield (t)
select /* selected string expression too long */
'value is longer than thirty five bytes'
from sysibm/sysdummy1
; -- Value for column or variable T too long.
insert into qtemp/txtfield (t)
select /* string expression cast to expected type\length */
CAST('value is longer than thirty five bytes' AS CHAR(35))
from sysibm/sysdummy1
; -- only char(35) amount of the string was inserted
-- syntax: INSERT INTO ... VALUES ...
-- CHAR [cast] scalar
insert into qtemp/txtfield (t) values
( 'value is longer than thirty five bytes' )
; -- Value for column or variable T too long.
insert into qtemp/txtfield (t) values
( CHAR('value is longer than thirty five bytes', 35) )
; -- only char(35) amount of the string was inserted
-- LEFT scalar
insert into qtemp/txtfield (t) values
( 'value is longer than thirty five bytes' )
; -- Value for column or variable T too long.
insert into qtemp/txtfield (t) values
( LEFT('value is longer than thirty five bytes', 35) )
; -- only char(35) amount of the string was inserted
</code>
Regards, Chuck
As an Amazon Associate we earn from qualifying purchases.