create or replace procedure p_stuinfo is
v_sql varchar2(45);
begin
EXECUTE IMMEDIATE 'create table system.t_studentinfo(
stuid varchar2(10),
stuname varchar2(12),
score varchar2(10)
)';
EXECUTE IMMEDIATE 'insert into system.t_studentinfo values(''1'',''张三'',''A'')';
EXECUTE IMMEDIATE 'insert into system.t_studentinfo values(''2'',''李四'',''B'')';
commit;
v_sql:= 'select stuid,stuname,score from system.t_studentinfo';
for stu_record in (execute immediate v_sql) loop
dbms_output.put_line(stu_record.stuid||' '||stu_record.stuname||' '||stu_record.score);
end loop;
end;
v_sql varchar2(45);
begin
EXECUTE IMMEDIATE 'create table system.t_studentinfo(
stuid varchar2(10),
stuname varchar2(12),
score varchar2(10)
)';
EXECUTE IMMEDIATE 'insert into system.t_studentinfo values(''1'',''张三'',''A'')';
EXECUTE IMMEDIATE 'insert into system.t_studentinfo values(''2'',''李四'',''B'')';
commit;
v_sql:= 'select stuid,stuname,score from system.t_studentinfo';
for stu_record in (execute immediate v_sql) loop
dbms_output.put_line(stu_record.stuid||' '||stu_record.stuname||' '||stu_record.score);
end loop;
end;
for stu_record in (execute immediate v_sql) loop --这句不对,不能这么用
可以改成
for stu_record in (select stuid,stuname,score from system.t_studentinfo) loop
v_sql varchar2(200);
stu_cur sys_refcursor;
v_stuid varchar2(10);
v_stuname varchar2(12);
v_score varchar2(10);
begin
EXECUTE IMMEDIATE 'create table system.t_studentinfo(
stuid varchar2(10),
stuname varchar2(12),
score varchar2(10)
)';
EXECUTE IMMEDIATE 'insert into system.t_studentinfo values(''1'',''张三'',''A'')';
EXECUTE IMMEDIATE 'insert into system.t_studentinfo values(''2'',''李四'',''B'')';
commit;
v_sql:= 'select stuid,stuname,score from system.t_studentinfo';
open stu_cur for v_sql;
loop
fetch stu_cur into v_stuid,v_stuname,v_score;
exit when stu_cur%notfound;
dbms_output.put_line(v_stuid||' '||v_stuname||' '||v_score);
end loop;
end;