sequence creation in oracle

夙愿已清 提交于 2020-01-13 10:23:06

问题


I want to create a sequence in oracle where the max value of the column field (Empid) must be the min value of the sequence.

The below was the one i found in our same stackexchange

create sequence mytemp_seq start with &v_Startval;

This command prompts me to enter the max value of teh column name which i have to enter.

How can I fix the value of &v_startval with out it prompting ,but directly setting the values from the below statement

select max(empid) from mytemp..

I am trying like this below

create sequence mytemp_seq start with (SELECT MAX(empid) from mytemp)

But it doesnt work.


回答1:


You could do it with some PL/SQL:

declare
  v_startval integer;
begin
  select max(empid)+1 into v_startval from mytemp;
  execute immediate 'create sequence mytemp_seq start with ' || v_startval;
end;



回答2:


In sqlplus you can do

col max_id new_value seq_min_val  
SELECT MAX(empid)+1 AS max_id from mytemp;
create sequence mytemp_seq start with &seq_min_val;


来源:https://stackoverflow.com/questions/6057191/sequence-creation-in-oracle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!