declare
     sql_string varchar2(200);
     nyr varchar2(8):='20081001';
     v_n number;
begin
     sql_string:='select count(*) from dm_mpg_test where nyr='||nyr;
     dbms_output.put_line(sql_string);

     execute immediate sql_string into v_n;
     dbms_output.put_line(v_n); --输出值显示
end;
execute immediate不能执行多行查询,要输出多行查询结果可使用游标

declare
     v_nyr varchar2(8):='20081001';
     v_kh varchar2(10);
     cursor v_cursor is
                    select * from dm_mpg_test where nyr=v_nyr;
begin
     open v_cursor;
                loop
                    fetch v_cursor into v_kh,v_nyr;
                            exit when v_cursor%notfound;
                            dbms_output.put_line('nyr:'||v_nyr);
                            dbms_output.put_line('kh:'||v_kh);
                end loop;
     close v_cursor;
end;