动态SQL 执行 declare v_col_name varchar2(30) := 'name'; --字段名 name 用变量来表示 v_user_name varchar2(30); --用户名称 v_user_age integer; --用户年龄 v_sql_str varchar2(500); --动态 SQL 语句 begin v_sql_str := 'select '||v_col_name||',age from users --字段名后面不能紧随 into 到变量了 where age between :start_age and :end_age and rownum=1'; --两个命名参数
--用 execute immediate 动态执行 SQL 语句 --注意其后的 into 字段值到变量的写法,还有 using 来代入参数
execute immediate v_sql_str into v_user_name,v_user_age using 18,25;
同意楼上说法: create or replace procedure pr_demo(p_con in varchar2) as v_sql varchar2(200); v_count number; begin v_sql:='select count(*) from tab where fldid=:1'; executte immediate v_sql into v_count using p_con; end;
declare
v_col_name varchar2(30) := 'name'; --字段名 name 用变量来表示
v_user_name varchar2(30); --用户名称
v_user_age integer; --用户年龄
v_sql_str varchar2(500); --动态 SQL 语句
begin
v_sql_str := 'select '||v_col_name||',age from users
--字段名后面不能紧随 into 到变量了
where age between :start_age and :end_age and rownum=1'; --两个命名参数
--用 execute immediate 动态执行 SQL 语句
--注意其后的 into 字段值到变量的写法,还有 using 来代入参数
execute immediate v_sql_str into v_user_name,v_user_age using 18,25;
dbms_output.put_line('第一个符合条件的用户:'||v_user_name||',年龄:'||v_user_age);
end;
create or replace procedure pr_demo(p_con in varchar2)
as
v_sql varchar2(200);
v_count number;
begin
v_sql:='select count(*) from tab where fldid=:1';
executte immediate v_sql into v_count using p_con;
end;