Home » SQL & PL/SQL » SQL & PL/SQL » Query with multiple parameters (Oracle 10g)
Query with multiple parameters [message #600592] Fri, 08 November 2013 16:14 Go to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
[MERGED by LF]


i have a query form based on two blocks where i will be passing two where clause one is getting satisfied the other one is not comming correctly as i want to pass IN clause dynamically , i am not able to identify it.


CREATE TABLE OT_JOB_STAT ( JOB_NO VARCHAR2(12),JOB_STATUS VARCHAR2(12),JOB_EQUIP VARCHAR2(12),JOB_TYPE VARCHAR2(12))

insert into ot_job_stat ( JOB_NO ,JOB_STATUS ,JOB_EQUIP ,JOB_TYPE ) values ('0001','Open','CNC1','Prev')

insert into ot_job_stat ( JOB_NO ,JOB_STATUS ,JOB_EQUIP ,JOB_TYPE ) values ('0002','Close','CNC2','Brk')

when i execute the below query its comming correctly bringing all the records if passed parameter is null or depending on the status
it will bring the records.
select * from ot_job_stat where  (1 = 1 and :STATUS IS NULL) or (1 = 0 or JOB_STATUS = :STATUS ) 

JOB_NO	JOB_STATUS	JOB_EQUIP	JOB_TYPE
0001	Open	           CNC1	          Prev
0002	Close	           CNC2	          Brk

but when i add additional filter to filter on job_equip result is similar to first how can i filter only CNC1 OR CNC1 AND CNC2 job_equip along with first query , if i dont input job_equip all the job_equip should come.
select * from ot_job_stat where  (1 = 1 and :STATUS IS NULL) or (1 = 0 or JOB_STATUS = :STATUS ) 
           and job_equip IN (:VAL1) )

--and the :val1 in ('CNC1')
 
JOB_NO	JOB_STATUS	JOB_EQUIP	JOB_TYPE
0001	Open	          CNC1	           Prev
0002	Close	          CNC2	           Brk


[EDITED by LF: fixed [code] tags and removed superfluous empty lines]

[Updated on: Sat, 09 November 2013 08:13] by Moderator

Report message to a moderator

