Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

another - how can i do this sql in oracle - question 1

Status
Not open for further replies.

FALCONSEYE

Programmer
Jul 30, 2004
1,158
US
t-sql:

Code:
if exists( 
select quantity, id
from myTable
where id = 55
)

begin
  print 'yes'
end
else 
begin
   .. run another select statement to look another table
end

I have to the same thing in Oracle. Currently I have:

Code:
declare 
tempquantity number;
tempid number;

begin
select quantity, id
into tempquantity, tempid 
from myTable
where id = 55

if tempquantity > 0
then 
  /* found */
else
  .. run another select statement to look another table
end if;
end;

the problem is the first select statement sometimes returns 'no rows selected' and tempquantity > 0 fails.

I tried tempquantity number := -1; but had no luck either.
any help?

 
Falcon,

The problem with doing a "SELECT...INTO..." in PL/SQL is that it requires that precisely ONE row returns from the query...not zero...not two or more. Therefore, you can use this code to ensure that one row returns:
Code:
declare 
    tempquantity number;
    tempid number;
    rowcnt number;
begin
    select count(*) into rowcnt from myTable
     where id = 55;
    if rowcnt <> 1 then
        ...<do what you want for this condition>
    else
        select quantity, id
          into tempquantity, tempid 
          from myTable
         where id = 55
        if tempquantity > 0 then 
            /* found */
        else
            .. run another select statement to look another table
        end if;
    end if;
end;
Let us know if this resolves your need.

[santa]Mufasa
(aka Dave of Sandy, Utah, USA)
[I provide low-cost, remote Database Administration services: www.dasages.com]
“Beware of those that seek to protect you from harm or risk. The cost will be your freedoms and your liberty.”
 
Thanks, I just did something very similar.


defined a tmpCount number;

and used

Code:
select count(1) into tmpCount 
from myTable
where id = 55

if tmpCount > 0
then
  -- it's found over in myTable
  DBMS_OUTPUT.put_line ('found it in myTable');
else
  -- run other select
  DBMS_OUTPUT.put_line ('look at other table');
end if;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top