How to create sequence when minvalue is unknown?

I am currently migrating a lot of tables (that already have data) over to using sequences. I'd like to have a script that creates these sequences without having to hardcode the minimum value that the sequence should start at.
I am trying to do something like this:
create sequence mySeq minvalue (select max(id)+1 from table) increment BY 1 cache 20;
This always gives the error "Invalid number". Can anyone provide some guidance on how to solve this problem?

Like this..?
SQL> declare
  2   min_val number;
  3   str varchar2(1000);
  4  begin
  5   select nvl(max(empno),0)+1
  6   into min_val
  7   from emp;
  8   str := 'create sequence seq1 start with '||min_val||' increment by 1 cache  20';
  9   execute immediate str;
10  end;
11  /
PL/SQL procedure successfully completed.
SQL> select seq1.nextval
  2  from dual;
   NEXTVAL
      7935
http://download-uk.oracle.com/docs/cd/B19306_01/appdev.102/b14251/adfns_dynamic_sql.htm#i1006172                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • How to create sequence

    How to create sequence(Auto generated no in oralce) with out using Triggers?
    If i create trigger sequence of numbers are getting generated but in case if i want to include explicit sequence no at the end of the table say like '9999'...it is not getting updated
    Pls help very urgent..

    user2134711 wrote:
    How to create sequence(Auto generated no in oralce) with out using Triggers? Sequence is an oracle object used to create unique number sequence. It has no connection with trigger.
    Generally Sequence is used in a Trigger to generate some column value automatically. Something like the IDENTITY column used in SQL Server.
    If i create trigger sequence of numbers are getting generated but in case if i want to include explicit sequence no at the end of the table say like '9999'...it is not getting updated This totally depends on how you have written the trigger. If you can show your trigger code we can help you out.

  • How to create reference, when an instance is created through  interface

    Hi All,
    How to create reference, when an instance is created through  interface.
    Regards,
    Lisa

    hi
    good
    http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5c54f411d194a60000e8353423/content.htm
    http://help.sap.com/saphelp_nw2004s/helpdata/en/c3/225b5f54f411d194a60000e8353423/content.htm
    /people/jocelyn.dart/blog/2006/06/28/getting-started-with-abap-oo-for-workflow-using-the-ifworkflow-interface
    THANKS
    MRUTYUN

  • How to CREATE SEQUENCE in one table

    dear all
    the one thing i want to know when i use CREATE SEQUENCE in one table like this and then at that table
    CREATE SEQUENCE oproduct_sequence
    START WITH 1  INCREMENT BY 1 
    nocache
    create table oproduct(
    tname varchar2(20) not null,
    tid int default  oproduct_sequence.nextval
    the system indicat that i cannot use this way to create table , so i really want to know how can i achieve this method
    becuase when i want to insert inot table oproduct only use
    insert into oproduct  valuse('aaaa');
    and the result like
    aaaaa 1
    bbbb   2

    Actual name is before insert trigger. Some examples are listed below:
    Example Number 1 ...
    create sequence product_seq start with 1 increment 1
    create or replace trigger product_insert before insert for each row begin
    select productseq.nextval
    into :new.product_id
    from dual;
    end;
    Example Number 2 ...
    How to create an autoincrement field in a table with a sequence ...
    SQLWKS> create table bob(a number , b varchar2(21));
    Statement processed.
    First create a sequence
    SQLWKS> create sequence x ;
    Statement processed.
    Then create the trigger.
    create trigger y before insert on bob
    for each row
    when (new.a is null)
    begin
    select x.nextval into :new.a from dual;
    end;
    Example Number 3 ...
    First create a sequence:
    create sequence emp_no_seq;By default it increments by 1 starting at 0.
    Use its values when inserting data into the table:
    insert into t_emp values (emp_no_seq.nexval, 'Joe Black');~ Madrid.

  • How to create spool when running a report in background

    hi folks,
    i am running a bdc report in bkgrnd... and i want all the success and eror messages to be displayed in a spool... plz explain with example as to how this can be achieved .

    Hello Nishant,
    You could use this sample code to create the spool.
    DATA : X_NAME       LIKE TSP03D-NAME,
           X_DEST       LIKE TSP03D-PADEST VALUE 'LOCL',
           X_ROWS       LIKE SXPCKLSTI1-BODY_NUM VALUE 0,
           X_STARTROW   LIKE SXPCKLSTI1-BODY_START VALUE 1,
           X_PAGES      LIKE RSPOTYPE-PAGES VALUE 1,
           X_PAGES_1    TYPE P DECIMALS 2,
           X_RQTITLE    LIKE SXPCKLSTI1-OBJ_DESCR,
           X_RQCOPIES   TYPE I VALUE 1,
           X_RQOWNER    LIKE TRDYSE01CM-USERNAME,
           X_IMMEDIATE  LIKE PRI_PARAMS-PRIMM VALUE ' ',
           X_RQID       LIKE TSP01-RQIDENT,
           I_CONTENTS    LIKE  SOLISTI1 OCCURS 0 WITH HEADER LINE.
    X_PAGES   = 1.
    X_RQOWNER = SY-UNAME.
    X_DEST     = 'LOCL'.
    X_STARTROW = 1.
    X_RQCOPIES = 1.
    X_IMMEDIATE = ' ' .
    I_CONTENTS-LINE = 'I Got your Childhood PHOTO'.
    APPEND I_CONTENTS.
    CALL FUNCTION 'RSPO_SX_OUTPUT_TEXTDATA'
    EXPORTING
          name                 =   x_name
        DEST                 =  X_DEST
        ROWS                 =  X_ROWS
        STARTROW             =  X_STARTROW
        PAGES                =  X_PAGES
        RQTITLE              =  X_RQTITLE
        RQCOPIES             =  X_RQCOPIES
        RQOWNER              =  X_RQOWNER
        IMMEDIATELY          =  X_IMMEDIATE
        IMPORTING
        RQID                 =  X_RQID
        TABLES
    <b>    TEXT_DATA            = I_CONTENTS</b> <b>Your Message Table</b>
        EXCEPTIONS
        NAME_MISSING         = 1
        NAME_TWICE           = 2
        NOT_FOUND            = 3
        ILLEGAL_LAYOUT       = 4
        INTERNAL_ERROR       = 5
        SIZE_MISMATCH        = 6
        OTHERS               = 7.
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.      ENDIF.
    ENDIF.
    WRITE: X_RQID
    If useful reward.
    Vasanth

  • How to create Sequence Diagrams in Jdev 10.1.3?

    Hi there,
    I´m migrating (trying) from Eclipse to JDeveloper and I´m having several dificulties....
    I´m creating a new project in JDeveloper:
    0) create an application workspace [ok]
    1) create a web application project [ok]
    2) create some packages and Java classes [ok]
    3) create a class diagram related to the
    above classes [ok]
    4) create a sequence diagram [ok]
    5) use the classes into the sequence
    diagram [more and less]
    6) use the methods of the classes into
    the message lines of the sequence
    diagram [no]
    Some observations that are messing me:
    1) Do I need to create an "Object Lifetime" for
    every class and after that select "Attach Classifier" ?? Why I can´t just drag the classes into the diagram (like Jude) ?
    2) Can I use the methods already defined in the classes to create messages in the sequence diagram?
    3) The sequence diagram od JDeveloper is disconnected from the project classes? i.e., is it just a draw ?
    best regards,
    Felipe Gaúcho

    Hi,
    I am glad to find that you are working on the sequence diagram. I take your point about selecting the methods and assigning them to the messages. Unfortunately this feature is gated by some architecture changes and it something I will be actively pursuing as soon as it is possible.
    In reply to your other points:
    1) Do I need to create an "Object Lifetime" for
    every class and after that select "Attach Classifier" ?? > Why I can´t just drag the classes into the diagram (like > Jude) ?The reason for this is that the sequence modeler is simply a view onto a single diagram type that can contain any object. This allows the mingling of different UML type on the same diagram. It can be usefull to mix usecase and seqeunce for example.
    For that reason the default action for java class is to model a java class, and you do indeed need to create a lifeline first. Did you know you could drag and drop the classifier, in your case a java class, from the navigator onto the lifeline. This might be quicker in your case.
    2) Can I use the methods already defined in the classes > to create messages in the sequence diagram?As noted before in the preview you can't select existing messages. This is something we would like to add as soon as it is possible.
    3) The sequence diagram od JDeveloper is disconnected
    from the project classes? i.e., is it just a draw ?The sequence modeler isn't totally disconnected from the project classes; but it wont update as you edit the code if this is what you mean.
    Could you perhaps expand on this point a little bit?
    Thanks,
    Gerard Davison
    UML Modelers

  • How to create alert when some record are deleted

    HI to all,
    i am new to alert having req like if salary is deleted in the HRMS form then alert has to fired.
    how to do it.
    pls help.

    out of box it is not possible unless you enable the audit trail and fire the alert from the audit table.
    the other way may be you create a temp table and insert the deleted data using user hook before the data is deleted and then fire the alert whenever new record is inserted to the temp table.

  • How to create sequence 0.1,0.2....0.6 1,1.1,1.2...1.6,2

    hi to all,
    i tried to do above type sequence which similar circket over sequence , i tried to do but increament and start with are allowing only integer value..
    if anny one know the answer please share with us ....
    thnaks in advance ....

    Hello
    I'll take a wild stab and make an assumption that you need this in a query rather than using it for some sort of primary key generation...
    SELECT
          TO_CHAR(first_num)||'.'||TO_CHAR(second_num)
    FROM
        (   SELECT
                  ROWNUM - 1 first_num
            FROM
                dual
            CONNECT BY LEVEL <=10
        (   SELECT
                  ROWNUM second_num
            FROM
                dual
            CONNECT BY LEVEL <=10
    ORDER BY first_num,second_num   
    TO_CHAR(FIRST_NUM)||'.'||TO_CHAR(SECOND_NUM)
    0.1
    0.2
    0.3
    0.4
    0.5
    0.6
    0.7
    0.8
    0.9
    0.10
    1.1
    1.2
    1.3
    1.4
    1.5
    1.6
    1.7
    1.8
    1.9
    1.10
    2.1
    2.2
    select
           rtrim(xmlagg(xmlelement(e, seq || ',')).extract('//text()').extract('//text()') ,',') seq
    from ( SELECT
                  TO_CHAR(first_num)||'.'||TO_CHAR(second_num) seq
            FROM
                (   SELECT
                          ROWNUM - 1 first_num
                    FROM
                        dual
                    CONNECT BY LEVEL <=10
                (   SELECT
                          ROWNUM second_num
                    FROM
                        dual
                    CONNECT BY LEVEL <=10
            ORDER BY first_num,second_num
    SEQ
    0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.10,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,1.10,2.1,2.2,2.3,2.4,2.5,2.6,2.7,2.8,2.9,2.10,3.1,3.2,3.3,3.4,3.5,3.6,3.7
    ,3.8,3.9,3.10,4.1,4.2,4.3,4.4,4.5,4.6,4.7,4.8,4.9,4.10,5.1,5.2,5.3,5.4,5.5,5.6,5.7,5.8,5.9,5.10,6.1,6.2,6.3,6.4,6.5,6.6,6.7,6.8,6.9,6.10,7.1,7.2,7.3,7
    .4,7.5,7.6,7.7,7.8,7.9,7.10,8.1,8.2,8.3,8.4,8.5,8.6,8.7,8.8,8.9,8.10,9.1,9.2,9.3,9.4,9.5,9.6,9.7,9.8,9.9,9.10but without more info, it is just a wild stab.
    HTH
    David

  • How to create sequence based (SERIAL) primary key?

    Does anybody know any workaround to modify dictionary table outside Web Dynpro?
    I specify default column value in SQL Studio:
    ALTER TABLE PB_ORGANIZATION COLUMN ORG_ID ADD DEFAULT SERIAL(1)
    Everything is OK, but when I try to insert values in modified table from Web Dinpro EJB application I get the error:
    com.sap.engine.services.ejb.exceptions.
    BaseReliableNoSuchEntityException: The entity does not
    exist in the database. Its value in the cache has to be
    refreshed.
    It seems WebAS keeps some metadata, generated by Web Dynpro, and manual changes in a database table put it out of sync with this metadata.
    I need serial primary keys in my project. Is it possible to solve the above problem?

    I'll better send You entire application. It has Web Service frontend (PhoneBook) which can be used for testing. Method createOrganization() throws the exception.
    No, this error appears in EJB application only.
    BTW, I have even made attempt to replace <default-value>0</default-value> by <default-value>SERIAL(1)</default-value> but it didn't help.

  • How to create interface when target is a webservice

    Hi
    I have following requirement
    I want to sent data from a table to a web service.I have source as oracle database table and in the target end
    i have web service.Do i have to reverse engineer the webservice in this case.If so how i have make web service as target in the interface.Can some one explain the process how i need to start ?.
    Thanks in advance.
    BS

    In order to use the web service, you should get the WSDL of the webservice, so that you can reverse engineer the XML. In ODI you should populate the XML in one or more interfaces, then use the ODIInvoke WebService request to call the actual service with your data. These steps can be put together into a package for execution as a single step.

  • How to create a sequence for an particular item in my apex form

    Hi friends,
    I created an database application, of a form with a report, and it is working fine...
    But in my form, i have a requirement....The below are the existing fields in my form
    issue no
    created by
    start date
    status
    priority
    due date
    Among these fields in my form i need to create a 'Sequence' for my field "issue no",
    So that whenever i opened the form the 'issue number' must generate automatically like 1 for the first time, 2 for the second time and so on..
    For that i created a sequence
    CREATE SEQUENCE "ORDERS_SEQ"
    MINVALUE 1
    MAXVALUE 999999999999999999999999999
    INCREMENT BY 1
    START WITH 1000
    NOCACHE
    NOCYCLE;
    But for validation where i need to write the sequence query for the particular item 'issue no'....i dont have any idea of where to write the validation query for the sequence..
    please tell where i need to write in step wise manner..please help me friends...
    As the below is my validated sequence query for item 'issue no'
    'select seq.issue_id.nextval into issue_no'
    This is my above validation query whether the query that i mentioned is right..if not let me know the validation query..
    And also i need where to apply this validation query in steps..
    Thanks in advance
    Regards,
    Harry...

    Harry,
    Rik is on the right track. Here is a sample insert trigger: Would need to substitute you sequence ORDERS_SEQ with my sequence las_log_seq, how you define or use timestamps is up to you.
    DROP TRIGGER LASDEV.BINS1_LAS_LOG_TBL;
    CREATE OR REPLACE TRIGGER LASDEV."BINS1_LAS_LOG_TBL"
       BEFORE INSERT
       ON las_log_tbl
       FOR EACH ROW
    BEGIN
       -- Description: Insert log_seq, creation_dt, creation_id,
       --              lst_updt_dt and lst_updt_id.
       -- Maintenance:
       -- Date        Actor          Action
       -- ====        =====          ======
       -- 07-Sep-2010 J. Wells       Create.
       :new.creation_id := nvl( v( 'app_user' ), user );
       :new.creation_dt := SYSDATE;
       :new.lst_updt_dt := :new.creation_dt;
       :new.lst_updt_id := :new.creation_id;
        SELECT las_log_seq.NEXTVAL
          INTO :new.las_log_seq
          FROM DUAL;
    END bins1_las_log_tbl;
    /Heff

  • Create sequence help

    Hi all
    please
    How to create sequence with alpha
    like example
    aaa
    aab
    aac
    aad
    aaz
    aba
    abb
    abc
    abd
    abz
    aca
    acb
    acc
    acd
    acz
    ada
    adb
    adc
    add
    adz
    xyz
    xza
    ...

    DROP SEQUENCE S
    CREATE SEQUENCE S START WITH 0 MINVALUE 0
    DECLARE
        v_decimal NUMBER;
        v_base26  VARCHAR2(10);
    BEGIN
        FOR rec in (select s.nextval v_i from dual connect by level <= 52) LOOP
          v_decimal := rec.v_i;
          v_base26  := NULL;
          LOOP
            v_base26 := CHR(ASCII('A') + MOD(v_decimal,26)) || v_base26;
            EXIT WHEN v_decimal < 26;
            v_decimal := TRUNC(v_decimal / 26) - 1;
          END LOOP;
          DBMS_OUTPUT.PUT_LINE(TO_CHAR(rec.v_i,'999.') || ' ' || v_base26);
        END LOOP;
    END;
    0. A
    1. B
    2. C
    3. D
    4. E
    5. F
    6. G
    7. H
    8. I
    9. J
    10. K
    11. L
    12. M
    13. N
    14. O
    15. P
    16. Q
    17. R
    18. S
    19. T
    20. U
    21. V
    22. W
    23. X
    24. Y
    25. Z
    26. AA
    27. AB
    28. AC
    29. AD
    30. AE
    31. AF
    32. AG
    33. AH
    34. AI
    35. AJ
    36. AK
    37. AL
    38. AM
    39. AN
    40. AO
    41. AP
    42. AQ
    43. AR
    44. AS
    45. AT
    46. AU
    47. AV
    48. AW
    49. AX
    50. AY
    51. AZ
    PL/SQL procedure successfully completed.
    SQL> SY.

  • Assigning a 'dynamically created sequence' value to a variable

    in my procedure i am creating a sequence on the fly, i am preparing the name with some passed parameters like below
    v_seq_name := 'seq_'||loadid||v_table_name;
    execute immediate 'CREATE SEQUENCE '||v_seq_name||' MINVALUE 1 MAXVALUE 999999999999999999999999999 START WITH 1 increment by 1 cache 20';
    and now after doing some operations i need to assign the current value of sequence to a number variable i tried following but not working
    1) v_curr_value : = v_seq_name.currval ;
    2) select v_seq_name||'.nextval' into v_curr_value from dual;
    can you please suggest me how i can get the value in plsql block.

    DIVI wrote:
    in my procedure i am creating a sequence on the fly, i am preparing the name with some passed parameters like below
    v_seq_name := 'seq_'||loadid||v_table_name;
    execute immediate 'CREATE SEQUENCE '||v_seq_name||' MINVALUE 1 MAXVALUE 999999999999999999999999999 START WITH 1 increment by 1 cache 20';
    and now after doing some operations i need to assign the current value of sequence to a number variable i tried following but not working
    1) v_curr_value : = v_seq_name.currval ;
    2) select v_seq_name||'.nextval' into v_curr_value from dual;
    can you please suggest me how i can get the value in plsql block.Well, you haven't given the error you are getting but I guess the procedure isn't compiling? You need to execute immediate any reference to the sequence.
    Having said that, your architecture is probably wrong if you are dynamically creating things in a procedure.
    Why do you need to create them dynamically?

  • Create sequence, function and view all at once -script or something similar

    Hi I would like to know in what way can I write a script or something like that which would define names for a sequence, function and a view in the beginning (for example TEST_SEQ, TEST_FJ, TEST_VIEW...) and after that create this sequence, function and view with definitions like
    CREATE SEQUENCE  TEST_SEQ
    MINVALUE 1 MAXVALUE 999999999999999999999999999
    INCREMENT BY 1 START WITH 1 NOCACHE  NOORDER  NOCYCLE;
    create or replace FUNCTION TEST_FJ RETURN NUMBER AS
    tmp number;
    BEGIN
    select TEST_SEQ.NEXTVAL  into tmp from dual
    RETURN tmp;
    END TEST_FJ;
    and so on...
    In the end I would also like to grant some rights on these objects I just created:
    grant select on TEST_SEQ to public;
    grant execute on TEST_FJ to public;
    So my question is how to package all these things together so I can execute them from a single file in SQL Developer, and if i need to change the names of these tables I want do it in one place in the beginning of this script (or something like a script, I'm not sure what)...
    Thanks in advance!

    hi,
    hope help you...
    this is my basic generic solution...
    create or replace procedure createSequence( psequenceName in varchar2 ) is
    begin
    execute immediate 'create sequence ' || psequenceName ;
    execute immediate 'grant select on ' || psequenceName || ' to public ';
    end ;
    create or replace function getNextVal( psequenceName in varchar2 ) return number is
    queryText varchar2(100) := 'select <sequence_name>.nextval into :next_value from DUAL' ;
    next_value number ;
    begin
    queryText := replace(queryText,'<sequence_name>',psequenceName);
    execute immediate queryText into next_value ;
    return( next_value ) ;
    end ;
    Edited by: edogt on Nov 27, 2008 5:33 AM
    Edited by: edogt on Nov 27, 2008 5:35 AM
    Edited by: edogt on Nov 27, 2008 5:35 AM

  • How to call sequence in Oracle Forms

    Hi,
    I am using Oracle Developer Suite 10g. I want to call a sequence that I have created in SQL Plus. When I try to call the sequence in a trigger for a command button (WHEN-BUTTON-PRESSED) the compiler tells me that the sequence cannot be used in this context. I am passing the sequence to a procedure as a parameter. Let me show you some code:
    My table is:
    Location(loc_id, loc_name);
    primary key is loc_id
    My sequence is:
    CREATE SEQUENCE loc_seq
    MINVALUE 1
    MAXVALUE 999999
    START WITH 1
    INCREMENT BY 1
    CACHE 20;
    My procedure is:
    CREATE OR REPLACE PROCEDURE loc_change (v_loc_id IN Location.loc_id%TYPE, v_loc_name IN Location.loc_name%TYPE) AS
    BEGIN
    INSERT INTO Location(Location.loc_id, Location.loc_name)
    VALUES (v_loc_id, v_loc_name);
    COMMIT;
    END loc_change;
    My WHEN-BUTTON-PRESSED trigger is
    BEGIN
    loc_change(loc_seq.nextval, :DATA_BLOCK.VARIABLE);
    END;
    All that I need to do is generate a random number for the parameter passed that is the primary key for Location, from within my trigger. Any thoughts?

    This
    DECLARE
      nSeq number;
    BEGIN
      select loc_seq.nextval
      into nSeq
      from dual;
      loc_change(nSeq, :DATA_BLOCK.VARIABLE);
    END;should do the job...
    you can also put the fetching of the sequence into the database procedure. if you have DB Version 11g, you can reference the sequence simply by this:
    nSeq := loc_seq.nextval;in prior 11g you have to fetch it using select into from dual.
    regards

Maybe you are looking for

  • HT3819 How to use homesharing between different user accounts on the same iMac?

    I find plenty of articles about homesharing on iTunes between computers, but not between two different user accounts on the same iMac.  Any advice or direction to an existing artical is greatly appreciated.  I'm knew to the Mac and still have Windows

  • Goods Receipt without Serial number?

    Hello Experts, Is there a way to do goods receipt on a material without entering the serial number even if the material is maintained with a serial number profile? Is there some type of special stock you can put the material into where you do not nee

  • DESFORMAT=spreadsheet and formatting issues in excel

    Hi all, Using reports builder 10 I have created a report layout for use with spreadsheets but am having trouble with a couple columns. In the data model the columns are just a straight select, they are varchar2 in the table. The columns themselves co

  • Error in File To Fcc Scenario

    Hi All, i done File to Fcc scenario, my scenario is successful, i got black and white flag in moni and in communicaion channel monitoring no error is displayed in trace. but in my output file olny first field of a record is getting displayed. my rece

  • Plug-ins are disappeared

    I use Photoshop cs6/64 bit version. since a short time all plug-ins are disappeared, nik-software as well as pt-lens. What happened? The plug-ins are correctly installed in the plugin-folder.