Results 1 to 12 of 12

Thread: Next available number...

  1. #1
    Programmer/Analyst I bryce4president's Avatar
    Join Date
    Apr 2007
    Location
    Lewisburg, PA
    Age
    29
    Posts
    2,890
    Rep Power
    4547

    Next available number...

    Hey Guy,
    I know this has been discussed before, probably about 1000 times, but I can't seem to find anything. As many of you have done before, I need to store a value that is the Next Number to be used for some purpose. Common uses would be customer number, order number, etc. What are some of the ways to do this, which would you think is easiest? This is really just a down and dirty type of thing that will only be needed for about the next year... I need to generate a Catalog Number... basically a 6 digit number, probably seeded above 100000. Is there some user space or something? Or use a file and increment with SQL? What do you think?
    Your future President
    Bryce

    ---------------------------------------------
    http://www.bravobryce.com

  2. #2
    Harbinger of Doom tomholden's Avatar
    Join Date
    Dec 2007
    Location
    Lebanon, Tennessee, United States
    Posts
    4,175
    Rep Power
    8372

    Re: Next available number...

    personally for a Q&D short term process I'd simply use a data area for this.
    I'm not anti-social, I just don't like people -Tommy Holden

  3. #3
    Analyst DeadManWalks's Avatar
    Join Date
    Mar 2006
    Location
    Atlanta GA
    Posts
    1,346
    Rep Power
    3496

    Re: Next available number...

    id go with max(CatalogNumber), then update it with Max + 1. The data area is good, if only one user is going to be using it at a time, or it could blow up. A simple (R) will fix the blow up, but I would not want the calls.
    /Free your self

  4. #4
    Experienced Forum Member Mark A Smith's Avatar
    Join Date
    Jun 2011
    Location
    McDonough
    Posts
    209
    Rep Power
    902

    Re: Next available number...

    An interface control file with a serial number field works as well.

    Choose your flavor.

  5. #5
    Analyst
    Join Date
    Sep 2005
    Location
    Germany
    Posts
    1,433
    Rep Power
    8031

    Re: Next available number...

    Why not using an SQL SEQEUENCE Object, which is under the cover nothing else than a data area, but you do not have to care about locking and managing the next number:

    Code:
    CREATE SEQUENCE MySchema/MySeqObj 
    	AS Integer Start With 1 Increment by 1;
    To get the next value you can use it for example in an insert statement as follows:
    Code:
    Insert into MySchema/MyTable (Col1, Col2, .... ColN)
    Values (Next Value For MySeqObj, col2Val, ... ColNVal);
    You also can retrieve the next value with embedded SQL:
    Code:
    /Free
       Exec SQL   Set :NextValue = Next Value For MySeqObj;
       ...
    Because a sequence is a independent object, it can be used where ever needed, for example for multiple columns within the same table, for a single or multiple columns in multiple tables, or whenever you simply need a unique value.

    Birgitta
    Last edited by B.Hauser; August 4th, 2012 at 02:53 AM.

  6. #6
    Experienced Forum Member dhanuxp's Avatar
    Join Date
    Mar 2008
    Location
    Sri Lanka
    Posts
    554
    Rep Power
    2131

    Re: Next available number...

    Why not create table with auto-increment field ?
    Code:
    Create Table  qtemp/mytable                            
    (MyId Integer Generated Always as Identity             
            (Start with 1 Increment By 1), XX CHAR (10 ) )
    PS:
    I found difficulties in calling insert statement with sequence object using Java.
    (When we have to mention two libraries in single statement)
    but if you use hibernate, this also works fine!!
    Code:
    Insert into MySchema.MyTable (Col1, Col2, .... ColN)
    Values (Next Value For QGPL.MySeqObj, col2Val, ... ColNVal);
    dhanuxp
    Last edited by dhanuxp; August 5th, 2012 at 10:42 PM.

  7. #7
    Analyst
    Join Date
    Sep 2005
    Location
    Germany
    Posts
    1,433
    Rep Power
    8031

    Re: Next available number...

    PS:
    I found difficulties in calling insert statement with sequence object using Java.
    (When we have to mention two libraries in single statement)
    but if you use hibernate, this also works fine!!

    dhanuxp
    I assume it is not because you are using JAVA but because you are using SQL Naming instead of System Naming. The difference between the naming conventions is much more than simply separating the library and (database) object by either a slash or a period.
    When using SQL naming all unqualified specified tables or views are searched within a single Schema/Library (=Default or Current Schema).
    The current schema can be set by executing the SQL command SET CURRENT SCHEMA.

    If you have to access tables or views in multiple schemas with SQL naming, you either have to qualify them or create aliases in your main data library (Default Schema) that point to the database objects in the other schemas.

    All other database objects such as sequences or stored procedures are searched in the SQL path. Contrary to the current schema, in the SQL path multiple schemas/libraries can be listed and are searched like the library list.
    The SQL PATH can be set by executing the SQL Command SET PATH. The special value *LIBL can be specified with the SET PATH statement.

    Birgitta

  8. #8
    Experienced Forum Member dhanuxp's Avatar
    Join Date
    Mar 2008
    Location
    Sri Lanka
    Posts
    554
    Rep Power
    2131

    Re: Next available number...

    Quote Originally Posted by B.Hauser View Post
    I assume it is not because you are using JAVA but because you are using SQL Naming instead of System Naming.

    Birgitta
    That is it Birgitta.. changed the naming into "system" & works great!
    everyday I learn new things from you... Thank you so much!
    Last edited by dhanuxp; August 6th, 2012 at 05:25 AM.

  9. #9
    Programmer/Analyst I bryce4president's Avatar
    Join Date
    Apr 2007
    Location
    Lewisburg, PA
    Age
    29
    Posts
    2,890
    Rep Power
    4547

    Re: Next available number...

    I really like the sequence object idea. I didn't even know about that. I think that's the way to go. Quick, easy, down and dirty... and oh so effective.
    Your future President
    Bryce

    ---------------------------------------------
    http://www.bravobryce.com

  10. #10
    Noffascropulan gcraill's Avatar
    Join Date
    Mar 2006
    Location
    Riyadh
    Age
    45
    Posts
    1,461
    Rep Power
    3261

    Re: Next available number...

    Quote Originally Posted by B.Hauser View Post
    Why not using an SQL SEQEUENCE Object, which is under the cover nothing else than a data area, but you do not have to care about locking and managing the next number:

    Birgitta
    *Like*
    Greg Craill: "Life's hard - Get a helmet !!"

  11. #11
    Programmer/Analyst I bryce4president's Avatar
    Join Date
    Apr 2007
    Location
    Lewisburg, PA
    Age
    29
    Posts
    2,890
    Rep Power
    4547

    Re: Next available number...

    Ok, I'm about to set up this SQL Sequence Object. How would you document that its been created? Its kind of one of those things that you just plop the SQL statement into STRSQL and bam, its created... Do you guys do something special to document these kinds of things in your systems??
    Your future President
    Bryce

    ---------------------------------------------
    http://www.bravobryce.com

  12. #12
    Analyst
    Join Date
    Sep 2005
    Location
    Germany
    Posts
    1,433
    Rep Power
    8031

    Re: Next available number...

    I normally use either System iNavigator - Database, position on the database object, right click and execute generate SQL to store the SQL statement either as source physical file member or SQL script/IFS file. A source physical file member can be executed with the CL command RUNSQLSTM and an SQL Script can be executed with System iNavigator.

    Currently I have to store all my SQL scripts as source physical file member (in File QSQLDDL - All Tables, Views, Indexes, Sequences and QSQLDML - All stored procedures , User defined (Table) functions.

    Birgitta

Facebook Comments


Similar Threads

  1. Number 400...
    By tock085 in forum Freshers
    Replies: 27
    Last Post: November 1st, 2011, 07:07 AM
  2. Replies: 1
    Last Post: December 15th, 2009, 10:03 AM
  3. how to convert decimal number to hex number
    By OliveraM in forum RPG/RPGLE
    Replies: 2
    Last Post: July 15th, 2008, 08:42 AM
  4. Reciprocal of a Number
    By dcutaia in forum /Free format
    Replies: 9
    Last Post: May 3rd, 2007, 10:31 AM
  5. string or number
    By rachely_p in forum RPG/RPGLE
    Replies: 1
    Last Post: May 5th, 2005, 06:45 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •