Foreach & Concactenation
Posted in 2003
Topics: Stored Procedures & SPL
I'm trying to concatenate the values of a field within a foreach
statement, and return the whole string.
Create procedure informix.sp_elililly() Returning char (255); Define sdesc char (255);
Define sLongDesc char (255);
Let sLongDesc = '';
ForEach
Select tddesc Into sDesc From TimeDesc Where tindex = 2478770
Let sLongDesc = sLongDesc || sDesc;
End Foreach;
Return sLongDesc;
End Procedure;
Unfortuantely I get one record returned, but empty...I guess I'm going
about this wrong...any ideas?
CHAR variables are always as long as they are declared. If new value is too
short, then variable is filled with space characters. What you need to do is
use VARCHAR or CLIP before concatenating:
...
FOREACH
SELECT tddesc INTO sDesc FROM TimeDesc WHERE tindex = 2478770
LET sLongDesc = sLongDesc CLIPPED || sDesc
END FOREACH
...
But I prefer using VARCHAR.
Gorazd
"Steve Schroeder" <zamdrist@pconline.com> wrote in message
news:c13afac3.0309260805.69448951@posting.google.com...
> I'm trying to concatenate the values of a field within a foreach
> statement, and return the whole string.
>
> Create procedure informix.sp_elililly() Returning char (255);> Define sdesc char (255);
> Define sLongDesc char (255);
>
> Let sLongDesc = '';
>
> ForEach
> Select tddesc Into sDesc From TimeDesc Where tindex = 2478770
> Let sLongDesc = sLongDesc || sDesc;
> End Foreach;
>
> Return sLongDesc;
> End Procedure;
>
> Unfortuantely I get one record returned, but empty...I guess I'm going
> about this wrong...any ideas?
corrected inline.
"Steve Schroeder" <zamdrist@pconline.com> wrote in message
news:c13afac3.0309260805.69448951@posting.google.com...
> I'm trying to concatenate the values of a field within a foreach
> statement, and return the whole string.
>
Create procedure informix.sp_elililly() Returning char (255); Define sdesc varchar (255);
Define sLongDesc varchar (255);
-- the field should be of type varchar instead of char.
Let sLongDesc = '';
ForEach
Select tddesc Into sDesc From TimeDesc Where tindex = 2478770
Let sLongDesc = trim(sLongDesc) || trim(sDesc);
End Foreach;
Return sLongDesc;
End Procedure;
rk-
ps: email id is bogus. reply to srkrishna at yahoo dot com.
>
> Unfortuantely I get one record returned, but empty...I guess I'm going
> about this wrong...any ideas?