Oracle SQL: How to replace within all members of a list - sql

Using Oracle SQL...
I'd like to use the replace function on all values of a hardcoded list. Something like this...
WHERE (
(field IN replace(('PREFIX-ITEM0','PREFIX-ITEM1','PREFIX-ITEM2','PREFIX-ITEM3','PREFIX-ITEM4','PREFIX-ITEM5','PREFIX-ITEM6','PREFIX-ITEM7','PREFIX-ITEM8','PREFIX-ITEM9'),'PREFIX-','NEWBIE-'))
)
Is there any way to do this?

If you have a fixed list of values - so they aren't coming from a table - then it would be much simpler to modify them externally before putting them into a query.
But if for some reason you can't do that, you could treat the list as a varray of strings, and use a function to replace them individually. In recent versions of Oracle you can do that with a local function defined in a with clause:
with
function myfunc (p_in_list sys.odcivarchar2list, p_old varchar2, p_new varchar2)
return sys.odcivarchar2list is
l_out_list sys.odcivarchar2list;
begin
l_out_list := new sys.odcivarchar2list();
l_out_list.extend(p_in_list.count);
for i in p_in_list.first..p_in_list.last loop
l_out_list(i) := replace(p_in_list(i), p_old, p_new);
end loop;
return l_out_list;
end;
select field
from t
where field in (
select * from myfunc(
sys.odcivarchar2list('PREFIX-ITEM0','PREFIX-ITEM1','PREFIX-ITEM2','PREFIX-ITEM3','PREFIX-ITEM4','PREFIX-ITEM5','PREFIX-ITEM6','PREFIX-ITEM7','PREFIX-ITEM8','PREFIX-ITEM9'),
'PREFIX-',
'NEWBIE-')
)
/
which with some dummy data with a mix of PREFIX and NEWBIE values produces:
FIELD
------------
NEWBIE-ITEM2
NEWBIE-ITEM3
NEWBIE-ITEM4
db<>fiddle

Related

How to use listagg parameter in Oracle procedure where clause

I group a series of IDs using LISTAGG.
Here is an example of a returned value-
A 1,2,3,4,5 PROC.ABC
B 6,7,8 PROC.ABC
C 2,3,4 PROC.DEF
I then try to use a cursor and pass each value into the following procedure:
PROCEDURE abc(id_list IN VARCHAR2) IS
BEGIN
UPDATE table_a SET flag = 1 WHERE id IN (id_list);
END;
This errors out ("Invalid Number" error) because id_list is being inserted as
'1,2,3,4,5'
, not
1,2,3,4,5
. How can I get this to work? I would prefer not to use dynamic SQL if possible.
If you have APEX installed in your database, you can use the APEX_STRING.SPLIT function to split your string by commas. If you know id_list is always going to be numbers, you can use APEX_STRING.SPLIT_NUMBERS as well.
PROCEDURE abc (id_list IN VARCHAR2)
IS
BEGIN
UPDATE table_a
SET flag = 1
WHERE id IN (SELECT * FROM TABLE (apex_string.split (id_list, ',')));
END;
Firstly you need to create a function that returns a list of values.
FUNCTION TVF_SPLIT (expression CLOB, delimiter CHAR) RETURN sys.ODCIVARCHAR2LIST
AS
v_TYPE_TABLE_SEPRATOR sys.ODCIVARCHAR2LIST :=sys.ODCIVARCHAR2LIST();
v_xml_data CLOB;
BEGIN
v_xml_data := '<r><n>' || replace(expression,delimiter,'</n><n>') || '</n></r>';
SELECT * BULK COLLECT INTO v_TYPE_TABLE_SEPRATOR FROM
(
SELECT * FROM
XMLTABLE ( '/r/*'
PASSING xmltype(v_xml_data)
COLUMNS
r VARCHAR2(4000) PATH '/n'
) xmlt
);
RETURN v_TYPE_TABLE_SEPRATOR;
END;
Select * from table(TVF_SPLIT('1,2,3,4,5',','));
Don't use listagg; create a collection type.
create or replace type t_vchar_tab as table of integer
Then replace listagg in the query-
CAST(COLLECT(id) AS t_vchar_tab) as id_list
Then the procedure should reference the collection w/use of MEMBER
PROCEDURE abc(id_list t_vchar_tab) IS
BEGIN
UPDATE table_a SET flag = 1 WHERE id MEMBER OF id_list;
END;

How to make a function that takes code like "SELECT * FROM SOME_TABLE" as an input and returns a table as an output?

I want to create a function that takes some code as an input (e.g. Select * FROM SOME_TABLE) and returns the result of a query as an output.
I want to use it in procedures in order to return tables as a result.
It should look like this:
BEGIN
--some procedure code
CREATE TABLE SOME_TABLE as Select * FROM ...;
Select * FROM table(my_function('Select * FROM SOME_TABLE'));
END;
Important tips:
The resulting table can have multiple columns, from 1 to +inft
The resulting table can have multiple rows, from 1 to +inft
So the size of a table can be both very small or very large.
The input query can have several where, having, partition, and other Oracle constructions.
I want to have a table as an output, not DBMS_OUTPUT.
I can't install any modules/applications, or use other languages hints. However, I can manually create types, functions, procedures.
I tried to search in the net but could not find a solution that meets all my expectations. The best link I've found was this:
https://sqljana.wordpress.com/2017/01/22/oracle-return-select-statement-results-like-sql-server-sps-using-pipelined-functions/
DBMS_SQL.RETURN_RESULT works if your "code" is a select query
DECLARE
l_cur SYS_REFCURSOR;
l_query VARCHAR2(4000) := 'select * from SOME_TABLE';
BEGIN
OPEN l_cur for l_query;
DBMS_SQL.RETURN_RESULT(l_cur);
END;
/
you can create a function that has a string as parameter and return a cursor.
select statement you should pass as a string. in a function you can open a Cursor.
declare
v_sql varchar2(100) := 'select 1,2,3,4,5 from dual';
cur_ref SYS_REFCURSOR;
function get_data(in_sql in varchar2) return SYS_REFCURSOR
as
cur_ret SYS_REFCURSOR;
begin
OPEN cur_ret FOR in_sql;
return cur_ret;
end;
begin
:cur_ref := get_data(v_sql);
end ;
if your select statement is longer than 32K than you maybe should use a clob instead of varchar2 for your Parameter type. But you have to try that

String Pattern Matching in Sql / Pl/sql

