Wed, 14 Nov 2007

SQL0326

One day, I've created a program in the SQLRPG language, with RPG400 (and not RPGIV). It was something like the snippet shown below, except that it had about two dozens of fields in the SELECT clause.

C/EXEC SQL 
C+ DECLARE C1 CURSOR FOR SELECT A, B, C FROM T
C/END-EXEC
 * ...
C/EXEC SQL
C+ FETCH NEXT FROM C1 INTO :A, :B, :C
C/END-EXEC

When compiling, I've got the strange SQL0326 error message. A little command DSPMSGD SQL0326 QSQLMSG shows me that it meant Too many host variables specified. I restart counting again and again, but I definitely had the same number of host variables as the number of fields in my SELECT clause. Googling didn't help, seems like I didn't have the right search keywords or no one before encountered this issue. I tried every figure I could imagine, blindly, and after one day of searching, I finally found the trick!

Some fields in the database had 300 characters length. In fact, they represented in one string an array of 6 50 characters sub-elements. In my program, I declared the variable as an array of 6 elements of 50 charcters long. I thought it as a clever way to get automagically the value of each element, but that was the problem.

In an RPG/400 program, a string variable can have a length less that 256 characters. This doesn't apply to arrays, so, even if the total length of the variable array was 300, the compiler didn't see it as a problem. But at the FETCH instruction, I think fields having a length larger that 256 characters weren't considered. The solution was to, either use SUBSTR in my SELECT clause to limit the length of the strings fetch to be under 256, or CVTRPGSRC to pass to RPGIV, which does not have this limitation.

C/EXEC SQL 
C+ DECLARE C1 CURSOR FOR SELECT 
C+ SUBSTR(A, 1, 250) AS A1, SUBSTR(A, 251, 50) AS A2, B, C FROM T
C/END-EXEC
 * ...
C/EXEC SQL
C+ FETCH NEXT FROM C1 INTO :A1, :A2, :B, :C
C/END-EXEC
posté le: 14:16 | path: /data/as400/sqlrpg | permalien
Août 2010
Dim Lun Mar Mer Jeu Ven Sam
       

Malagasy miray
About

DotMG's joblog

Work hard at whatever you do. (Ecc. 9. 10a. CEV)
Valid XHTML 1.0 Transitional   Valid CSS!   Powered by blosxom 2.0