我有employee1表,字段为student_id,名称,标记和带字段名称的表,假设NEHA出现在名称字段中,我有一个名为NEHA的表。在名称表标记中,id和subject字段在那里。我想要从名称表(neha)中获取主题,其中标记> 10。我需要编写sqlrpgle程序来通过动态查询从employee1获取名称,因为名称每次都在更改。我已经共享了代码。在此代码中,每次从emoployee1获取不同的名称,但此代码中的问题无法从NEHA表中获取SUBJECT列。在调试其即将到来的主题=' &#39 ;.但同样的查询在strsql中完美运行。任何人都建议任何解决方案吗?
D stmt1 s 500A inz
D subject s 10A
D @subject s 10A
D name s 20A
D @Name s 20A
D STUdent_ID s 10P 0
d marks s 10P 2
/Free
EXEC Sql
declare c1 scroll cursor for
select name,student_id,marks from employee1;
EXEC Sql
open c1;
EXEC Sql
fetch first from c1 into :@name,:student_id,:marks;
dow sqlcod=0;
stmt1='select subject from ' + %Trim(@name)+ ' where marks>10';
EXEC Sql
declare c2 scroll cursor for
select subject from @name where marks>10;
EXEC Sql
open c2;
EXEC sql
fetch first from c2 into :@SUBJECT;
If SQLCOD = 0;
dsply subject;
EndIf;
EXEC Sql
Prepare s2 from :stmt1;
EXEC Sql
execute s2;
EXEC SQL
Fetch next from c1 into :name,:student_id,:marks;
EXEC SQL
Fetch next from c2 into :subject;
enddo;
EXEC Sql
close c1;
EXEC Sql
close c2;
*inlr = *on;
/END-FREE
答案 0 :(得分:1)
因此,假设您有一个引用其他表名称的名称表,那么动态sql语句的顺序以及它们的调用方式就会出现问题。
exec sql
declare C1 cursor for
select name, student_id, marks from employee1;
exec sql
declare S2 statement;
exec sql
declare C2 cursor for S2;
exec sql
open C1;
exec sql
fetch first from C1 into :@name, :student_id, :marks;
dow sqlcode >= 0;
stmt1 = 'select subject from ' + %Trim(@name)+ ' where marks>10';
exec sql
prepare S2 from :stmt1;
exec sql
open C2;
exec sql
fetch first from c2 into :@SUBJECT;
if sqlcod = 0;
dsply subject;
endif;
exec sql
close C2;
exec sql
fetch next from C1 into :@name, :student_id, :marks;
enddo;
exec sql
close C1;
我把所有声明放在前面。它们不可执行,并且不需要重复调用,因为它们被完全注释掉并且在程序中不生成代码。在游标中使用动态预处理语句的顺序是:
关于执行的一些注意事项: