How to filter and retrieve the results after a Specific characters from stored procedure in oracle plsql? - oracle

I have a column "Names" in the "Employee" table that has following values. The values either contain only single name (first, last, username) or Multiple names separated with semicolon (;). I need to search the values from that table either by first name or last name or username.
I have created a procedure but it is fetching only 1st,4th,5th records. Please let me know how to retrieve 2nd and 3rd records as well.
Firstname and lastname can be given by user with minimum of 2 characters length.
Username is given entire.
Employee:
ID Name Title
1 Andrea Warbutton (awr01) Manager
2 Claire Taylor (cta02);Mark Kites (mak03);Anitha Rooney (anr06) HOD;Supervisor;Business
3 Dave Rites (dar12);Jessica Simpson (jesi10) Lead;Analyst
4 Nick Ken (nik56) Product (Local,Regional)
5 Claire Pilkington (cpt09) Sales Owner
Code:
Create or replace empl (pm_firstname varchar2(100),
pm_lastname varchar2(100),
pm_username varchar2(100))
BEGIN
Select * from Employee
where Upper(Name) like Upper(pm_firstname ||'%'||) -- this will fetch 1st,4th,5th record
OR Upper(SUBSTR(Name, INSTR(Name),' '+1)) like Upper(pm_lastname ||'%'||) -- this will fetch 1st,4th,5th record
OR upper(REGEXP_SUBSTR(Name,'\((.+)\)',1,1,NULL,1)) = Upper(pm_username); -- -- this will fetch 1st,4th,5th record
END;
End empl ;
Please let me know how to retrieve 2nd and 3rd records as well.
Desired Output:
When searched with firstname = "Andrea", the output is below
ID Name Title
1 Andrea Warbutton (awr01) Manager
When searched with firstname = "Claire", the output is below
ID Name Title
2 Claire Taylor (cta02) HOD
5 Claire Pilkington (cpt09) Sales Owner
When searched with lastname = "Simps", the output is below
ID Name Title
3 Jessica Simpson (jesi10) Analyst
When searched with username = "mak03", the output is below
ID Name Title
2 Mark Kites (mak03) Supervisor
When searched with username = "nik56", the output is below
ID Name Title
4 Nick Ken (nik56) Product (Local,Regional)

with
x as (select id, name, '"'||replace(name, ';', '","')||'"' xml from employee),
n as (select id, name, column_value as cv from x, xmltable(xml))
select id,
trim(regexp_substr(cv, '(\S*)(\s)')) fname,
trim(regexp_substr(cv, '(\S*)(\s)', 1, 2)) lname,
regexp_substr(cv, '\((.+)\)', 1, 1, NULL, 1) uname
from n
Your task would be much easier if you normalize these data. Above query outputs:
ID FNAME LNAME UNAME
1 Andrea Warbutton awr01
2 Claire Taylor cta02
2 Mark Kites mak03
2 Anitha Rooney anr06
3 Dave Rites dar12
3 Jessica Simpson jesi10
4 Nick Ken nik56
5 Claire Pilkington cpt09
demo
Now you can search first, last, usernames however you want. First expression finds first word, then second and word between brackets.
Edit:
I posted the table structure with just ID and Name columns. However, I
have Titles column also in the same format separated with (semicolon).
In this case, How can I Normalize Titles as well along with Names
This query worked for provided examples:
with
x as (select id, name, '"'||replace(name, ';', '","')||'"' xmln,
'"'||replace(title, ';', '","')||'"' xmlt
from employee),
n1 as (select id, trim(xn.column_value) nm, rownum rn from x, xmltable(xmln) xn),
n2 as (select id, trim(xt.column_value) tt, rownum rn from x, xmltable(xmlt) xt)
select id, trim(regexp_substr(nm, '(\S*)(\s)')) fname,
trim(regexp_substr(nm, '(\S*)(\s)', 1, 2)) lname,
regexp_substr(nm, '\((.+)\)', 1, 1, NULL, 1) uname,
tt title
from n1 join n2 using (id, rn)
dbfiddle demo
Be careful however, because we cannot write ideal query. If you have entries like Benicio Del Toro, Mary Jo Catlett, Jean Claude Van Damme, it's impossible to write correct regexp. Sometimes second word is part of lastname, sometimes it is firstname, middlename etc.
The proper way is to modify table structure, divide rows, check results and put correct values in correct name columns. Now you have lists which are hard to search and every method may return wrong results.

