How can i make dynamic pl/sql? - oracle

I would like to make dynamic pl/sql using type variables in parameter values.
parameter value
>
type = {'name + place', 'resno', 'hpno', 'telno'};
for example
if i got parameter values('resno', 'hpno')
Two combine queries're required(2 union all)
so, it means that The number of queries depends on the parameter value.
select * from (
--repeatation
select * from
(select a.custnm
, 'name + place' as vtype
, a.custnm || '-' || c.pjtcd || '-' || c.dong || '-' || c.ho as con
, count(a.custid) as nodup
from custtable a
, thng c
where a.custid = c.custid(+)
group by a.custnm, c.pjtcd, c.dong, c.ho
having count(a.custid) > 1) x
--/repeatation
union all
--repeatation
select * from
(select a.custnm
, 'resno' as vtype
, a.resno as condup
, count(a.custid) as nodup
from custtable a
group by a.custnm, a.resno
having count(a.custid) > 1) x2
--repeatation
union all
--repeatation
select * from
(select a.custnm
, 'hpno' as vtype
, a.hpno as condup
, count(a.custid) as nodup
from custtable a
group by a.custnm, a.hpno
having count(a.custid) > 1) x3
--repeatation
union all
--repeatation
select * from
(select a.custnm
, 'telno' as vtype
, a.telno as condup
, count(a.custid) as nodup
from custtable a
group by a.custnm, a.telno
having count(a.custid) > 1) x4
--repeatation
)
order by decode(vtype, 'name +`enter code here` place', 1 ,'resno', 2 ,'hpno', 3, 'telno', 4), nodup desc
plz let me know how to make plsql with parameter value

