Link to home
Get AccessLog in
Avatar of Torrwin
TorrwinFlag for United States of America

asked on

Select Case on Subset?

Hello,

I am very experienced on SQL Server, but am very new to Oracle.

I had a package with a function that returned one row.  However, there were rare occasions when it would need to return more than one row, so I have updated the function to return multiple rows as needed.

However, this has now caused the query that used my function to fail, because it was matching on the one returned row and now there are sometimes multiple returned rows.  So, my question is, is it possible to update the following case statement to look through a subset instead?  If so, what would the syntax be?

Something like: CASE WHEN Field4 IN package.function(Field3) THEN...

Thanks,
-Torrwin
SELECT Field1, Field2, (CASE WHEN package.function(Field3) = Field4 THEN 'True' ELSE 'False' END)
FROM Table1

Open in new window

Avatar of Sean Stuber
Sean Stuber

How does your function return multiple values?


 A SQL collection type?  A PL/SQL collection type? XMLTYPE? a concatenated string? etc?
SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
if pl/sql collection type, then you can't use your function in SQL

if XML, you will need to use an XPATH query to search for field4 in the resulting XML nodes

if concatenated string, you could try using INSTR, but you have the potential of false positives unless you're very careful.
Avatar of Torrwin

ASKER

Actually, now that you mention it, that's got to be a problem too.  I need to update the return type as well, it was originally just NUMBER.

Here's the basic function:
FUNCTION Function1(parameter1 NUMBER)
 RETURN NUMBER IS

 variable1 NUMBER;

 BEGIN
  SELECT Field1
  INTO variable1
  FROM ...
  WHERE Field2 = paramater1
   AND ROWNUM >= 1;
 END;

 RETURN (variable1);
END;

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
if your collection will be small, you can also do it as a non-pipelined function.
doing that can be marginally faster, but will consume more resources, particularly memory
SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
as a side note...  

AND ROWNUM >= 1  

isn't necessary, or helpful
Avatar of Torrwin

ASKER

Thanks, i'm testing it now, it looks pretty solid.

The ROWNUM >= 1 exists because I have several of this query chained together with a EXCEPTION WHEN NO DATA FOUND clause.
how does that help?

ROWNUM is a pseudo-column generated by the existence of data.

so ROWNUM >= 1 only does anything when there is at least one row

in other words,  your filter is saying,  "only return rows when there are rows"

which is legal, but doesn't offer any functionality
Avatar of Torrwin

ASKER

> so ROWNUM >= 1 only does anything when there is at least one row

Correct, which is what i'm looking to do.  I didn't explain it fully because the details aren't really important, and obviously the query is scaled down to where I can get the point across but still give enough information to get an informed answer.  

Basically, we have a situation where we need to return the "most correct" record from a hierarchy.  So, the business logic looks something like "If Type A exists, then use this record.  If type A doesn't exist, then check for type B.  If type B exists ...etc..."
>>>> so ROWNUM >= 1 only does anything when there is at least one row


You're missing the point if you're counting on that criteria to "DO" anything.

"return rows if there are rows" you could just as easily say where rownum != 0
or where rownum is not null

or best of all:  Don't use that condition at all.



rownum only exists if there is a row,
So, if you have a row, it will be at least 1

if you don't have a row, then you won't have a rownum to compare to 1

You should remove the condition,  

Right now, that query is sort of the SQL version of the old joke

"Raise your hand if you're not here"






your if A then A, else B logic will NOT be enforced by a


 ROWNUM >= 1 condition

If you think you have an example that demonstrates it,  please open a new question for discussion