I need to match a string to a pattern to validate the given string.
The given string could be like this 1234/5678.
I should validate the string in such a way that the first four and the last four characters will have to be numbers and they must be seperated by a slash.
How can I do this in SQL or PL/SQL?
I tried different functions such as REGEXP_LIKE, REGEXP_REPLACE,REGEXP_SUBSTR.
Can anyone please help me on this?
If this needs to be done in PL/SQL (e.g. you're validating user input, rather than data in a table), you can create a function to do the validation, e.g.:
DECLARE
v_str VARCHAR2(10);
FUNCTION validate_string (in_str VARCHAR2) RETURN BOOLEAN
IS
BEGIN
RETURN regexp_like(in_str, '\d{4}/\d{4}');
END validate_string;
PROCEDURE validation_output (in_str VARCHAR2)
IS
BEGIN
IF validate_string (in_str => in_str) THEN
dbms_output.put_line(in_str||': validated');
ELSE
dbms_output.put_line(in_str||': not validated');
END IF;
END validation_output;
BEGIN
v_str := '1234/5678';
validation_output (v_str);
v_str := '12/5678';
validation_output (v_str);
v_str := NULL;
validation_output (v_str);
END;
/
1234/5678: validated
12/5678: not validated
: not validated
if you are using oracle you can user regexp_like
https://www.techonthenet.com/oracle/regexp_like.php
if you are using mysql regexp or rlike
https://dev.mysql.com/doc/refman/5.5/en/regexp.html
for sqlserver IsMatch()
https://github.com/zzzprojects/Eval-SQL.NET/wiki/SQL-Server-Regex-%7C-Use-regular-expression-to-search,-replace-and-split-text-in-SQL#sql-regex---ismatch
ORACLE
SELECT * FROM T WHERE COL REGEXP_LIKE REGULAREXP
MYSQL
SELECT * FROM T WHERE COL RLIKE REGULAREXP
SELECT * FROM T WHERE COL REGEXP REGULAREXP
sample table:
SELECT * FROM ns_98;
4321/4567
43/45
43898/4521
4388/4521
43885/45215
4388///4521
SELECT a
FROM ns_98
WHERE REGEXP_LIKE (a,'^[0-9]{4}/{1}[0-9]{4}$');
output:
4321/4567
4388/4521

PL/SQL equivalent of SELECT statement

What would be the PL/SQL equivalent of this SQL query:
SELECT * FROM table(OWNER.PACKAGE.get_exam('123456789'));
This is the Function that I am trying to call:
FUNCTION get_exam(id IN VARCHAR2)
RETURN ab_assign_v1
IS
CURSOR c_exams(cid VARCHAR2) IS
SELECT t_api_exam_v1(
sei.person_id, --unique id
l.description --loc description
)
FROM my_view sei
JOIN loc l
ON sei.loc_code = l.loc_code
v_collection ab_assign_v1;
BEGIN
OPEN c_exams(id);
FETCH c_exams BULK COLLECT INTO v_collection;
CLOSE c_exams;
RETURN v_collection;
EXCEPTION
WHEN OTHERS THEN
error_a1.raise_error(SQLCODE, SQLERRM);
END get_exam;
Hope this helps.
DECLARE
lv <COLLECTION_NAME>;
BEGIN
lv:= OWNER.PACKAGE.get_exam('123456789');
dbms_output.put_line(lv.COUNT);
END;
/
Assuming that you want to return the result of a function :
select owner.package.get_exam('123456789') from table
Your function returns a nested table type. You simply need to declare a variable of that type, and assign to it as you would if it were a scalar:
declare
l_coll your_collection_type;
begin
l_coll := OWNER.PACKAGE.get_exam('123456789');
end;
/
In this example your_collection_type is a placeholder for whatever object your function actually returns.
" I am getting this error: PLS-00201: identifier 'ab_assign_v1' must be declared "
ab_assign_v1 is the type used by your function. From the code posted in your revised question it seems that type is in the same schema which owns the package with the function. However your original pseudo-code prefixes the call with the schema name. So, putting two and two together, you need to revise the variable declaration to include the schema too. (You may need to grant EXECUTE on it too, if you haven't done this already).
declare
l_coll OWNER.ab_assign_v1;
begin
l_coll := OWNER.PACKAGE.get_exam('123456789');
end;
/

Define a function in an SQL Navigator query

I would like to extract a function from a piece of SQL-code which is used multiple times in one query. I'm looking for a functionality which is similar to the following (invented by me) syntax:
with f(x) as (return x+1)
select f(thing1), f(thing2), f(thing3) from things
thing1, thing2, thing3 are integer columns in the table "things" in the example. Also, imagine that f is more complicated than an add-one function.
How do I define a function inside a query?
Declaration of a function in the WITH clause of a query is not possible but according to the information presented at OOW it will be in 12c version. So for now you need to create a function as a schema object whether it would be a stand-alone function or part of a package. For example:
create or replace function F(p_p in number)
return number
is
begin
return p_p + 1;
end;
And then call it in a query, ensuring that the data type of a column you are passing in to the function as a parameter is of the same data type as the parameter of the function:
select f(col1)
, f(col2)
, ...
, f(coln)
from your_table
Are you trying to build a function for dynamic table and table's columns, I would do some code like this? And you cannot declare a function in the WITH clause of a query.
SELECT f ( tablename,columnname1 ),
f ( tablename,columnname2 ),
........
FROM tablename;
Create or replace function f (tableName varchar2,ColumnName varchar2)
Return somethingHere
Is
varTableName varchar2(200);
varColumnName varchar2(200);
varValue integer;
t_cid INTEGER;
t_command VARCHAR2(200);
Begin
--Get tableName
varTableName := tableName ;
--Get columnName
varColumnName := ColumnName ;
t_command := 'SELECT ' || varColumnName ||' FROM ' || varTableName;
--Here execute dynamic sql statement
DBMS_SQL.PARSE
DBMS_SQL.DEFINE_COLUMN
DBMS_SQL.EXECUTE
--fatch row values into varValue
DBMS_SQL.COLUMN_VALUE (..,..,varValue);
--then do your x+1 magic here
varValue := varValue+1
--then output your value.
End;