Oracle动态SQL返回单条结果和结果集
1. DDL 和 DML
2. 返回单条结果
500 );
c_1 );
r_1 test % rowtype;
c_1: select * from test where name=:c WHERE ROWNUM=1 execute immediate into r_1 using c_1;
DBMS_OUTPUT.PUT_LINE(R_1.NAME || R_1.ADDRESS);
;
3. 返回结果集
/*** DDL ***/
begin EXECUTE IMMEDIATE ' drop table temp_1 ' ;
create table temp_1(name varchar2(8)) end ;
** DML **declare
v_1 varchar2 ( 8 );
v_2 10 );
str 50 );
v_1: = 测试人员 ;
v_2: 北京 ;
: = INSERT INTO test (name,address) VALUES (:1,:2) USING v_1,v_2;
commit ;
begin EXECUTE IMMEDIATE ' drop table temp_1 ' ;
create table temp_1(name varchar2(8)) end ;
** DML **declare
v_1 varchar2 ( 8 );
v_2 10 );
str 50 );
v_1: = 测试人员 ;
v_2: 北京 ;
: = INSERT INTO test (name,address) VALUES (:1,:2) USING v_1,v_2;
commit ;
2. 返回单条结果
500 );
c_1 );
r_1 test % rowtype;
c_1: select * from test where name=:c WHERE ROWNUM=1 execute immediate into r_1 using c_1;
DBMS_OUTPUT.PUT_LINE(R_1.NAME || R_1.ADDRESS);
;
3. 返回结果集
CREATE
OR
REPLACE
package pkg_test
as
定义ref cursor类型
不加return类型,为弱类型,允许动态SQL查询,
否则为强类型,无法使用动态SQL查询;
type myrctype is ref cursor ;
-- 函数申明
function get(intID number ) return myrctype;
pkg_test;
/
package body pkg_test 函数体 myrctype
rc myrctype; 定义ref cursor变量 sqlstr if intID = 0 then 静态测试,直接用select语句直接返回结果 open rc for select id,name,sex,address,postcode,birthday from student;
else 动态sql赋值,用:w_id来申明该变量从外部获得 sqlstr : select id,birthday from student where id=:w_id 动态测试,用sqlstr字符串返回结果,用using关键词传递参数 sqlstr using intid;
rc;
get;
/
定义ref cursor类型
不加return类型,为弱类型,允许动态SQL查询,
否则为强类型,无法使用动态SQL查询;
type myrctype is ref cursor ;
-- 函数申明
function get(intID number ) return myrctype;
pkg_test;
/
package body pkg_test 函数体 myrctype
rc myrctype; 定义ref cursor变量 sqlstr if intID = 0 then 静态测试,直接用select语句直接返回结果 open rc for select id,name,sex,address,postcode,birthday from student;
else 动态sql赋值,用:w_id来申明该变量从外部获得 sqlstr : select id,birthday from student where id=:w_id 动态测试,用sqlstr字符串返回结果,用using关键词传递参数 sqlstr using intid;
rc;
get;
/