No need for PL/SQL.
SQL> with temp as
2 (select id,
3 regexp_substr(name, '[^;]+', 1, column_value) name
4 from employee cross join
5 table(cast(multiset(select level from dual
6 connect by level <= regexp_count(name, ';') + 1
7 ) as sys.odcinumberlist))
8 )
9 select id, name
10 from temp
11 where instr(name, '&search_for_name') > 0;
Enter value for search_for_name: Claire
ID NAME
---------- ------------------------------
2 Claire Taylor (cta02)
5 Claire Pilkington (cpt09)
SQL> /
Enter value for search_for_name: mak03
ID NAME
---------- ------------------------------
2 Mark Kites (mak03)
SQL>
What does it do?
temp CTE splits semi-colon separated values into rows
final query uses a simple instr function which detects whether "rows" (extracted previously) contain value you're looking for
If it must be a function, that code can be reused. As you didn't say what exactly (which datatype, I mean) you want to return, I returned a string.
SQL> create or replace function f_search (par_what in varchar2)
2 return sys.odcivarchar2list
3 is
4 retval sys.odcivarchar2list;
5 begin
6 with temp as
7 (select id,
8 regexp_substr(name, '[^;]+', 1, column_value) name
9 from employee cross join
10 table(cast(multiset(select level from dual
11 connect by level <= regexp_count(name, ';') + 1
12 ) as sys.odcinumberlist))
13 )
14 select id ||' - '|| name
15 bulk collect into retval
16 from temp
17 where instr(name, par_what) > 0;
18
19 return retval;
20 end;
21 /
Function created.
SQL> select * from table(f_search('Andrea'));
COLUMN_VALUE
--------------------------------------------------------------------------------
1 - Andrea Warbutton (awr01)
SQL> select * from table(f_search('Claire'));
COLUMN_VALUE
--------------------------------------------------------------------------------
2 - Claire Taylor (cta02)
5 - Claire Pilkington (cpt09)
SQL>

Related

Insert record from one table to another table - Oracle