This is a SELECT statement. The way it is written now, it is SQL, not PL/SQL, and my suggestion is to leave it that way. Instead of writing a horrible PL/SQL (dynamic SQL usually isn't nice), why wouldn't you create a view, based on that statement?
create or replace view v_my_view as
select * from (
--repeatation
select * from
(select a.custnm
, 'name + place' as vtype
<snip>
Once you do that, use it anywhere you want (PL/SQL included).

Related

How to convert delimited string to a PL/SQL table for JOINing?

I have the following table:
CREATE TABLE T_DATA
(
id VARCHAR2(20),
value VARCHAR2(30),
index NUMBER,
valid_from DATE,
entry_state VARCHAR2(1),
CONSTRAINT PK_T_DATA PRIMARY KEY(id, value)
);
and I have the following string:
id1:value1,id2:value2,id3:value3...
where id and value are actually corresponding values on T_DATA. I'm expected to use that string and return a resultset from T_DATA usind the ids and values provided as filters (basically, a select). I was told I can convert the string into a PL/SQL table with the two columns and with that, a simple SELECT * FROM T_DATA INNER JOIN [PL/SQL table] ON [fields] will retrieve the rows required, but I can't find out how to convert the string to a PL/SQL table with multiple columns. How can I do it?
The simplest solution I can think of (although it may not be the most efficient) is to just use a simple INSTR
WITH
t_data
AS
( SELECT 'id' || ROWNUM AS id,
'value' || ROWNUM AS VALUE,
ROWNUM AS index_num,
SYSDATE - ROWNUM AS valid_from,
'A' AS entry_state
FROM DUAL
CONNECT BY LEVEL <= 10)
SELECT *
FROM t_data
WHERE INSTR ('id1:value1,id3:value3', id || ':' || VALUE) > 0;
If you want to split the search string, you can try a query like this one
WITH
t_data
AS
( SELECT 'id' || ROWNUM AS id,
'value' || ROWNUM AS VALUE,
ROWNUM AS index_num,
SYSDATE - ROWNUM AS valid_from,
'A' AS entry_state
FROM DUAL
CONNECT BY LEVEL <= 10),
split_string AS (SELECT 'id1:value1,id3:value3' AS str FROM DUAL),
split_data as (
SELECT substr(regexp_substr(str, '[^,]+', 1, LEVEL),1,instr(regexp_substr(str, '[^,]+', 1, LEVEL), ':') - 1) as id,
substr(regexp_substr(str, '[^,]+', 1, LEVEL),instr(regexp_substr(str, '[^,]+', 1, LEVEL), ':') + 1) as value
FROM split_string
CONNECT BY INSTR (str, ',', 1, LEVEL - 1) > 0)
SELECT t.*
FROM t_data t
join split_data s
on( t.id = s.id and t.value = s.value);
You can use the query using LIKE as follows:
SELECT *
FROM T_DATA
WHERE ',' || YOUR_STRING || ',' LIKE '%,' || ID || ':' || VALUE || ',%'

Oracle - Performance between Regexp_substr and Instr

As my title, somecases I see Regexp_substr faster and less cost than Instr and somecases its opposite.
I don't know when I should use Instr or Regexp_substr, someone can explain for me and tell me benefit of each? The example following:
**Regexp_substr:**
SELECT * FROM tabl1
WHERE 1 = 1
AND col1 IN (
SELECT regexp_substr(abc,'[^,]+',1,level) AS A
FROM (
SELECT 001 abc -- replace with parameter
FROM DUAL
)
CONNECT BY LEVEL <= LENGTH (REGEXP_REPLACE (abc,'[^,]'))+1 );
**Instr:**
SELECT * FROM tabl1
WHERE 1 = 1
AND INSTR (',' || '001' || ',',',' || col1 || ',') > 0 ;
Thanks!

Select data depend with IF ELSE condition

I have a query as below :
SELECT R.*
FROM
(
WITH TABLE_X AS
(
SELECT A,B,C FROM Y
)
--PSEUDO CODE
IF (COUNT(TABLE_X.*) > 0)
THEN SELECT CONCAT(A ,B, C) FROM TABLE_X
ELSE 'No Data'
END
) R
In this case, If TABLE_X have data, the selection will return A,B,C. In others way, this will return something else like 'No data'.
Please help me clarify and suggest some soluiton on it.
Thank you for your attention.
If there is at most one row in TABLE_X, then you can use aggregation:
WITH TABLE_X AS (
SELECT A, B, C FROM Y
)
SELECT (CASE WHEN COUNT(*) = 0 THEN 'No Data'
ELSE MAX(A || B || C)
END) as col
FROM TABLE_X;
Alternatively, use UNION ALL:
WITH TABLE_X AS (
SELECT A, B, C FROM Y
)
SELECT A || B || C as col
FROM TABLE_X
UNION ALL
SELECT 'No Data'
FROM Dual
WHERE NOT EXISTS (SELECT 1 FROM TABLE_X);
Looking too you query i think you simply need
select nvl( A||B,||C , 'No data')
from y

Redundant blank line in query result

I have this sql:
with p_1 as
(
select 1 sorszam, 'X1' tipus from dual
union all select 2 sorszam, 'X2' tipus from dual
union all select 3 sorszam, 'X3' tipus from dual
)
select (
(case when p1.sorszam=1 then ('[' || chr(13) || chr(10)) else '' end) ||
p1.tipus
|| (case when p1.sorszam=(select max(sorszam) from p_1) then (chr(13) || chr(10) || ']') else '' end)
) szoveg
from p_1 p1
order by p1.sorszam
The result is:
SZOVEG
--------
[
X1
X2
X3
]
My question is: why is there a blank line after the first line?
Using SET RECSEP OFF removes the record separator.
http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12040.htm#i2699269

Invalid number of returned comma delimited string in a IN clause in Oracle

I am trying to use a subquery that returns a comma delimited string in a IN clause.
The following way:
SELECT p.person_id, g.name || '>>' || p.firstname || ' ' || p.lastname as GROUP_PATH
FROM PERSON p
LEFT JOIN GROUP g ON (
g.group_id = p.group_id
)
WHERE p.person_id IN (
SELECT person_ids FROM other WHERE other_id = :OTHER_ID
)
ORDER BY lower(GROUP_PATH)
And I am getting the following error:
ORA-01722: invalid number.
Is there a better way to do this or even possible?
The most obvious explanation is that you are trying to do math with a string...
The attempted conversion of a
character string to a number failed
because the character string was not a
valid numeric literal. Only numeric
fields or character fields containing
numeric data may be used in arithmetic
functions or expressions. Only numeric
fields may be added to or subtracted
from dates.
http://ora-01722.ora-code.com/
Update #1:
Your description worries me:
I am trying to use a subquery that
returns a comma delimited string in a
IN clause.
Your subquery should not return a comma delimited string (unless g.group_id is a string and expects a comma delimited string). You must retrieve individual items in as many rows as needed (less than 1,000 anyway).
Update #2:
Just to make it clear:
SELECT *
FROM (
SELECT 1 AS FOO_ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL
) FOO;
FOO_ID
----------------------
1
2
3
You can do this:
SELECT *
FROM (
SELECT 1 AS FOO_ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL
) FOO
WHERE FOO_ID IN (1, 2);
FOO_ID
----------------------
1
2
But not this:
SELECT *
FROM (
SELECT 1 AS FOO_ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL
) FOO
WHERE FOO_ID IN ('1,2');
SQL Error: ORA-01722: invalid number
Because you cannot compare number 1 with string '1,2'. Subqueries follow similar rules:
SELECT *
FROM (
SELECT 1 AS FOO_ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL
) FOO
WHERE FOO_ID IN (
SELECT 1 AS FOO_ID FROM DUAL UNION SELECT 2 FROM DUAL
);
FOO_ID
----------------------
1
2
SELECT *
FROM (
SELECT 1 AS FOO_ID FROM DUAL UNION SELECT 2 FROM DUAL UNION SELECT 3 FROM DUAL
) FOO
WHERE FOO_ID IN (
SELECT '1,2' AS FOO_ID FROM DUAL
);
SQL Error: ORA-01722: invalid number
At a minimum, in order to reference the alias GROUP_PATH, you would need to need to use a nested subquery where the alias is defined before you reference it in your ORDER BY clause. That's realistically not causing the ORA-01722 error, but it is a problem
SELECT group_id, group_path
FROM (SELECT g.group_id,
g.name || '>>' || p.firstname || ' ' || p.lastname as GROUP_PATH
FROM PERSON p
LEFT JOIN GROUP g ON (
g.group_id = p.group_id
)
WHERE p.person_id IN (
SELECT person_ids FROM other WHERE other_id = :OTHER_ID
)
ORDER BY lower(GROUP_PATH)
If the PERSON_IDS column in the OTHER table is a comma separated list of values, your IN list is not going to do what you'd expect. You would need to transform the scalar string (that happens to have commas in it) into some sort of collection of multiple PERSON_ID values. There are various approaches to doing this, Tom Kyte has one example of using a variable IN list. Assuming you copy Tom's IN_LIST function, you should be able to do something like
SELECT group_id, group_path
FROM (SELECT g.group_id,
g.name || '>>' || p.firstname || ' ' || p.lastname as GROUP_PATH
FROM PERSON p
LEFT JOIN GROUP g ON (
g.group_id = p.group_id
)
WHERE p.person_id IN (
SELECT column_value
FROM TABLE(SELECT in_list(person_ids)
FROM other
WHERE other_id = :OTHER_ID)
)
ORDER BY lower(GROUP_PATH)

Resources