I have employee1 table with field student_id,name,marks and table with field name,suppose NEHA is present in name field,I have a table with name NEHA .In name table marks,id and subject field is there.I want to fetch subject from name table(neha) where marks >10. I need to write sqlrpgle program to fetch name from employee1 by dynamic query as name is changing each time.I have share the code. In this code each time different name is fetched from emoployee1 but the problem in this code is not able to fetch SUBJECT column from NEHA table.In debug its's coming subject=' '. but the same query run perfectly in strsql.could anyone suggest any solution?
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