Comma Seperated Query for Multiple Parametes in PLSQL - oracle

I am able to query comma separated IN parameter in PLSQL so far with reference to Query with comma seperated IN parameters in PLSQL. and working perfect. My question how do I implement this same solution for at least 3 comma separated parameters. My query parameters are like this,
I_PRODUCT query (R%, L%)
I_MODEL query (E%,T%,R%)
I_TYPE query (A5,B%,C%)
Is it good to make as a function and call for all these parameters? Any other quick solution?
create or replace PROCEDURE RQUERY1
(
I_PRODUCT VARCHAR2
I_MODEL VARCHAR2
I_TYPE VARCHAR2
, O_Cursor OUT SYS_REFCURSOR
) AS BEGIN
O_Cursor := NULL;
OPEN O_Cursor FOR
WITH PROD_SEARCH AS
(
select regexp_substr(I_PRODUCT,'[^,]+', 1, level) pattern from dual
connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null
)
SELECT * FROM table1
WHERE EXISTS (SELECT NULL FROM PROD_SEARCH WHERE table1.PRODUCT LIKE pattern );
END RQUERY1 ;
update: I am looking to query the parameters (I_PRODUCT,I_MODEL,I_TYPE) from my java code using stored procedure and need to display the output value.

Use a collection:
CREATE OR REPLACE PROCEDURE RQUERY1
(
I_PRODUCT IN SYS.ODCIVARCHAR2LIST,
I_MODEL IN SYS.ODCIVARCHAR2LIST,
I_TYPE IN SYS.ODCIVARCHAR2LIST,
O_Cursor OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN O_Cursor FOR
SELECT t.*
FROM table1 t
INNER JOIN TABLE( I_PRODUCT ) p ON t.PRODUCT = p.COLUMN_VALUE
INNER JOIN TABLE( I_MODEL ) m ON t.MODEL = m.COLUMN_VALUE
INNER JOIN TABLE( I_TYPE ) y ON t.TYPE = y.COLUMN_VALUE;
END RQUERY1;
/
Then you can call it in Java like this:
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.internal.OracleTypes;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
public class TestDatabase {
public static void main(String args[]){
try{
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:#url:port:sid","UserName","Password");
String[] products = { "Product1", "Product2", "Product3" };
String[] models = { "Model1", "Model2", "Model3" };
String[] types = { "Type1", "Type2", "Type3" };
ArrayDescriptor des = ArrayDescriptor.createDescriptor("SYS.ODCIVARCHAR2LIST", con);
CallableStatement st = con.prepareCall("call TEST.RQUERY1(?,?,?,?)");
st.setArray( 1, new ARRAY( des, con, products ) );
st.setArray( 2, new ARRAY( des, con, models ) );
st.setArray( 3, new ARRAY( des, con, types ) );
st.registerOutParameter( 4, OracleTypes.CURSOR );
st.execute();
ResultSet cursor = ((OracleCallableStatement)st).getCursor(4);
while ( cursor.next() )
{
int id = cursor.getInt(1);
String product = cursor.getString(2);
String model = cursor.getString(3);
String type = cursor.getString(4);
System.out.println( String.format( "Id: %5d", id ) );
System.out.println( String.format( " Product: %s\t", product ) );
System.out.println( String.format( " Model: %s", model ) );
System.out.println( String.format( " Type: %s", type ) );
}
} catch(ClassNotFoundException | SQLException e) {
System.out.println(e);
}
}
}

Edit:
I finally understood what you want.
As described here, you can define multiple with statements. So you can write it like this:
OPEN O_Cursor FOR
WITH PROD_SEARCH AS
(
select regexp_substr(I_PRODUCT,'[^,]+', 1, level) pattern from dual
connect by regexp_substr(I_PRODUCT, '[^,]+', 1, level) is not null
),
MODEL_SEARCH AS
(
select regexp_substr(I_MODEL,'[^,]+', 1, level) pattern from dual
connect by regexp_substr(I_MODEL, '[^,]+', 1, level) is not null
),
TYPE_SEARCH AS
(
select regexp_substr(I_TYPE,'[^,]+', 1, level) pattern from dual
connect by regexp_substr(I_TYPE, '[^,]+', 1, level) is not null
)
SELECT * FROM table1
WHERE EXISTS (SELECT NULL FROM PROD_SEARCH WHERE table1.PRODUCT LIKE pattern )
AND EXISTS (SELECT NULL FROM MODEL_SEARCH WHERE table1.MODEL LIKE pattern );
AND EXISTS (SELECT NULL FROM TYPE_SEARCH WHERE table1.TYPE LIKE pattern );
END RQUERY1 ;

Related

ORA-01427 Single row subquery return more than one row

I have the below query once I run it, it's exceeded successfully without error, but when I run it inside a procedure, I got the single error.
CREATE OR REPLACE PROCEDURE ABLEA_NEW.AB_VATFILE
IS
fHandle UTL_FILE.FILE_TYPE;
err varchar2(200);
v_str VARCHAR2(4000);
CURSOR VAT1 IS
SELECT (SELECT cif_no
FROM nbfc_customer_m
WHERE customerid = a.bpid) ||
(SELECT EXTRACT_ACCT(HOST_ACCT_INFO, 'SUFFIX')
FROM LEA_AGREEMENT_GROUPGL_MAP A, FA_ACCTCATG_M B
WHERE EXTRACT_ACCT(A.HOST_ACCT_INFO, 'ACCTCATG') = B.ACCTCATG
AND B.GROUPID = 'FA'
AND A.ACTIVE_FLAG = 'Y'
and AGREEMENTID = a.caseid)
"Account No",lpad(a.caseid,6,0) Loan_No
,
(SELECT AGREEMENTNO
FROM lea_agreement_dtl
WHERE AGREEMENTID = a.caseid)
AGREEMENTNO
,
LPAD(A.productid,3,0) Scheme_ID,
(SELECT rpad(schemedesc,35,' ')
FROM lea_scheme_m
WHERE schemeid = a.productid)
SchemeDesc,
to_char(a.advicedate,'ddmmyyyy') advicedate,
it_conv(a.adviceamt) adviceamt,
rpad(a.chargeid,6,' ')chargeid,
(SELECT rpad(chargedesc,35,' ')
FROM nbfc_charges_m
WHERE chargeid = a.chargeid)
"Charge Description",
IT_CONV(a.chargeamt)chargeamt,
(SELECT
decode(count(1),0,'N','Y')
FROM nbfc_pmnt_dtl y
WHERE a.txnadviceid = y.txnadviceid
AND a.status = 'A'
AND y.status IS NULL
--and TRUNC(y.pmntdate) between :p_datefrom and :p_dateto
AND a.tax_applicable = 'Y'
AND a.ptxnadviceid IS NULL)Paid,
LPAD(b.chargeid,6,0)
"VAT ChargeID",
(SELECT RPAD(chargedesc,35,' ')
FROM nbfc_charges_m
WHERE chargeid = b.chargeid)
"VAT Charge Description",
IT_CONV(b.chargeamt)
"VAT Amount"
FROM (SELECT *
FROM nbfc_txn_advice_dtl
WHERE status = 'A' AND tax_applicable = 'Y' AND ptxnadviceid IS NULL)
a,
(SELECT *
FROM nbfc_txn_advice_dtl
WHERE status = 'A' AND ptxnadviceid IS NOT NULL) b
WHERE a.txnadviceid = b.ptxnadviceid;
BEGIN
fHandle := UTL_FILE.FOPEN('UAEDB', 'VAT', 'W');
FOR I IN VAT1
LOOP
v_str:= null;
v_str:= I."Account No"||I.Loan_No||I.AGREEMENTNO || I.Scheme_ID ||I.SchemeDesc|| I.advicedate|| I.adviceamt|| I.chargeid||I."Charge Description"||I.chargeamt||I.Paid||
I."VAT ChargeID" ||I."VAT Charge Description"||I."VAT Amount";
UTL_FILE.PUTF(fHandle,v_str);
UTL_FILE.PUTF(fHandle, '\n');
END LOOP;
UTL_FILE.FCLOSE(fHandle);
END ;
/
How can I solve this?
Note: the query return mor than 10000 record.
comment or un-comment the "(select from )as ColumnAlias" query column one by one, you can find which sub query column return more than one row

stored procedure parameter of CSV input to return all records

I have the following Oracle stored procedure that takes on a string of CSV of user ID's which would return the list of users to the output cursor which works fine:
create or replace PROCEDURE GET_USERS_BY_IDS
(
v_cur OUT sys_refcursor
,v_userIdsCsv IN varchar2 DEFAULT ''
) AS
BEGIN
open v_cur for
with userIds
as
(
select
trim( substr (txt,
instr (txt, ',', 1, level ) + 1,
instr (txt, ',', 1, level+1) - instr (txt, ',', 1, level) -1 ) )
as token
from (select ','||v_userIdsCsv||',' txt
from dual)
connect by level <=
length(v_userIdsCsv)-length(replace(v_userIdsCsv,',',''))+1
)
select
id
,lastname
,firstname
from
users
where
id in (select * from userIds);
END GET_USERS_BY_IDS;
so by doing exec GET_USERS_BY_IDS(:cur1, '123,456') I can get users of IDs of 123 and 456. However I would like to return ALL users if I pass in an empty string, i.e. exec GET_USERS_BY_IDS(:cur1, '') would return all users. What do I have to change in the sproc code to accomplish that? Thanks.
Consider this solution using REGEXP functions which I feel simplifies things. I also incorporated the test from my comment as well. Note the REGEXP handles a NULL list element too:
create or replace PROCEDURE GET_USERS_BY_IDS
(
v_cur OUT sys_refcursor
,v_userIdsCsv IN varchar2 DEFAULT '1'
) AS
BEGIN
open v_cur for
with userIds
as
(
select trim( regexp_substr(v_userIdsCsv, '(.*?)(,|$)', 1, level, NULL, 1) ) as token
from dual
connect by level <= regexp_count(v_userIdsCsv, ',') + 1
)
select
id
,lastname
,firstname
from
users
where v_userIdsCsv = '1' -- Empty list returns all users
OR id in (select * from userIds);
END GET_USERS_BY_IDS;
Its untested so let us know what happens if you test it.
Do you mean, something as simple as
BEGIN
if v_userIdsCsv = '' then
open v_cur for select id, lastname, firstname from users
else (rest of your code)
end if;
?
OK, with the confirmation in comments...
It seems you should be able to change the WHERE condition at the very end:
where
v_userIdsCsv = '' or id in (select * from userIds);
Outer join between user and user_ids. And clever where condition.
Has it helped?
with csv as (select '321,333' aa from dual)
,userIds
as
(
select
trim( substr (txt,
instr (txt, ',', 1, level ) + 1,
instr (txt, ',', 1, level+1) - instr (txt, ',', 1, level) -1 ) )
as token
from (select ','||(select aa from csv )||',' txt
from dual)
connect by level <=
length((select aa from csv ))-length(replace((select aa from csv),',',''))+1
)
select
user_id
,username
from
all_users a
left join userIds b on a.user_id = b.token
where nvl2((select aa from csv),b.token,a.user_id) = a.user_id
I think I found a more efficient way to do this now. In the where statement, I can just short-circuit it if the input parameter is a blank:
where
v_userIdsCsv = '' or
id in (select * from userIds);

Sort the Items with Stored Procedure

So I have following example from my database:
"John" "1"
"Diva" "1"
"Christ" "2"
"Azzam" "2"
"Sunny" "3"
"Daniel" "3"
"Alex" "4"
"Mike" "4"
Two Names can have same NameID. Now I want to sort in such a way that when I pass the parameter "NameID" it will show Name related to that Id at first and the other Name close to that ID respectively. Example If I pass the parameter "3" to Stored Procedure the result should be:
"Sunny" "3"
"Daniel" "3"
"Alex" "4"
"Mike" "4"
"Christ" "2"
"Azzam" "2"
"John" "1"
"Diva" "1"
What I did till now?
Select * From Database
Order by NameID
I couldn't go further than that.
Use a CTE or subquery to assign a weight to the matching NameIDs. Then use that weight so those names appear first in the result set.
Here's an example from SQL Server, but the basic idea can be used in almost any SQL dialect:
SET NOCOUNT ON;
DECLARE #name_id_parameter INT = 3;
DECLARE #names TABLE
(
[name] NVARCHAR(MAX) NOT NULL,
[name_id] INT NOT NULL
);
INSERT INTO #names
(
[name],
[name_id]
)
VALUES
('John', 1),
('Diva', 1),
('Christ', 2),
('Azzam', 2),
('Sunny', 3),
('Daniel', 3),
('Alex', 4),
('Mike', 4);
/* Solution #1: Using a common table expression (CTE) */
WITH cte([name], [name_id], [sort_weight])
AS
(
/* Assign a higher "weight" to the [name_id]s that match
#name_id_parameter, so those [name_id]s will appear
first in the final result set. */
SELECT
[name],
[name_id],
CASE
WHEN [name_id] = #name_id_parameter THEN 1
ELSE 0
END
FROM
#names
)
SELECT
[name],
[name_id]
FROM
cte
ORDER BY
[sort_weight] DESC, [name_id] DESC
/* Solution #2: Using a subquery */
SELECT
S.[name],
S.[name_id]
FROM
/* Assign a higher "weight" to the [name_id]s that match
#name_id_parameter, so those [name_id]s will appear
first in the final result set. */
(SELECT
[name],
[name_id],
[sort_weight] =
CASE
WHEN [name_id] = #name_id_parameter THEN 1
ELSE 0
END
FROM
#names) AS S
ORDER BY
S.[sort_weight] DESC, S.[name_id] DESC
DECLARE #ID AS NVARCHAR(50)
SET #ID = '3';
SELECT *
FROM ( SELECT TOP(100)
[Name] ,
[NameID]
FROM [Test].[dbo].[Table_1]
WHERE NameID = #ID
ORDER BY NameID DESC
) a
UNION ALL
SELECT *
FROM ( SELECT TOP(100)
[Name] ,
[NameID]
FROM [Test].[dbo].[Table_1]
WHERE NameID #ID
ORDER BY NameID DESC
) b
Here is I think what you want:
DECLARE #t TABLE ( name VARCHAR(20), id INT )
INSERT INTO #t
VALUES ( 'John', '1' ),
( 'Diva', '1' ),
( 'Christ', '2' ),
( 'Azzam', '2' ),
( 'Sunny', '3' ),
( 'Daniel', '3' ),
( 'Alex', '4' ),
( 'Mike', '4' )
declare #id int = 3
SELECT * FROM #t
ORDER BY ABS(#id - id), #id - id, name
Output:
name id
Daniel 3
Sunny 3
Alex 4
Mike 4
Azzam 2
Christ 2
Diva 1
John 1

how to delete corresponding matching rows in Oracle SQL?

I'm trying to delete rows in a table only when there is a corresponding entry with a negative amount. The tricky part is there could be more positive than negative or more negative than positive.
value1 amt
12345 50
12345 50
12345 -50
12345 -50
abcde 40
abcde 40
abcde -40
11111 30
11111 -30
11111 -30
The result should be:
abcde 40
11111 -30
I have to apologize. I realized the posters data set was too simple. Here is a revised answer that I believe works.
Basically, you need to partition into pairs and then delete the pairs having sum() = 0.
create table t ( id varchar2(20), val number );
delete from t;
INSERT INTO t ( id, val ) values ( '12345', 50);
INSERT INTO t ( id, val ) values ( '12345', 50);
INSERT INTO t ( id, val ) values ( '12345', -50);
INSERT INTO t ( id, val ) values ( '12345', -50);
INSERT INTO t ( id, val ) values ( 'abcde', 40);
INSERT INTO t ( id, val ) values ( 'abcde', 40);
INSERT INTO t ( id, val ) values ( 'abcde', 20);
INSERT INTO t ( id, val ) values ( 'abcde', 40);
INSERT INTO t ( id, val ) values ( 'abcde', -40);
INSERT INTO t ( id, val ) values ( '11111', 30);
INSERT INTO t ( id, val ) values ( '11111', -30);
INSERT INTO t ( id, val ) values ( '11111', -30);
INSERT INTO t ( id, val ) values ( 'aaaaa', 10);
INSERT INTO t ( id, val ) values ( 'aaaaa', -30);
COMMIT;
MERGE INTO t
USING (WITH value_partition AS
(SELECT t.*,
ROW_NUMBER () OVER (PARTITION BY t.id, t.val ORDER BY ROWID) rn_in_value
FROM t)
SELECT sp.ROWID row_id,
sp.*,
CASE WHEN SUM (sp.val) OVER (PARTITION BY sp.id, ABS (sp.val), rn_in_value) = 0 THEN 'N' ELSE 'Y' END
keep_row
FROM value_partition sp) u
ON (t.ROWID = u.row_id
AND u.keep_row = 'N')
WHEN MATCHED THEN
UPDATE SET t.val = u.val
DELETE
WHERE u.keep_row = 'N';
SELECT * FROM t;

Result set different from query than from stored procedure in Oracle

I have a sp that is giving me wrong results. That sp returns a cursor which is missing some rows. Now
When we fire the query within the sp directly in a query window, correct results come up. The only difference in this query is that instead of a cursor, the query directly outputs the result set; i.e. no cursor is opened when we fire the query directly.
When we recompile the sp, correct results come up for some time, and then it is back to missing rows after some time.
Have not been able to make much progress on this. Any pointers on what to look for, how to make progress etc. are much appreciated. I can paste the sp in here if needed, and probably the sp can be rewritten in some other way to make the problem go away, but I am more interested in knowing root cause - what could be causing this strange problem?
SP
CREATE OR REPLACE PROCEDURE "SP_GETTAXSETTINGCHANNELINFO" (v_bid varchar2,
p_rdTaxSetting out SYS_REFCURSOR) as
vT_bid varchar2(100);
begin
vT_bid := v_bid;
Open p_rdTaxSetting for
Select taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE
FROM (
WITH ChannelData AS (
SELECT ONDEMAND_SELECTED_SOURCES AS OnDemandChannels,
SCHEDULED_SELECTED_SOURCES AS ScheduledChannels
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
Property_Data As (
SELECT
SC.PROPERTY_NUM AS PropertyID
,SC.Company_Name as PropertyName
,SC.competitor_number as PropertyOrder
FROM SUB_ORDER S
INNER JOIN CONTACT C ON S.Supplier_ID = C.Contact_ID
INNER JOIN SUB_ORDER_COMPETITOR SC ON S.Order_Id = SC.Order_ID
WHERE C.BID = vT_bid
AND S.STATUS='Complete' and current_ind = 1
),
ODChannel as (
select regexp_substr (OnDemandChannels, '[^,]+', 1, rn) as Channel
from (Select OnDemandChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (OnDemandChannels, '[^,]+'))) + 1 max_value
from (Select OnDemandChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (OnDemandChannels, '[^,]+', 1, rn) is not null
),
SCDChannel As (
select regexp_substr (ScheduledChannels, '[^,]+', 1, rn) as Channel
from (Select ScheduledChannels From ChannelData)
cross join
(select rownum rn
from (select max (length (regexp_replace (ScheduledChannels, '[^,]+'))) + 1 max_value
from (Select ScheduledChannels From ChannelData))
connect by level <= max_value)
where regexp_substr (ScheduledChannels, '[^,]+', 1, rn) is not null
),
ChaData1 As (
Select
CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel
from ODChannel
),
ChaData2 As (
Select CASE lower(Channel)
WHEN 'galileo' then 'GDS'
else Channel
end AS Channel FROM SCDChannel
),
PropData As (
Select Distinct PropertyID,PropertyName from Property_Data
order by PropertyID
),
AllChannel AS (
Select Channel From ChaData1
union
Select Channel From ChaData2
),
Prop_Cha_Data As (
Select Distinct Channel,PropertyID,PropertyName from AllChannel,Property_Data
order by Channel,PropertyID
),
Tax_Channel_Prop_Exists AS ( select
CASE lower(CHANNELNAME)
WHEN 'galileo' then 'GDS'
else CHANNELNAME
end AS CHANNELNAME,
PropertyName As PROPERTYNAME, TCP.PROPERTYID As PROPERTYID,TAXTYPE,
ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE,taxmappingid
from TAXSETTING_CHANNEL_PROPERTY TCP
INNER JOIN AllChannel PC On TRIM (CASE lower(TCP.CHANNELNAME)
WHEN 'galileo' then 'GDS'else TCP.CHANNELNAME end) = TRIM(PC.Channel)
INNER JOIN PropData CP On TCP.PROPERTYID = CP.PropertyID
where TCP.BID=vT_bid
)
Select Distinct taxmappingid, CHANNELNAME,PROPERTYNAME, PROPERTYID, TAXTYPE, ISPARTIALLYINCLUSIVE, PARTIALLYINCLUSIVEVALUE from Tax_Channel_Prop_Exists
UNION ALL
select DISTINCT 0 As taxmappingid, PCD.Channel As CHANNELNAME,
PCD.PropertyName As PROPERTYNAME ,
PCD.propertyid As PROPERTYID,
-1 As TAXTYPE,
0 As ISPARTIALLYINCLUSIVE,
'' As PARTIALLYINCLUSIVEVALUE
FROM Prop_Cha_Data PCD
WHERE NOT EXISTS (
Select taxmappingid FROM Tax_Channel_Prop_Exists E
WHERE E.channelname = PCD.Channel AND
E.propertyid = PCD.propertyid )
)
Order by PROPERTYNAME,CHANNELNAME ;
end SP_GetTaxSettingChannelInfo;

Resources