问题
select SEQ_TT_EVENT.nextval,A.* from (
select 'a1','b1' from dual union all
select 'a2','b2' from dual union all
select 'a3','b3' from dual
) A;
上面的语法是正确的,结果如下:
select A.* from (
select SEQ_TT_EVENT.nextval,'a1','b1' from dual union all
select SEQ_TT_EVENT.nextval,'a2','b2' from dual union all
select SEQ_TT_EVENT.nextval,'a3','b3' from dual
) A;
这样的语法报错:ORA-02287: 此处不允许序号
.
这样也报错:
原因
是oracle不让这样使用,具体说明如下:
Restrictions on Sequence Values You cannot use CURRVAL and NEXTVAL in the
following constructs:
■ A subquery in a DELETE, SELECT, or UPDATE statement
■ A query of a view or of a materialized view
■ A SELECT statement with the DISTINCT operator
■ A SELECT statement with a GROUP BY clause or ORDER BY clause
■ A SELECT statement that is combined with another SELECT statement with the
UNION, INTERSECT, or MINUS set operator
■ The WHERE clause of a SELECT statement
■ The DEFAULT value of a column in a CREATE TABLE or ALTER TABLE statement
■ The condition of a CHECK constrain
解决方案就是用第一种sql那种写法。
另一种解决方案:
很多oracle语句在使用的时候会有限制,但是Function在大多数情况下没有限制,我们可以通过程序来获取nextval以及currval.
-- 获取序列下一个值
create or replace function get_seq_next (seq_name in varchar2) return number
is
seq_val number ;
begin
execute immediate 'select '|| seq_name|| '.nextval from dual' into seq_val ;
return seq_val ;
end get_seq_next;
SELECT id,name FROM (select get_seq_next('SEQ_B_LOG_ID') id , 'elong_deo' name from dual);
https://blog.csdn.net/qq525099302/article/details/43053291
https://blog.csdn.net/ludonqin/article/details/51450719