I have a table TABLE1 which has 5 columns (ROLL_NO, NAME, UNITS, CODE, AMOUNT);
CREATE TABLE TABLE1 (ROLL_NO VARCHAR2(3), NAME VARCHAR2(4), UNITS NUMBER, AMOUNT NUMBER, CODE VARCHAR2(3));
------------------------------------------------------------------------------------------
INSERT INTO TABLE1 VALUES ('101', 'JOHN', 1, 6, 'ABC');
INSERT INTO TABLE1 VALUES ('101', 'JOHN', 2, 6, 'ABC');
INSERT INTO TABLE1 VALUES ('102', 'TOMS', 1, 7, 'ABC');
INSERT INTO TABLE1 VALUES ('102', 'TOMS', 6, 7, 'ABC');
INSERT INTO TABLE1 VALUES ('103', 'FINN', 1, 1, 'BCD');
ROLL_NO NAME UNITS AMOUNT CODE
-------------------------------------------------------
101 JOHN 1 6 ABC
101 JOHN 2 6 ABC
-------------------------------------------
102 TOMS 1 7 ABC
102 TOMS 6 7 ABC
103 FINN 1 1 BCD
There is second table TABLE2 where we need to insert data from TABLE1
CREATE TABLE TABLE2 (ROLL_NO VARCHAR2(3), NAME VARCHAR2(4), RESULT VARCHAR2(3));
There are three conditions to insert data into TABLE2
1st case : If CODE is 'ABC' and SUM(UNITS) of particular ROLL_NO is equal to AMOUNT then don't insert data into TABLE2
2nd case : If CODE is 'ABC' and SUM(UNITS) of particular ROLL_NO is not equal to AMOUNT then insert data with RESULT column value as 'YES'
3rd case : If CODE is not 'ABC' then RESULT column will be 'YES'.
Note: NAME, CODE and AMOUNT will be same for particular ROLL_NO though ROLL_NO has multiple UNITS.
Example :
ROLL_NO 102 CODE 'ABC' and two lines with SUM(UNITS) as 7 and its equal to AMOUNT i.e. 7 and (1st case)
ROLL_NO 101 has CODE 'ABC' and two lines with SUM(UNITS) as 3 and its not equal to AMOUNT i.e. 6 (2nd case)
ROLL_NO 103 has CODE 'BCD' which is not equal to 'ABC'(3rd case)
At the end TABLE2 should have
ROLL_NO NAME RESULT
-----------------------------
101 JOHN YES
103 FINN YES
I have tried this oracle query but it is inserting data related to 102 ROLL_NO which I don't need
SELECT T1.ROLL_NO, T1.NAME,
CASE
WHEN T1.CODE <> 'ABC' THEN 'YES'
WHEN T1.CODE = 'ABC' AND T2.TOT_UNITS <> T1.AMOUNT THEN 'YES'
END RESULT
FROM (SELECT DISTINCT ROLL_NO, NAME, AMOUNT, CODE
FROM TABLE1 ) T1
JOIN (SELECT ROLL_NO, SUM(UNITS) AS TOT_UNITS
FROM TABLE1
GROUP BY ROLL_NO) T2 ON T1.ROLL_NO = T2.ROLL_NO
I am not able to figure out how to not insert ROLL_NO 102 record into TABLE2..Can anyone provide better query than this if possible? Thank you
A "better" option is to scan table1 only once.
SQL> insert into table2 (roll_no, name, result)
2 with temp as
3 (select roll_no, name, sum(units) sum_units, amount, code,
4 case when code = 'ABC' and sum(units) = amount then 'NO'
5 when code = 'ABC' and sum(units) <> amount then 'YES'
6 else 'YES'
7 end as result
8 from table1
9 group by roll_no, name, amount, code
10 )
11 select roll_no, name, result
12 from temp
13 where result = 'YES';
2 rows created.
SQL> select * from table2;
ROL NAME RES
--- ---- ---
101 JOHN YES
103 FINN YES
SQL>

It is possible insert dummy (dash) into first row data use select statement in oracle SQL?

table_A
col_color col_name col_qty
- - - <----- dummy dash
RED APPLE 2
YEL BANANA 1
GRN GREEN_APPLE 3
Hi, it is posible to insert first row of dummy dash for viewing not store into database
use oracle sql plus ?
Anyone help is much apprecited.
One option is to UNION two data sets; one contains dummy dashes, while another contains "real" data. Note that dashes are considered to be strings, which means that you'll have to cast other datatypes to character datatype (see to_char(deptno) in my example):
SQL> with temp as
2 (select 1 rn, '-' deptno , '-' dname, '-' loc from dual
3 union all
4 select 2 rn, to_char(deptno), dname , loc from dept
5 )
6 select deptno, dname, loc
7 from temp
8 order by rn, deptno;
DEPTNO DNAME LOC
---------- -------------- -------------
- - -
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL>
The rn column is used to correctly sort the output (dashes first, the rest of data next).
If you don't want to use 'with', then how about this?
(
SELECT '-' COL_COLOR
, '-' COL_NAME
, '-' COL_QTY
FROM DUAL
)
UNION ALL
(
SELECT *
FROM table_A
)
I think this way is the best way not using 'with'.

How to get 'IS NULL' where clause values if initial condition returns none in oracle

