8

我想count(*)在动态 plsql 语句中获得价值。我们可以将静态 stmt 写为:

select count(*) into tmp_cnt from table_info where nbr_entry='0123456789';

但是如何tmp_cnt在编写动态 sql 语句时获取值?或任何其他方式将count(*)价值转化为tmp_cnt变量?

4

2 回答 2

11

也许不同的 oracle 版本,但对我有用的是:

...
execute immediate 'select count(*) from ' || p_table_name into l_count;
...
于 2015-01-30T14:22:33.853 回答
9

您可以通过 EXECUTE IMMEDIATE ... RETURNING INTO 来实现它:

function count_rows(p_table_name varchar2)
  return number
is
  l_count number;
begin
  execute immediate 'select count(*) from ' || p_table_name into l_count;
  return l_count;
end count_rows;
于 2012-07-23T07:46:16.517 回答