Oracle 序列:此处不允许使用 CURRVAL?

发布于 2024-09-11 13:38:19 字数 428 浏览 2 评论 0原文

以下 Oracle SQL 代码生成错误“ORA-02287:此处不允许序列号”:

INSERT INTO Customer (CustomerID,Name) VALUES (Customer_Seq.nextval,'AAA');
SELECT * FROM Customer where CustomerID=Customer_Seq.currval;

该错误发生在第二行(SELECT 语句)。我真的不明白这个问题,因为这确实有效:

INSERT INTO Customer (CustomerID,Name) VALUES (Customer_Seq.nextval,'AAA');
SELECT Customer_Seq.currval from dual;

The following Oracle SQL code generates the error "ORA-02287: sequence number not allowed here":

INSERT INTO Customer (CustomerID,Name) VALUES (Customer_Seq.nextval,'AAA');
SELECT * FROM Customer where CustomerID=Customer_Seq.currval;

The error occurs on the second line (SELECT statement). I don't really understand the problem, because this does work:

INSERT INTO Customer (CustomerID,Name) VALUES (Customer_Seq.nextval,'AAA');
SELECT Customer_Seq.currval from dual;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜尕妞 2024-09-18 13:38:19

您已经发布了一些示例代码,因此不清楚您想要实现什么目标。如果你想知道分配的值,比如说传递给其他过程,你可以这样做:

SQL> var dno number
SQL> insert into dept (deptno, dname, loc)
  2      values (deptno_seq.nextval, 'IT', 'LONDON')
  3      returning deptno into :dno
  4  /

1 row created.

SQL> select * from dept
  2  where deptno = :dno
  3  /

    DEPTNO DNAME          LOC
---------- -------------- -------------
        55 IT             LONDON

SQL>

编辑

我们可以使用 RETURNING 子句来获取任何列的值,包括那些已经被赋值的列。使用默认值或触发代码设置。

You have posted some sample code, so it is not clear what you are trying to achieve. If you want to know the assigned value, say for passing to some other procedure you could do something like this:

SQL> var dno number
SQL> insert into dept (deptno, dname, loc)
  2      values (deptno_seq.nextval, 'IT', 'LONDON')
  3      returning deptno into :dno
  4  /

1 row created.

SQL> select * from dept
  2  where deptno = :dno
  3  /

    DEPTNO DNAME          LOC
---------- -------------- -------------
        55 IT             LONDON

SQL>

Edit

We can use the RETURNING clause to get the values of any column, including those which have been set with default values or by trigger code.

許願樹丅啲祈禱 2024-09-18 13:38:19

你没有说你正在使用什么版本的Oracle。过去,序列在 PL/SQL 中的使用位置存在限制——在 11G 中大部分(如果不是全部)都消失了。此外,SQL 中也存在限制 - 请参阅此列表

在这种情况下,您可能需要编写:(

SELECT Customer_Seq.currval INTO v_id FROM DUAL; 
SELECT * FROM Customer where CustomerID=v_id;

在评论后编辑)。

You don't say what version of Oracle you are using. There have in the past been limitations on where sequences can be used in PL/SQL - mostly if not all gone in 11G. Also, there are restrictions in SQL - see this list.

In this case you may need to write:

SELECT Customer_Seq.currval INTO v_id FROM DUAL; 
SELECT * FROM Customer where CustomerID=v_id;

(Edited after comments).

緦唸λ蓇 2024-09-18 13:38:19

这并不能真正直接回答您的问题,但也许您想要做的事情可以使用 INSERT 的 RETURNING 子句来解决?

DECLARE
  -- ...
  last_rowid rowid;
  -- ...
BEGIN
  -- ...
  INSERT INTO Customer (CustomerID,Name) VALUES (Customer_Seq.nextval,'AAA') RETURNING rowid INTO last_rowid;
  SELECT * FROM Customer where rowid = last_rowid;
  -- ...
END;
/

This doesn't really directly answer your question, but maybe what you want to do can be resolved using a the INSERT's RETURNING clause?

DECLARE
  -- ...
  last_rowid rowid;
  -- ...
BEGIN
  -- ...
  INSERT INTO Customer (CustomerID,Name) VALUES (Customer_Seq.nextval,'AAA') RETURNING rowid INTO last_rowid;
  SELECT * FROM Customer where rowid = last_rowid;
  -- ...
END;
/
信愁 2024-09-18 13:38:19

您不能在 WHERE 子句中使用序列 - 它在您的上下文中看起来确实很自然,但 Oracle 不允许在比较表达式中进行引用。

[编辑]

这将是一个 PL/SQL 实现:

declare
v_custID number;
cursor custCur is
  select customerid, name from customer
   where customerid = v_custID;
begin
select customer_seq.nextval into v_custID from dual;
insert into customer (customerid, name) values (v_custID, 'AAA');
commit;
for custRow in custCur loop
 dbms_output.put_line(custRow.customerID||' '|| custRow.name); 
end loop;
end;

You may not use a sequence in a WHERE clause - it does look natural in your context, but Oracle does not allow the reference in a comparison expression.

[Edit]

This would be a PL/SQL implementation:

declare
v_custID number;
cursor custCur is
  select customerid, name from customer
   where customerid = v_custID;
begin
select customer_seq.nextval into v_custID from dual;
insert into customer (customerid, name) values (v_custID, 'AAA');
commit;
for custRow in custCur loop
 dbms_output.put_line(custRow.customerID||' '|| custRow.name); 
end loop;
end;
南城旧梦 2024-09-18 13:38:19

您还没有创建任何

sequence 

首先创建任何序列其循环和缓存。这是一些基本示例

Create Sequence seqtest1
Start With 0             -- This Is Hirarchy Starts With 0
Increment by 1           --Increments by 1
Minvalue 0               --With Minimum value 0
Maxvalue 5               --Maximum Value 5. So The Cycle Of Creation Is Between 0-5
Nocycle                  -- No Cycle Means After 0-5 the Insertion Stopes
Nocache   --The cache Option Specifies How Many Sequence Values Will Be Stored In Memory For Faster Access

您不能在 SQL 中对序列执行Where 子句,因为您无法过滤序列。使用@APC 所说的程序

You have not created any

sequence 

First create any sequence its cycle and cache. This is some basic example

Create Sequence seqtest1
Start With 0             -- This Is Hirarchy Starts With 0
Increment by 1           --Increments by 1
Minvalue 0               --With Minimum value 0
Maxvalue 5               --Maximum Value 5. So The Cycle Of Creation Is Between 0-5
Nocycle                  -- No Cycle Means After 0-5 the Insertion Stopes
Nocache   --The cache Option Specifies How Many Sequence Values Will Be Stored In Memory For Faster Access

You cannot do Where Clause on Sequence in SQL beacuse you cannot filter a sequence . Use procedures like @APC said

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文