-
FETCH into sqlda structure
I am using DB2 UDB V8.2 on AIX. We recently added some VARCHAR fields
into our database and I found an issue with how we are reading data
when retrieving it into an sqlda structure using dynamic sql. Here is
the issue:
This particular table has several VARCHAR(256) fields. I set up a few
records that spanned all 256 char bytes then ran the debugger. I
found that I am only receiving the first 254 bytes of data in sqlda-
>sqvar.sqldata. I understand that the first two bytes in sqldata are
for the length but can anyone tell me why all 256 bytes of data are
not coming through?
Sample of the data in the database where two columns that are both
VARCHAR(256) with a record that has all 256 bytes inserted in each
field:
USERNAME
PASSWORD
-------------- --------------
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
2222222222222456
777777777777777777777777777777777777777777777777777777777777777
77777777777777777777777777777777777777777777777777777777777777777777777777777777
77777777777777777777777777777777777777777777777777777777777777777777777777777777
777777777777777777777777777777768
So now I run my program that fetches the data into the sqlda-
>sqlvar.sqldata and this is what I see:
(dbx) p sqldata[0]
'^A' <----length is 256
(dbx) p sqldata[1]
'\0' <---part of length field?
(dbx) p sqldata[2]
'2' <----start of the USERNAME
data
(dbx) p sqldata[3]
'2'
(dbx) p sqldata[253]
'2'
(dbx) p sqldata[254]
'2'
(dbx) p sqldata[255]
'4' <-----254th byte of the data (or
253rd if you are looking at the index)
(dbx) p sqldata[256]
'^A' <---this should be a value of 5
but instead it is the length for the PASSWORD field
(dbx) p sqldata[257]
'\0'
(dbx) p sqldata[258]
'7' <---start of the PASSWORD field
(dbx) p sqldata[259]
'7'
so it seems I am only getting back a total of 256 bytes including the
length fields and that truncates the data. I found the following
statement by Knut in a different post and it brought up some
questions:
"No, not for VARCHAR data. The string 'some text' requires 12 bytes
on disc
(2 length + 1 null-indicator + 9 data bytes) whereas 'some more text'
uses
17 bytes on disc (2 + 1 + 14 data bytes). "
1) does this mean there could be 2-3 fields for length + null and that
this could vary depending on the length?
2) does this also mean that even though I could insert 256 bytes of
data into the VARCHAR(256) we can only use 253 for data to account for
the 3 extra bytes in sqldata? ( would be suprised if the answer is
yes!)
or
did we just do something wrong with the lengths somewhere in the
call? sqllen is set to 256 after the DESCRIBE...should the
application set this to 259?
I appreciate your time.
-
Re: FETCH into sqlda structure
Has anyone had any experience with this? I would appreciate any help
if you are using this technique.
-
Re: FETCH into sqlda structure
"shorti" escribió en el mensaje
news:0b0c6b01-1704-4709-826e-5665211fa120@m44g2000hsc.googlegroups.com...
> ... I
> found that I am only receiving the first 254 bytes of data in sqlda-
> >sqvar.sqldata. I understand that the first two bytes in sqldata are
> for the length but can anyone tell me why all 256 bytes of data are
> not coming through?
>
Two extra bytes at the begining of the receiving buffer sould be reserved in
order to acomodate varchar length.
And sqlvar.sqllen should be increased in 2 too.
Hope this helps
Vicente
-
Re: FETCH into sqlda structure
On Jul 29, 5:52*am, "VGD" wrote:
> "shorti" escribió en el mensajenews:0b0c6b01-1704-4709-826e-5665211fa120@m44g2000hsc.googlegroups.com...
>
> > ... I
> > found that I am only receiving the first 254 bytes of data in sqlda-
> > >sqvar.sqldata. *I understand that the first two bytes in sqldata are
> > for the length but can anyone tell me why all 256 bytes of data are
> > not coming through?
>
> Two extra bytes at the begining of the receiving buffer sould be reservedin
> order to acomodate varchar length.
> And sqlvar.sqllen should be increased in 2 too.
>
> Hope this helps
>
> * * Vicente
Does this mean that after the sqlvar.sqllen is returned from the
"DESCRIBE s1 INTO: SQLDA" I am required to add 2 to this length for
all VARCHARs before the "FETCH c1 USING DESCRIPTOR:SQLDA"? (i.e.
sqlvar.sqllen += 2 ) otherwise 2 bytes will always be cut off of the
sqldata for variable length?
That is one of the reasons why I am asking...it seems strange that DB2
would go through all that work to design this nice process of holding
a varying amount of data only to require the user to manually add the
additional 2 bytes length for a VARCHAR. I still feel I am missing
something...or is this truly just a hiccup in the design?
Any imput from our DB2 specialists ??