I have a table with values as follows
![ID S_ID NAME
1 James
2 2455 Patrick
3 2566 Sam
4 25988 Rick]1
My requirement is to get the NAME based on S_ID and if the given S_ID is not in the table then get the NAME with no S_ID
EX:
SELECT NAME FROM STUDENTS WHERE S_ID=2455
The result would be Patrick
SELECT NAME FROM STUDENTS WHERE S_ID=2411
The result should be James
I have tried
select NAME from STUDENTS where S_ID=2455 or S_ID IS NULL
It gives be bot James and Patrick
I would really appreciate any help
Here's one option:
SQL> set ver off
SQL> with students (id, s_id, name) as
2 (select 2, 2389, 'Patrick' from dual union all
3 select 1, null, 'James' from dual union all
4 select 3, 2566, 'Sam' from dual
5 )
6 select name from students where s_id = &&par_s_id
7 and exists (select null from students where s_id = &&par_s_id)
8 union all
9 select name from students where s_id is null
10 and not exists (select null from students where s_id = &&par_s_id);
Enter value for par_s_id: 2389
NAME
-------
Patrick --> because S_ID = 2389 exists in a table
SQL> undefine par_s_id
SQL> /
Enter value for par_s_id: 123456
NAME
-------
James --> because S_ID = 123456 doesn't exist
SQL>
As you already have the table, you'd use code from line #6 onward.
#Joe,
As per my understanding if S_ID is found in the STUDENT table it should return the NAME of the student, if S_ID is not found the query should return a message as 'No #S_ID'.
Please let me know if I am making any mistake in understanding your question !
[Solution]
Its a Tsql so we can use it as is in the sql or in any language. And note I have tested it in MSSQL but the approach remains the same.
declare #sidInput int
set #sidInput = 2455
if exists (select NAME from STUDENTS where S_ID = #sidInput)
begin
select NAME from STUDENTS where S_ID = #sidInput
end
else
begin
declare #outputMsg varchar(20)
set #outputMsg = 'No ' + convert( varchar(10), #sidInput)
select top 1 #outputMsg as NAME from STUDENTS where S_ID not in ( #sidInput )
end
[Test Cases]
1) For Input #sidInput = 2455
Output - NAME as Patrick
2) For Input #sidInput = 2411
Output - NAME as James
3) For Input #sidInput = 2222 #S_ID value not found in STUDENT table
Output - NAME as No 2222
Let me know if it works for you.
Regards,
Arnab
#Joe,
try this t-sql (and pardon me I was using maria db - so used it ) . basically it remains the same as for my above response its just that I have updated the else block. ( Note - If you are using MySQL can simply use this as it for testing )
DROP PROCEDURE IF EXISTS prod_studentName;
DELIMITER $$
CREATE PROCEDURE`prod_studentName`()`
BEGIN
declare sidValue int;
set sidValue = 2389;
if exists (select NAME from STUDENT where S_ID = sidValue )
then
select NAME from STUDENT where S_ID = sidValue;
else
select NAME from STUDENT where S_ID is NULL;
end if;`
END$$
DELIMITER ;
refer to the attached image for the table inputs -
test outcomes
positive test - lookup for s_id = 2389
outcome -
negative test - lookup for s_id = 7777 this id doesn't even exist at all
outcome -
hope this helps you

Combining two stored procedures into a single one using some joins