Re: multi select dynamic [message #600616 is a reply to message #600592] Sat, 09 November 2013 08:06 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
If I understood you correctly, you need to enclose the "status" part of the WHERE clause into its own brackets.

This is what you have:
select * from ot_job_stat 
where (1 = 1 and :STATUS IS NULL) or (1 = 0 or JOB_STATUS = :STATUS)
  and job_equip IN (:VAL1);

while it should be
select * from ot_job_stat 
where ((1 = 1 and :STATUS IS NULL) or (1 = 0 or JOB_STATUS = :STATUS))
--    ^                                                              ^
--    |                                                              |  these brackets!
  and job_equip IN (:VAL1);
Re: query with multiple parameters [message #600617 is a reply to message #600592] Sat, 09 November 2013 08:08 Go to previous messageGo to next message
misragopal
Messages: 125
Registered: June 2005
Location: New Delhi, india
Senior Member

Although it is not clear what actually you want to ask but considering that you are looking for solution of
Quote:
if the :equip is passed as 'CNC1' then only CNC1 will come otherwise it bring all the equip.How to combine them together.
select * from ot_job_stat WHERE (1=1 and:EQUIP is null ) OR (1=0 OR job_equip = :equip) OR (1=0 AND :STATUS IS NULL) ;


Because you are only taking '=' condition for job_equip, your query is performing correct. you have to change your query in such a way that it will tackle both conditions.
you need to change "(1=0 OR job_equip = :equip)" condition to show All records or Single record.

Replace above part with this code, ad post results (for both the cases).

   OR (1=0 OR job_equip LIKE NVL(:equip,'%')) 

[Updated on: Sat, 09 November 2013 08:08]

Report message to a moderator

Re: multi select dynamic [message #600622 is a reply to message #600616] Sat, 09 November 2013 09:24 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
Thanks littlefoot, you have reightly pointed out the where clause but there is one problem, actually there can be two or more selections in job_equip columns as user may select two or more machine and my data will be in the format CNC1,CNC2.the where clause is not working if there are two or more equip as below mentioned example.


insert INTO OT_JOB_STAT (
  JOB_NO      ,
  JOB_STATUS,
  JOB_EQUIP   ,
  JOB_TYPE   
)
VALUES(
'0003',
'Close',
'CNC3',
'Prev');

commit;

select * from ot_job_stat 
where ((1 = 1 and :STATUS IS NULL) or (1 = 0 or JOB_STATUS = :STATUS) and job_equip IN (:VAL1));

:status is null
:VAL1  = CNC1,CNC2

with the above parameters i am getting the below results which is wrong

JOB_NO	JOB_STATUS	JOB_EQUIP	JOB_TYPE
0001	Open	           CNC1	            Prev
0002	Close	           CNC2	             Brk
0003	Close	           CNC3	            Prev


Actaull it should filter only for CNC1 AND CNC2

JOB_NO	JOB_STATUS	JOB_EQUIP	JOB_TYPE
0001	Open	           CNC1	            Prev
0002	Close	           CNC2	             Brk



Many Thanks littlefoot and misra

Re: multi select dynamic [message #600623 is a reply to message #600622] Sat, 09 November 2013 09:59 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Isn't LF's solution correct?
orclz> select * from ot_job_stat
  2  where ((1 = 1 and :STATUS IS NULL) or (1 = 0 or JOB_STATUS = :STATUS))
  3  and job_equip IN ('CNC1','CNC2');

JOB_NO       JOB_STATUS   JOB_EQUIP    JOB_TYPE
------------ ------------ ------------ ------------
0001         Open         CNC1         Prev
0002         Close        CNC2         Brk

orclz>
Re: multi select dynamic [message #600624 is a reply to message #600623] Sat, 09 November 2013 10:06 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
>and job_equip IN ('CNC1','CNC2');
above is different from below
>:VAL1 = CNC1,CNC2
Just curious, what datatype is VAL1 where above is valid syntax?
Re: multi select dynamic [message #600625 is a reply to message #600624] Sat, 09 November 2013 12:25 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
Thanks Watson and Blackswan, the datatype is of char type , and actually i passing the values from the T list of another field, as the user selects the data , they will get concatenated as CNC1,CNC2 , Please check the attachment./forum/fa/11235/0/
Re: multi select dynamic [message #600626 is a reply to message #600625] Sat, 09 November 2013 12:43 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Am I supposed to reverse engineer your SQL from a PNG file?
Sorry, man.
Re: multi select dynamic [message #600627 is a reply to message #600626] Sat, 09 November 2013 12:59 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
No please dont mistake me, i am just tryin to tell you what i am doing.

the coding written is as below.


declare
	cnt number;
	var varchar2(32000);
	VAAL VARCHAR2(32500);
begin
cnt := Get_List_Element_Count('IP_REP_INFO.T_LIST_IN');
if cnt >= 1
	then
	var := null;
	for i in 1..cnt loop
		var := var||','||Get_List_Element_Value('IP_REP_INFO.T_LIST_IN',i);
			 VAAL :=  ''''||var||'''';
	end loop;


	:IP_REP_INFO.VAL := rtrim(substr(vaal, 2)); --This is the field which should be considered in In clause of query
	Else
		:IP_REP_INFO.VAL := nvl(:IP_REP_INFO.T_LIST_IN, -1);

end if;

end;

Re: multi select dynamic [message #600628 is a reply to message #600627] Sat, 09 November 2013 13:04 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Your last post does not include one word of SQL.
I give up. Perhaps someone else can help.
Re: multi select dynamic [message #600639 is a reply to message #600628] Sun, 10 November 2013 04:00 Go to previous messageGo to next message
m.abdulhaq
Messages: 254
Registered: April 2013
Location: Ajman
Senior Member
in simple how can i convert this string in sql


select * from ot_job_stat
  where ((1 = 1 and :STATUS IS NULL) or (1 = 0 or JOB_STATUS = :STATUS))
   and job_equip IN (CNC1,CNC2);

change automatically to


select * from ot_job_stat
  where ((1 = 1 and :STATUS IS NULL) or (1 = 0 or JOB_STATUS = :STATUS))
   and job_equip IN ('CNC1','CNC2');







Re: multi select dynamic [message #604598 is a reply to message #600639] Wed, 01 January 2014 17:01 Go to previous message
Barbara Boehmer
Messages: 9090
Registered: November 2002
Location: California, USA
Senior Member
If I understand your problem correctly, you are using Forms or Reports or some such thing to loop through a list of values of IP_REP_INFO.T_LIST_IN and concatenate them into a string using Get_List_Element_Count to get the number of values and Get_List_Element_Value to get each value. You are ending up with a comma separated list something like CNC1,CNC2. The problem is that you are trying to use that value for a bind variable :val1 in an IN clause. Even if you add quotes 'CNC1','CNC2' it still doesn't work right. Tom Kyte wrote a str2tbl function that takes such a string and turns it back into a collection that can be used with TABLE and CAST as a bind variable in an IN clause. The following is a demonstration of that concept. Since I am not using Forms or Reports, I have used other methods to simulate your t_list_in and get_list_element_count and get_list_element_value. I have simplified that portion of the code and provided the alternate lines that you can use.

SCOTT@orcl12c> -- table and data you provided:
SCOTT@orcl12c> CREATE TABLE OT_JOB_STAT
  2    (JOB_NO	    VARCHAR2(12),
  3  	JOB_STATUS  VARCHAR2(12),
  4  	JOB_EQUIP   VARCHAR2(12),
  5  	JOB_TYPE    VARCHAR2(12))
  6  /

Table created.

SCOTT@orcl12c> insert all
  2  into ot_job_stat (JOB_NO ,JOB_STATUS ,JOB_EQUIP ,JOB_TYPE)
  3    values ('0001','Open','CNC1','Prev')
  4  into ot_job_stat (JOB_NO ,JOB_STATUS ,JOB_EQUIP ,JOB_TYPE)
  5    values ('0002','Close','CNC2','Brk')
  6  into ot_job_stat (JOB_NO ,JOB_STATUS ,JOB_EQUIP ,JOB_TYPE)
  7    VALUES ('0003', 'Close', 'CNC3', 'Prev')
  8  select * from dual
  9  /

3 rows created.

SCOTT@orcl12c> -- variables:
SCOTT@orcl12c> VARIABLE status VARCHAR2(12)
SCOTT@orcl12c> VARIABLE val1   VARCHAR2(4000)
SCOTT@orcl12c> -- sample code to simulate generation of value for :val1:
SCOTT@orcl12c> DECLARE
  2    -- simulation of IP_REP_INFO.T_LIST_IN (skip next line):
  3    t_list_in SYS.ODCIVARCHAR2LIST := SYS.ODCIVARCHAR2LIST ('CNC1', 'CNC2');
  4    cnt	 NUMBER;
  5  BEGIN
  6    -- cnt := Get_List_Element_Count('IP_REP_INFO.T_LIST_IN');
  7    -- Use line above instead of line below.
  8    cnt := t_list_in.COUNT;
  9    FOR i IN 1..cnt LOOP
 10  	 -- :val1 := :val1 || Get_List_Element_Value('IP_REP_INFO.T_LIST_IN',i) || ',';
 11  	 -- Use line above instead of line below.
 12  	 :val1 := :val1 || t_list_in(i) || ',';
 13    END LOOP;
 14    :val1 := RTRIM (:val1, ',');
 15    DBMS_OUTPUT.PUT_LINE (:val1);
 16  END;
 17  /
CNC1,CNC2

PL/SQL procedure successfully completed.

SCOTT@orcl12c> -- str2tbl function by Tom Kyte from
SCOTT@orcl12c> -- http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:110612348061
SCOTT@orcl12c> create or replace type myTableType as table of varchar2(255);
  2  /

Type created.

SCOTT@orcl12c> create or replace function str2tbl( p_str in varchar2 ) return myTableType
  2  as
  3  	 l_str	 long default p_str || ',';
  4  	 l_n	    number;
  5  	 l_data    myTableType := myTabletype();
  6  begin
  7  	 loop
  8  	     l_n := instr( l_str, ',' );
  9  	     exit when (nvl(l_n,0) = 0);
 10  	     l_data.extend;
 11  	     l_data( l_data.count ) := ltrim(rtrim(substr(l_str,1,l_n-1)));
 12  	     l_str := substr( l_str, l_n+1 );
 13  	 end loop;
 14  	 return l_data;
 15  end;
 16  /

Function created.

SCOTT@orcl12c> -- query:
SCOTT@orcl12c> SELECT *
  2  FROM   ot_job_stat
  3  WHERE  ((1 = 1 and :STATUS IS NULL)
  4  OR      (1 = 0 or JOB_STATUS = :STATUS))
  5  AND    job_equip IN
  6  	    (SELECT *
  7  	     FROM   TABLE (CAST (str2tbl (:val1) AS mytabletype)))
  8  /

JOB_NO       JOB_STATUS   JOB_EQUIP    JOB_TYPE
------------ ------------ ------------ ------------
0001         Open         CNC1         Prev
0002         Close        CNC2         Brk

2 rows selected.

Previous Topic: Create DB Trigger
Next Topic: How to create outer Join in this Query
Goto Forum:
  


Current Time: Thu Apr 25 13:07:49 CDT 2024