Julen,
What language is your host for the SQL? I understand that you are running on an as/400 but what is the host language that the sql is embedded in? Cobol? PL/1? Or is it a stored procedure or similar?
Any way, you should be able to put debugging displays into the language in order to identify the exact point of error. Have a look at other programs at your site to see how they determine errors - usually this type of thing is pre-defined, and all you have to do is clone it for your program.
If needs be, post the code here if it is not too big.
Hope this helps.
Marc