I am very new to Oracle; I have written two stored procedures where they have both different parameters. I would like to combine the queries in that two stored procedures into single query in a single stored procedure, and make sure it supports any criteria.
These are the stored procedures:
procedure usp_testsp1(RC1 OUT RCT1,
in_dep_Id IN number,
in_Org_id IN number,
in_emp_no IN number)
is
begin
select emp.Name as name,select emp.phone as phone,select emp.Race as
race
from employee emp
JOIN
Project prj ON
emp.ProjectId=prj.ProjectId
JOIN Project_Org Prorg
ON prj.ProjectId=Prorg.ProjectId
JOIN Organization Org1
ON Prorg.OrgId=Org1.OrgId
JOIN Organization Org2
ON emp.OrgId=Org2.OrgId
AND Org2.OrgType=0
WHERE (upper(emp.emp_No) in (in_emp_No ))
AND Prorg.OrgId=in_Org_id
AND Org2.OrgId=in_dep_Id
end;
procedure usp_testsp2(RC1 OUT RCT1,
in_dep_Id IN number,
in_Org_id IN number,
in_vendor_id IN raw,
in_vendor_startdate IN date,
in_vendor_enddate IN date)
is
begin
OPEN RC1 FOR
select emp.Name as name,select emp.phone as phone,select emp.Race as
race
from
from employee emp
JOIN
Project prj ON
emp.ProjectId=prj.ProjectId
JOIN Project_Org Prorg
ON prj.ProjectId=Prorg.ProjectId
JOIN Organization Org1
ON Prorg.OrgId=Org1.OrgId
JOIN Organization Org2
ON emp.OrgId=Org2.OrgId
AND Org2.OrgType=0
INNER JOIN vendor_Emp
ON emp.employeeid=vendor_emp.employeeid
INNER JOIN Vendor
ON Vendor.VendorId=Vendor_emp.VendorId
WHERE Prorg.OrgId=in_Org_id
AND Org2.OrgId=in_dep_Id
AND Vendor.VendorId=in_vendor_id
AND vendor.StartDate=in_vendor_startdate AND Vendor.EndDate=in_vendor_enddate
end;
My objective is to combine the queries within these two separate procedures into a single query such that if I combine all the parameters, I have in_emp_no, in_vendor_id, in_vendor_startdate, in_vendor_enddate apart from the common parameters any of the parameters can be null and I want to make sure that those things won't affect the rest of the query even though its not null
For ex i have only in_emp_no ='xxxx' and common inputs rest of the params like
in_vendor_id etc is null.
I would like to make my query to work even with single paramter to filter the result set
NOTEWithout using Dynamic SQL
Thanks
BJ
You can make columns in the query behave as optional, as far as your join's go, you may need to play with various combinations to make sure everything works.
Sample Table
create table sqltest (
empid number,
name varchar2(10),
orgid number,
depid number,
vendorid number);
insert into sqltest values(1,'BoB',1,1,21);
insert into sqltest values(2,'Chuck',1,1,21);
insert into sqltest values(3,'Mary',1,2,21);
insert into sqltest values(4,'Jane',1,2,22);
insert into sqltest values(5,'Rick',2,1,22);
insert into sqltest values(6,'Samir',2,6,23);
insert into sqltest values(7,'Kirk',3,6,23);
insert into sqltest values(8,'Alex',3,9,23);
commit;
Sample Proc
create or replace procedure calltest (
rc1 out SYS_REFCURSOR,
pempid number,
porgid number,
pdepid number,
pvendorid number)
is
begin
open rc1 for
select * from sqltest
where
(pempid IS NULL OR empid = pempid) and
(porgid IS NULL OR orgid = porgid) and
(pdepid IS NULL OR depid = pdepid) and
(pvendorid IS NULL OR vendorid = pvendorid);
end;
Sample Call
set autoprint on;
var vcur refcursor;
declare
begin
calltest(rc1=>:vcur,pempid=>NULL,porgid=>1,pdepid=>NULL,pvendorid=>NULL);
--calltest(rc1=>:vcur,pempid=>NULL,porgid=>1,pdepid=>2,pvendorid=>NULL);
--calltest(rc1=>:vcur,pempid=>NULL,porgid=>1,pdepid=>2,pvendorid=>22);
--calltest(rc1=>:vcur,pempid=>NULL,porgid=>NULL,pdepid=>NULL,pvendorid=>23);
end;
Sample Output 1
VCUR
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EMPID NAME ORGID DEPID VENDORID
--------------------------------------- ---------- --------------------------------------- --------------------------------------- ---------------------------------------
1 BoB 1 1 21
2 Chuck 1 1 21
3 Mary 1 2 21
4 Jane 1 2 22
Sample Output 2
VCUR
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EMPID NAME ORGID DEPID VENDORID
--------------------------------------- ---------- --------------------------------------- --------------------------------------- ---------------------------------------
3 Mary 1 2 21
4 Jane 1 2 22
Sample Output 3
VCUR
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EMPID NAME ORGID DEPID VENDORID
--------------------------------------- ---------- --------------------------------------- --------------------------------------- ---------------------------------------
4 Jane 1 2 22
Sample Output 4
VCUR
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
EMPID NAME ORGID DEPID VENDORID
--------------------------------------- ---------- --------------------------------------- --------------------------------------- ---------------------------------------
6 Samir 2 6 23
7 Kirk 3 6 23
8 Alex 3 9 23

