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: -
FUNCTION get_info
-
(
-
p_status IN VARCHAR2,
-
p_user IN VARCHAR2,
-
p_type IN VARCHAR2
-
)
-
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?
6 1376
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: -
FUNCTION get_info
-
(
-
p_status IN VARCHAR2,
-
p_user IN VARCHAR2,
-
p_type IN VARCHAR2
-
)
-
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: -
-
FUNCTION get_info
-
(
-
p_status IN VARCHAR2,
-
p_user IN VARCHAR2,
-
p_type IN VARCHAR2
-
) IS
-
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);
-
-
RETURN SYS_REFCURSOR;
-
-
I think this is indeed the rigth track. But not sure what the - '||CHR(39)||p_status||CHR(39)||'
does. Is this some sort of conditional notation?
I think this is indeed the rigth track. But not sure what the - '||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.
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.
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: -
-
FUNCTION get_info
-
(
-
p_status IN VARCHAR2,
-
p_user IN VARCHAR2,
-
p_type IN VARCHAR2
-
) IS
-
BEGIN
-
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)';
-
-
RETURN SYS_REFCURSOR;
-
END;
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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
|
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...
| |