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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:03:14+00:00 2026-06-08T13:03:14+00:00

Disregard if using MERGE INTO is better in this case or not. I just

  • 0

Disregard if using MERGE INTO is better in this case or not. I just wonder if I can check if the row exists or not. If not, then set the return code to 1 and return immediately. If yes, then continue to execute the rest of code and set return code to 0 in the end. Below code is not working as it always executes to the end. How should I fix it?

  BEGIN

    -- check 
    SELECT CASE
      WHEN NOT EXISTS (SELECT 1 FROM s WHERE s.col1 = 1 AND s.col2 = 2)
      THEN 1
    END
    INTO ret FROM dual;

    -- update
    UPDATE s
    SET s.col3 = 3
    WHERE s.col1 = 1 AND s.col2 = 2;

    COMMIT;

    SELECT 0 INTO ret FROM dual;
    RETURN ret;

  END foo;

What if I want be able to distinguish if it’s s.col1 = 1 not exist or s.col2 = 2 not exist. And have 2 different return codes for them. How should I do in this case?

  • 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-08T13:03:15+00:00Added an answer on June 8, 2026 at 1:03 pm

    You are not doing anything with the value stored ret.

    There is no IF around the UPDATE statement that checks if ret is 1 or null (the other alternative). And because there is no IF the rest of the procedure is always executed.

    Something like this is needed:

    SELECT sum(case when col1 = 1 then 1 else 0 end) as col1_count, 
           sum(case when col2 = 2 then 1 else 0 end) as col2_count
        into ret1, ret2
    FROM s 
    WHERE s.col1 = 1
       or s.col2 = 2;
    
    if ret1 > 0 and ret2 > 0 then
       update ...;
       commit;
       ret := 0;
    elsif  (ret1 > 0 and ret2 = 0) then 
       ret := 1;
    elsif (ret1 = 0 and ret2 > 0) then 
       ret := 2;
    end if;
    
    return ret;
    

    A much more efficient approach is to simply do the update and check if any rows where modified. Running the select before doing the update simply doubles the work if the row exists. Running only the update when the row does not exist is the same work as doing the select.

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

Sidebar

Related Questions

This question is about code explanation, not code debugging. The code I'm using works.
Disregard this question: I've simply confused <script src=...></script> tag and <script> [some functions] </scipt>
Models (disregard typos / minor syntax issues. It's just pseudo-code): class SecretModel(models.Model): some_unique_field =
How to detect whether or not a particular font is installed using javascript only.
So I've been using CURL to get some portions of remote images....but then depending
I am reading from a text file in C# using a SQL statement. This
This question is derived from the topic: vector reserve c++ I am using a
I am using this workflow: http://reinh.com/blog/2009/03/02/a-git-workflow-for-agile-teams.html where he mentions: First, and while in your
I'm calling a service from PHP using cURL, like this: $response = curl_exec($ch); and
OK, please disregard what has gone before. I'm not getting the errors anymore, so

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.