473,322 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Proecdure/Function with conditional inputs

I have a task. I have to create a query that has optional elements for a where clause. This code is needed in a few different places so I fuigured a function to create a cursor wouold be best. So I have:
Expand|Select|Wrap|Line Numbers
  1. FUNCTION get_info
  2. (
  3.     p_status  IN  VARCHAR2,
  4.     p_user     IN  VARCHAR2,
  5.     p_type     IN  VARCHAR2
  6. )
  7. RETURN SYS_REFCURSOR;
Now the complexity is the inputs above affect my where clause, but are all optional. So I could have none of them specified (ie null) or any combination of them (ie status = 'update', user='benny'). I may also add other elements in the future. So my question is how do I do this in PL/SQL? In any other real programming language I would create my select call in a string and then conditionally append to the where clause, then execute the statement. But I am not sure PL/SQL has this capability. Any suggestions?
Mar 12 '08 #1
6 1376
amitpatel66
2,367 Expert 2GB
I have a task. I have to create a query that has optional elements for a where clause. This code is needed in a few different places so I fuigured a function to create a cursor wouold be best. So I have:
Expand|Select|Wrap|Line Numbers
  1. FUNCTION get_info
  2. (
  3. p_status IN VARCHAR2,
  4. p_user IN VARCHAR2,
  5. p_type IN VARCHAR2
  6. )
  7. RETURN SYS_REFCURSOR;
Now the complexity is the inputs above affect my where clause, but are all optional. So I could have none of them specified (ie null) or any combination of them (ie status = 'update', user='benny'). I may also add other elements in the future. So my question is how do I do this in PL/SQL? In any other real programming language I would create my select call in a string and then conditionally append to the where clause, then execute the statement. But I am not sure PL/SQL has this capability. Any suggestions?
Are you looking at executing the Dynamic statements?

Something like this:

Expand|Select|Wrap|Line Numbers
  1.  
  2. FUNCTION get_info
  3. (
  4. p_status IN VARCHAR2,
  5. p_user IN VARCHAR2,
  6. p_type IN VARCHAR2
  7. ) IS
  8. EXECUTE IMMEDIATE 'SELECT * FROM table_name WHERE status = '||CHR(39)||p_status||CHR(39)||' AND user = '||CHR(39)||p_user||CHR(39)||' AND type = '||CHR(39)||p_type||CHR(39);
  9.  
  10. RETURN SYS_REFCURSOR;
  11.  
  12.  
Mar 12 '08 #2
I think this is indeed the rigth track. But not sure what the
Expand|Select|Wrap|Line Numbers
  1. '||CHR(39)||p_status||CHR(39)||'
does. Is this some sort of conditional notation?
Mar 12 '08 #3
amitpatel66
2,367 Expert 2GB
I think this is indeed the rigth track. But not sure what the
Expand|Select|Wrap|Line Numbers
  1. '||CHR(39)||p_status||CHR(39)||'
does. Is this some sort of conditional notation?
CHR(39) denotes Single Quotes " ' ". Its just that I am enclosing the values of the input parameters with single quotes.
Mar 12 '08 #4
Ok. It was the '||' that confused me. Also the inputs may not exist. So for example statsu may not be passed in and thus should not be in the where clause. So that compilcates things some, but I guess I would build my base SQL call then conditionally add strings to it to build up things, then call EXECUTE IMMEDIATE on it.
Mar 12 '08 #5
amitpatel66
2,367 Expert 2GB
Ok. It was the '||' that confused me. Also the inputs may not exist. So for example statsu may not be passed in and thus should not be in the where clause. So that compilcates things some, but I guess I would build my base SQL call then conditionally add strings to it to build up things, then call EXECUTE IMMEDIATE on it.
Not a problem. Try this one:

Expand|Select|Wrap|Line Numbers
  1.  
  2. FUNCTION get_info
  3. (
  4. p_status IN VARCHAR2,
  5. p_user IN VARCHAR2,
  6. p_type IN VARCHAR2
  7. ) IS
  8. BEGIN
  9. EXECUTE IMMEDIATE 'SELECT * FROM table_name WHERE status = NVL('||CHR(39)||p_status||CHR(39)||',status) AND user = NVL('||CHR(39)||p_user||CHR(39)||',user) AND type = NVL('||CHR(39)||p_type||CHR(39)||',type)';
  10.  
  11. RETURN SYS_REFCURSOR;
  12. END;
  13.  
Mar 12 '08 #6
Seems to work, only issue remaing is how to deal with the user column. This col can be null. And for some odd reason the code above returns eevrything that is not null, apparently in PL/SQL null != null. So I may need a IF block for this one input, but the other work well.
Mar 12 '08 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Orest Kinasevych | last post by:
Okay, I made sense of the earlier suggestions and realized I was on the right track -- I appreciate the feedback which got me to this point. The suggestions posted here indeed worked and...
2
by: db2sysc | last post by:
I am getting SQL0628N when I run function with INSERT and MODIFIES SQL DATA Version: DB2 v8.1.7.445 Fixpack 7. Create function test1( a int, b int) returns integer language sql
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
2
by: Praveen Ramesh | last post by:
Hi, Is there some kind of support for "Conditional Compilation Directives" in the aspx file? I want to enclose the Register tag as follows: #if DOTNET10 <%@ Register TagPrefix="sfwg"...
3
by: John | last post by:
Hi I have an app to deal with two companies A & B within a group of companies depending on which database is selected. Is it possible for setup to ask the user at install time which company they...
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Andrew Gentile | last post by:
Hello, I am trying to write a function which takes several floats as inputs, then returns 1x6 array of floats. I normally program in Matlab, and I am often confused by C. It seems that this...
1
by: shivendravikramsingh | last post by:
hello friends, actually i have a problem in using conditional statement in ajax function i m tring to describe my problem properly,what i want when i select a value form a combo its vlue is passed...
2
by: cubicalmonkey | last post by:
Hello, I'm attempting to get this code to work in IE. The script I have below works fine in Firefox but doesn't work in IE. Basically this script adds an onclick function to radio buttons of a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.