問題場景一:
SELECT id,name FROM (select SEQ_B_LOG_ID.NEXTVAL id , 'elong_deo' name from dual);
insert into b_authority (id,role_id,authority,remark,url,yn,parent_id,authority_type,log_flag) select SEQ_B_AUTHORITY_ID.NEXTVAL,1, 'admin:role:listRole', '角色分頁查詢', '/admin/role/listRole.htm', 1,210,4, 1 from dual union select SEQ_B_AUTHORITY_ID.NEXTVAL,1, 'admin:role:toEditAuthority', '跳轉角色權限編輯', '/admin/role/toEditAuthority.htm', 1,210,4, 1 from dual union select SEQ_B_AUTHORITY_ID.NEXTVAL,1, 'admin:role:findAuthsByRoleId', '獲取角色權限', '/admin/role/findAuthsByRoleId.htm', 1,210,4, 1 from dual union select SEQ_B_AUTHORITY_ID.NEXTVAL,1, 'admin:role:updateRoleAuths', '更新角色權限', '/admin/role/updateRoleAuths.htm', 1,210,4, 1 from dual;
Restrictions on Sequence Values You cannot use CURRVAL and NEXTVAL in the
following constructs:問題解決之避免:
所謂的避免指的是不走入oracle序列的禁區,也就是盡量不要符合上述幾個情況,通過合理更改SQL語句達到我們的目的。
場景一解決:
SELECT SEQ_B_LOG_ID.NEXTVAL id ,name FROM (select 'elong_deo' name from dual);
場景二解決:
insert into b_authority (id,role_id,authority,remark,url,yn,parent_id,authority_type,log_flag) select SEQ_B_AUTHORITY_ID.NEXTVAL,t.c1,t.c2,t.c3,t.c4,t.c5,t.c6,t.c7 from (select 1 c1, 'admin:role:listRole' c2, '角色分頁查詢' c3, '/admin/role/listRole.htm' c4, 1 c5,210 c6,4 c7, 1 c8 from dual union all select 1, 'admin:role:toEditAuthority', '跳轉角色權限編輯', '/admin/role/toEditAuthority.htm', 1,210,4, 1 from dual union all select 1, 'admin:role:findAuthsByRoleId', '獲取角色權限', '/admin/role/findAuthsByRoleId.htm', 1,210,4, 1 from dual union all select 1, 'admin:role:updateRoleAuths', '更新角色權限', '/admin/role/updateRoleAuths.htm', 1,210,4, 1 from dual) t;
問題解決之另類強制執行:
很多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;
-- 獲取序列當前值(無需先執行nextval也可使用) create or replace function get_seq_curr (seq_name in varchar2) return number is seq_val number ; begin execute immediate 'select '|| seq_name|| '.currval from dual' into seq_val ; return seq_val ; end get_seq_curr;
場景一解決:
SELECT id,name FROM (select get_seq_next('SEQ_B_LOG_ID') id , 'elong_deo' name from dual);
場景二解決:
insert into b_authority (id,role_id,authority,remark,url,yn,parent_id,authority_type,log_flag) select get_seq_next('SEQ_B_AUTHORITY_ID'),1, 'admin:role:listRole', '角色分頁查詢', '/admin/role/listRole.htm', 1,210,4, 1 from dual union select get_seq_next('SEQ_B_AUTHORITY_ID'),1, 'admin:role:toEditAuthority', '跳轉角色權限編輯', '/admin/role/toEditAuthority.htm', 1,210,4, 1 from dual union select get_seq_next('SEQ_B_AUTHORITY_ID'),1, 'admin:role:findAuthsByRoleId', '獲取角色權限', '/admin/role/findAuthsByRoleId.htm', 1,210,4, 1 from dual union select get_seq_next('SEQ_B_AUTHORITY_ID'),1, 'admin:role:updateRoleAuths', '更新角色權限', '/admin/role/updateRoleAuths.htm', 1,210,4, 1 from dual;