sql loader load multiple lines from one line

I'm facing an issue loading a file with sql loader.
This is what my input file looks like:
#ID; CARD_NUMBER;USERNAME
1;86730975,86536522,86793501;JOHN SMITH
2;89734562;MICHAEL ABOT
3;87903546,87265390;JAMES ALBERT
And what I want to acheive after loading is this:
#Table data
1;86730975;JOHN SMITH
1;86536522;JOHN SMITH
1;86793501;JOHN SMITH
2;89734562;MICHAEL ABOT
3;87903546;JAMES ALBERT
3;87265390;JAMES ALBERT
I did something like this before using Sql Loader, but I can't find the source code. So if someone can help that'll be very nice for me.
I'm new to this forum and I don't know how to find all the posts related to my username, I'll appreciate if someone can help on that to.
You can go with external tables. To me personally it'll be a more convenient way to get it done:
Create a directory in Oracle that points to the exact location of the file on the server.
SQL> create or replace directory ff1 as 'c:\';
directory created
Create external table similar to this one:
SQL> create table t1(
2 t1_id number,
3 card_no varchar2(4000),
4 user_name varchar2(4000)
5 )
6 organization external(
7 type oracle_loader
8 default directory ff1
9 access parameters(
10 records delimited by newline
11 fields terminated by ';'
12 )
13 location('input.dat') --<-- file name
14 ) reject limit unlimited
15 ;
Table created.
After that, you can query that external table, and use any (there are many of them) method to split comma separated string. In this case it would be values of card_number. For example:
This is the actual data:
SQL> column card_no format a40;
SQL> column user_name format a17;
SQL> select * from t1;
T1_ID CARD_NO USER_NAME
---------- ---------------------------------------- -----------------
1 86730975,86536522,86793501 JOHN SMITH
2 89734562 MICHAEL ABOT
3 87903546,87265390 JAMES ALBERT
3 rows selected.
Transformed version of actual data:
SQL> column card_no format 9999999999;
SQL> with ocrs(ocr) as(
2 select level
3 from ( select max(regexp_count(card_no, '[^,]+')) as mx
4 from t1 ) s
5 connect by level <= s.mx
6 )
7 select *
8 from (select t.t1_id
9 , to_number(regexp_substr(t.card_no, '[^,]+',
10 1, o.ocr)) as card_no
11 , t.user_name
12 from t1 t
13 cross join ocrs o ) w
14 where w.card_no is not null
15 order by w.t1_id;
Result:
T1_ID CARD_NO USER_NAME
----------- ----------- -----------------
1 86730975 JOHN SMITH
1 86793501 JOHN SMITH
1 86536522 JOHN SMITH
2 89734562 MICHAEL ABOT
3 87265390 JAMES ALBERT
3 87903546 JAMES ALBERT
6 rows selected.
Having done that, you can use simple insert statement combined with the above query to insert new, transformed data into another table.
insert into another_table(id, card_number, user_name)
with ocrs(ocr) as(
select level
.....
/* the above query */

Resources