我是oracle新手,请帮忙。
我写了一个类似于 here 的函数在 schemaB 中,稍加改动。
当我在 schemaA 中从上述函数运行查询时,它运行良好并返回序列。
select schemaB.sequence_name_seq.nextVal from dual;
从 schemaB 运行函数当然会返回预期的序列。
但是,当我尝试从 schemaA 访问相同的函数(包含上述查询)时,它给了我一个错误: “ORA-00904:无效标识符”
我已向 schemaA 的 userA 授予 EXECUTE 权限(从“DBA_TAB_PRIVS”表确认)。
功能:
create or replace Function nextSeq
(
tableName in VARCHAR2
)return NUMBER as
nextNum Number;
begin
EXECUTE IMMEDIATE 'select '||tableName||'_SEQ.nextval from dual' into nextNum;
return nextNum;
END nextSeq;
调用:
select nextSeq('SCHEMAB.TABLENAME') from dual;
最佳答案
我的做法是这样的:作为 SCOTT
连接,我正在创建一个序列和一个函数;然后我将对该函数的 EXECUTE
授予用户 MIKE
:
SQL> show user
USER is "SCOTT"
SQL> create sequence dept_seq;
Sequence created.
SQL> create or replace function nextseq (tablename in varchar2)
2 return number
3 as
4 nextnum number;
5 begin
6 execute immediate 'select ' || tablename||'_seq.nextval from dual' into nextnum;
7 return nextnum;
8 end;
9 /
Function created.
SQL> select nextseq('dept') from dual;
NEXTSEQ('DEPT')
---------------
1
SQL> grant execute on nextseq to mike;
Grant succeeded.
SQL>
作为 MIKE
连接并使用 SCOTT
的功能:
SQL> connect mike/lion@xe
Connected.
SQL> show user
USER is "MIKE"
SQL> select scott.nextseq('dept') from dual;
SCOTT.NEXTSEQ('DEPT')
---------------------
2
SQL>
如您所见,它有效。与您的代码相比,差异如下:
You: select nextSeq('SCHEMAB.TABLENAME') from dual;
Me : select scott.nextseq('dept') from dual;
不应在表名称前面加上所有者,而是函数名称。
关于oracle - 访问函数 - ORA-00904 : invalid identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52354331/