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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:20:13+00:00 2026-06-10T02:20:13+00:00

SELECT Value1 INTO lValue FROM Table1 WHERE Field1 = lTempValue; This works fine when

  • 0
SELECT Value1 INTO lValue
FROM   Table1
WHERE  Field1 = lTempValue;

This works fine when the match is true. But if the match isn’t true, I receive an error.

ORA-01403: no data found

Ideally, that’s fine with me because I’m going to check that value next to see if it’s above 0 and if it is, use that value in an insert query. I don’t want to check for the value and then have to run the same query to retrieve it essentially, I want to do it in one query if possible, but I can’t figure out how that is done.

If there’s a value, then I want that value to go into lValue. If there is no value, then I want 0 to go into lValue. Anyone got any ideas? I’ve only done a quick google check, but it came up dry. Figured I’d post this while looking. Thanks for the help.

  • 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-10T02:20:15+00:00Added an answer on June 10, 2026 at 2:20 am

    Normally, you’d simply catch the exception

    BEGIN
      SELECT value1
        INTO lValue
        FROM table1
       WHERE field1 = lTempValue;
    EXCEPTION
      WHEN no_data_found
      THEN
        lvalue := 0;
    END;
    

    You can write less code by using NVL and an aggregate function (either MIN or MAX) but that tends to be a bit less obvious (note, for example, that those answers had to get revised a couple of times). And it requires whoever comes after you to pause for a moment to understand what you are doing (and whether you are doing it correctly or not). A simple nested PL/SQL block is pretty common and pretty self-explanatory.

    More than that, however, it doesn’t hide bugs due to duplicate rows. If you happen to get two rows in table1 where field1 is lTempValue, catching just the no_data_found exception allows the unexpected too_many_rows exception to propagate up to the caller. Since you don’t expect to have multiple rows, that is exactly the behavior that you want. Using aggregate functions hides the fact that the underlying data has problems causing you to return potentially incorrect results and making it impossible to detect that there is a problem. I would always rather get an error as soon as something is causing duplicate rows to appear– allowing me to fix the problem before it gets out of hand– rather than finding out years later that we’ve got millions of duplicate rows, that the code has been occasionally returning incorrect results, and that we have a huge data cleansing effort after addressing the root cause.

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

Sidebar

Related Questions

insert ignore into table1 select 'value1',value2 from table2 where table2.type = 'ok' When I
INSERT INTO options (owner, name, value, modified) SELECT owner, name, value, modified, @draft:=draft FROM
Question: Given a SQL string like CREATE VIEW TestView AS SELECT value1, value2 FROM
I am used to doing this in sql server IF (SELECT COUNT(*) FROM table
In the SQL Server, how to select multiple values into a temp table ?
I'm trying to dynamically load values into a select in a jqGrid. It almost
I am using setColProp to dynamically load values into a select edittype. I have
When I run this code $sql_select = INSERT INTO `database`.`table`(Columns) VALUES (Values)... ; $mysqlid
I have a query insert into carsdescription (description,idCar) value (@description,@idCar) where idCar=@idCar; select nameOfCar
INSERT INTO files (fileUID, filename) WITH fileUIDS(fileUID) AS ( VALUES(1) UNION ALL SELECT fileUID+1

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.