Solved

Conditional Clauses in Where or And Section of a SQL statement in an Oracle Stored Procedure

Posted on 2008-10-22
4
328 Views
Last Modified: 2013-12-07
I am converting from MS SQL Server to Oracle.  I've been tackling the problems one by one (with the help of Experts-exchange).  However this latest one is stumping me.

For example:

create or replace spGetTeams(teamID in number, teamName varchar2, crGetTeamInfo out sys_cursor)
as
Begin
open crGetTeamInfo for
Select team_name, team_id, team_record
From teams
where isActive = 1
And (team_id = teamID or teamID is null or teamID like '-1)
And (team_name = teamName or teamName is null or teamName like '%'||-1||'%')
null;
end spGetTeams;

The person that made the Stored procedure in MS SQL Server explained to me that that the -1 will return all records. So basically it will try to match to the parameter passed, or null records, or all records.  How would I go about doing this in PL/SQL?
0
Comment
Question by:vbemt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 22780887
try this:

CREATE OR REPLACE PROCEDURE spgetteams (
   teamid          IN       NUMBER,
   teamname                 VARCHAR2,
   crgetteaminfo   OUT      sys_cursor
)
AS
BEGIN
   OPEN crgetteaminfo FOR
      SELECT team_name, team_id, team_record
        FROM teams
       WHERE isactive = 1
         AND (team_id = teamid OR teamid IS NULL OR teamid LIKE '%')
         AND (team_name = teamname OR teamname IS NULL OR teamname LIKE '%');
END;

Open in new window

0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 500 total points
ID: 22780901

Small correction:
CREATE OR REPLACE PROCEDURE spgetteams (
   teamid          IN       NUMBER,
   teamname                 VARCHAR2,
   crgetteaminfo   OUT      sys_cursor
)
AS
BEGIN
   OPEN crgetteaminfo FOR
      SELECT team_name, team_id, team_record
        FROM teams
       WHERE isactive = 1
         AND (team_id = teamid OR (teamid IS NULL AND teamid LIKE '%'))
         AND (team_name = teamname OR (teamname IS NULL AND teamname LIKE '%'));
END;

Open in new window

0
 

Author Comment

by:vbemt
ID: 22786168
I think that's what I need, just have a miner question about the 2 examples you gave.  I'm trying to understand the difference between the 2.  I know one uses "AND" and the other uses "OR" but I don't see how it works.
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 22790090

Difference is the first one had a typo: missing parenthesis.

Therefore you have this:

1) if parameter exists -> query ( column = parameter ...)
2) else (OR), if parameter does not exist ->  query (parameter IS NULL AND column like '%')


0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question