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 3425572
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:36:36+00:00 2026-05-18T06:36:36+00:00

In Oracle, is there an easy way to perform this in a single query,

  • 0

In Oracle, is there an easy way to perform this in a single query, or is this the correct approach?

SELECT MAX(ID) INTO n_FOO_ID FROM FOO WHERE BAR = n_InputVar and ConditionalFlag IS NULL;

IF n_FOO_ID IS NULL OR n_FOO_ID = 0 THEN
    SELECT MAX(ID) INTO n_FOO_ID FROM FOO WHERE BAR = n_InputVar;
END IF;

What I’m trying to do is get the maximum ID where my condition matches an input variable, but prioritizing rows that don’t have a conditional flag.

Most of my database experience is in SQL Server, where I would do something like this:

SELECT TOP 1 ID INTO @FooID FROM FOO WHERE Bar = @InputVar ORDER BY ConditionalFlag, ID DESC

But this doesn’t seem to work the same on Oracle, or the more likely…I’m not doing it correctly. Can anyone provide any advice?

Thanks!

  • 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-18T06:36:37+00:00Added an answer on May 18, 2026 at 6:36 am

    I believe the Oracle equivalent of:

    SELECT TOP 1 ID 
    INTO @FooID 
    FROM FOO 
    WHERE Bar = @InputVar ORDER BY ConditionalFlag, ID DESC
    

    is

    SELECT ID FROM
    ( SELECT ID 
      INTO FooID 
      FROM FOO 
      WHERE Bar = InputVar ORDER BY ConditionalFlag, ID DESC
    ) WHERE ROWNUM = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any easy way to query a stored procedure (Oracle - PL/SQL) for
Is there an easy way to determine in a SQL Script if the ORACLE
Is there a way in Oracle to select the date on which daylight savings
Is there a difference in performance (in oracle) between Select * from Table1 T1
Is there a way to restore an Oracle 11g instance from raw data files
Is there a particularly easy way to convert a number like 21.08 into Twenty
Is there a way to extract timezone information directly from an oracle.sql.TIMESTAMPTZ object (selected
I am new to the SQL/Oracle, and wondering if there's an easy way to
Is there any easy way to retrieve table creation DDL from Microsoft Access (2007)
Is there an easy way to work with JSON within oracle? I have a

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.