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

  • SEARCH
  • Home
  • 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 4247784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:09:25+00:00 2026-05-21T04:09:25+00:00

I have a handful of raw SQL queries for SQL Server which use SCOPE_IDENTITY

  • 0

I have a handful of raw SQL queries for SQL Server which use SCOPE_IDENTITY to retrieve the generated ID for a specific INSERT immediately after that INSERT occurs all in one execution…

INSERT into Batch(
BatchName,
BatchType,
Source,
Area
) Values (
@strBatchName,
@strType,
@strSource,
@intArea
);

SELECT SCOPE_IDENTITY() BatchID;

The question is:

What’s the best way to do that for an Oracle database?

Can this be done on Oracle through standard SQL or do I have to switch this to use a stored procedure and place something similar in the body of the stored proc?

If it must be a stored proc, then what is the de-facto standard way for retrieving the last generated sequence number, taking care to consider there will likely be overlapping executions on multiple threads so this mechanism will need to retrieve the right generated ID and not necessarily the absolute last generated ID.

If two execute simultaneously then each must return the correct generated ID from each respective call. Notice I’m not using SQL Server’s “@@IDENTITY” because of that multithreaded nature of the calls.

I would rather keep it as raw SQL if possible since that’s much easier for me to manage across platforms (single file containing each platform’s SQL block separated by DBMS identifying tags). Stored procs are a bit more work for me to manage, but I can go that way if it’s the only way possible.

  • 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-05-21T04:09:26+00:00Added an answer on May 21, 2026 at 4:09 am

    Expanding a bit on the answers from @Guru and @Ronnis, you can hide the sequence and make it look more like an auto-increment using a trigger, and have a procedure that does the insert for you and returns the generated ID as an out parameter.

    create table batch(batchid number,
        batchname varchar2(30),
        batchtype char(1),
        source char(1),
        intarea number)
    /
    
    create sequence batch_seq start with 1
    /
    
    create trigger batch_bi
    before insert on batch
    for each row
    begin
        select batch_seq.nextval into :new.batchid from dual;
    end;
    /
    
    create procedure insert_batch(v_batchname batch.batchname%TYPE,
        v_batchtype batch.batchtype%TYPE,
        v_source batch.source%TYPE,
        v_intarea batch.intarea%TYPE,
        v_batchid out batch.batchid%TYPE)
    as
    begin
        insert into batch(batchname, batchtype, source, intarea)
        values(v_batchname, v_batchtype, v_source, v_intarea)
        returning batchid into v_batchid;
    end;
    /
    

    You can then call the procedure instead of doing a plain insert, e.g. from an anoymous block:

    declare
        l_batchid batch.batchid%TYPE;
    begin
        insert_batch(v_batchname => 'Batch 1',
            v_batchtype => 'A',
            v_source => 'Z',
            v_intarea => 1,
            v_batchid => l_batchid);
        dbms_output.put_line('Generated id: ' || l_batchid);
    
        insert_batch(v_batchname => 'Batch 99',
            v_batchtype => 'B',
            v_source => 'Y',
            v_intarea => 9,
            v_batchid => l_batchid);
        dbms_output.put_line('Generated id: ' || l_batchid);
    end;
    /
    
    Generated id: 1
    Generated id: 2
    

    You can make the call without an explicit anonymous block, e.g. from SQL*Plus:

    variable l_batchid number;
    exec insert_batch('Batch 21', 'C', 'X', 7, :l_batchid);
    

    … and use the bind variable :l_batchid to refer to the generated value afterwards:

    print l_batchid;
    insert into some_table values(:l_batch_id, ...);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a login.jsp page which contains a login form. Once logged in the
i have a input tag which is non editable, but some times i need
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
I have several USB mass storage flash drives connected to a Ubuntu Linux computer
I have a snippet to create a 'Like' button for our news site: <iframe
I have found this example on StackOverflow: var people = new List<Person> { new
Let say I have the following desire, to simplify the IConvertible's to allow me
I want to have generalised email templates. Currently I have multiple email templates with

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.