Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8092445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:17:06+00:00 2026-06-05T20:17:06+00:00

I would like to mimic the auto-incrementing primary keys feature that many databases have

  • 0

I would like to mimic the auto-incrementing primary keys feature that many databases have in OpenEdge (i.e. to not have to specify the primary key value when doing an INSERT) using the JDBC adapter. So far, I’ve come very close to what I need, except for the part of being able to access the primary key value that the database generated on return from the INSERT (ahem, so maybe not that close ;)).

My current solution uses a combination of table PK default value, trigger and sequence in order to pull it off:

CREATE TABLE users (
  id   BIGINT PRIMARY KEY DEFAULT -1,
  name VARCHAR(200)
);

CREATE SEQUENCE users_seq
START WITH 0,
INCREMENT BY 1,
NOCYCLE;

CREATE TRIGGER users_trigger
BEFORE INSERT ON users
REFERENCING NEWROW
FOR EACH ROW
IMPORT
import java.sql.*;
BEGIN
Long current_id = (Long)NEWROW.getValue(1, BIGINT);
if (current_id == -1) {
  SQLCursor next_id_query = new SQLCursor("SELECT TOP 1 users_seq.NEXTVAL FROM SYSPROGRESS.SYSCALCTABLE");
  next_id_query.open();
  next_id_query.fetch();
  Long next_id = (Long)next_id_query.getValue(1,BIGINT);
  next_id_query.close();
  NEWROW.setValue(1, next_id);
}
END

This allows me to run an insert statement like this:

INSERT INTO users(name) VALUES('Foo Bar')

and the new row automatically gets an ID from the database trigger. That part works fine.

What I really need now is the value of the ID that gets set; either to get the value directly, or a ResultSet that contains the row that just got inserted (that could then be unwrapped to view the ID). I know Oracle and Postgres both support a RETURNING clause for inserts, which is typically how this is handled. However, I don’t see anything like that for OpenEdge.

The only relevant part I could find in the 10.2B SQL Development manual is in section 5-10, where it shows how to access the CURRVAL of a sequence after performing an INSERT that uses NEXTVAL. However, that’s dangerous as I could get someone else’s ID if there are a large amount of INSERTs going on for that table across multiple JDBC sessions (race conditions and whatnot).

The only alternative I can come up with so far is to write a stored procedure specifically for wrapping/performing the INSERT operation, that has an output parameter of the ID that gets generated. However, that isn’t feasible for what I’m working on, which must use a plain SQL INSERT statement, and also seems a bit hackish and brittle (e.g. how to handle the different combinations and permutations of values that could be provided on the INSERT, and what if the schema changes?).

Also, the whole point of this is to not have to reference the primary key in the INSERT, so please don’t tell me to use users_seq.NEXTVAL in my INSERT statement. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:17:07+00:00Added an answer on June 5, 2026 at 8:17 pm

    There are no viable ways to implement this; the user must either specify a value for the PK or specify the name of the sequence so that CURRVAL / NEXTVAL can be used.

    An infeasible workaround is to construct a special stored procedure specifically for performing the INSERT(s), but that is not a general solution as the INSERT command takes a variable number of parameters (not least of which is which columns and data to populate with), while the stored procedure must have a fixed number of parameters.

    If there existed a SQL function that would return a special session ID that corresponded to the client’s transient connection to the SQL engine, it would be possible to have the client and trigger/stored procedure communicate using a pre-determined table as a request queue (by using the session ID as a key). Unfortunately, according to the documentation there is no such session ID.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following legacy code that I would like to mimic, with all
Would like to make anapplication in Java that will not automatically parse parameters used
I would like to mimic the Repository approach that is widely used in DDD
I would like to know that how can we mimic the UI of iOS
I have an application that I would like to write an automated test for.
I have an array of elements that I would like to apply a mapping
I would like to create a c++ type that mimic the build-in type exactly.
I want to draw some listview items disabled and would like to mimic the
I would like to mimic a standard JavaScript confirm() using a jQueryUI dialog. I
I would like to mimic the Run command in Windows in my program. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.