我在Oracle中有一个过程,它返回所有回答SQL语句的行。 该过程得到4个参数,其中3个使用in-said的SQL语句。向下看(代码)。 只有参数:in_table_name会导致错误,如何通过程序将table_name移动到SQL语句?(我尝试使用"&",但它没有& #39; t work)
create or replace Procedure return_rows_EMPLOYEE (in_order in number,
in_sorting in VARCHAR2, in_job in VARCHAR2, in_table_name in VARCHAR2, out_cursor OUT SYS_REFCURSOR) As
Begin
if in_order = 1 THEN
OPEN out_cursor for
select * from &in_table_name
WHERE EMPL_JOB_TITLE = in_job
ORDER BY in_sorting ASC;
else
OPEN out_cursor for
select * from &in_table_name
WHERE EMPL_JOB_TITLE = in_job
ORDER BY in_sorting DESC;
END if;
End;
答案 0 :(得分:0)
正如Rahul指出的那样,你不能使用表名作为参数。但我还要补充一点,你不能在USING
子句中使用表名作为EXECUTE IMMEDIATE
参数。您必须在查询中实际连接它。下面是(未经测试的!)结果查询。
create or replace Procedure return_rows_EMPLOYEE (in_order in number,
in_sorting in VARCHAR2, in_job in VARCHAR2, in_table_name in VARCHAR2, out_cursor OUT SYS_REFCURSOR)
As
query varchar2(4000);
Begin
if in_order = 1 THEN
query :=
'select * from ' || in_table_name ||
' WHERE EMPL_JOB_TITLE = :1
ORDER BY :2 ASC';
else
OPEN out_cursor for
query :=
'select * from ' || in_table_name ||
' WHERE EMPL_JOB_TITLE = :1
ORDER BY :2 DESC';
end if;
open out_cursor for query using in_job, in_sorting;
End;