Oracle 函数中动态执行语句

时间:2021-12-23 11:25:32
函数:
1 create or replace function fn_test(tablename in varchar2) return number is
sqls varchar2(100);
rtn number(10):=0;
begin
--获取学生表的记录条数
--select count(*) into rtn from student; sqls:='select count(*) from ' || tablename;
execute immediate sqls into rtn;
dbms_output.put_line('记录条数 rtn='|| rtn);
return(rtn);
end fn_test;

SQL:

 select fn_test('student') from dual;

Oracle 函数中动态执行语句

注意项:在拼接SQL语句的时候,查询语句 from 关键字与要拼接的表名之间必须要记得加空格。