How to call a procedure with a rowtype literal as parameter in PL/SQL?

落爺英雄遲暮 提交于 2021-01-27 16:29:23

问题


Lets say I have a table and a procedure that accepts one argument of the tables rowtype:

CREATE TABLE t (a NUMBER, b NUMBER);

CREATE PROCEDURE p (x t%ROWTYPE) IS
BEGIN
  NULL;
END;

Can I call that procedure using a rowtype literal, that is without explicitly creating a rowtype variable (or at least not explicitly listing and assigning every field of it)? The two following approaches both generate the below error:

p(1, 2);
p((1, 2));

PLS-00306: wrong number or types of arguments in call to 'P'


回答1:


You could also construct the record from a cursor loop:

for r in (
    select 1, 2 from dual
)
loop
    p(r);
end loop;

Unfortunately PL/SQL records are just simple structures and don't come with constructors as object types do. (I wish they did.)




回答2:


This is not an optimal solution, since it (a) requires the creation of a variable and (b) isn't very pretty. But at least it works:

DECLARE
  x t%ROWTYPE;
BEGIN
  SELECT 1, 2 INTO x FROM dual;
  p(x);
END;

I am still interested in better solutions.



来源:https://stackoverflow.com/questions/33777381/how-to-call-a-procedure-with-a-rowtype-literal-as-parameter-in-pl-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!