Compare passing values of array with table column [message #389915] |
Wed, 04 March 2009 02:06 |
|
deb.b
Messages: 44 Registered: December 2008
|
Member |
|
|
I need to check the passing value of an array with a column value of table A.
If the value found in column 1 in table A then the row of table A will be inserted into a new table table B with status say T
and if not found then it will be inserted as status F ( table b will have additional column 'Status' ).
I am not being able to figure out how to do this.
Thanks
|
|
|
|
Re: Compare passing values of array with table column [message #389933 is a reply to message #389918] |
Wed, 04 March 2009 03:43 |
|
deb.b
Messages: 44 Registered: December 2008
|
Member |
|
|
Hi,
Here is the test case below. Requirement is we are passing array list value and check with a table column
(here with EMPID) if it exits that row will be inserted in table NEW_EMP with status T else with status F
ARRAY IN LIST VALUE='001','003', '004'
CREATE TABLE EMP
( EMPID VARCHAR2(3),
EMPNAME VARCHAR2(25),
DEPTID VARCHAR2(3));
INSERT INTO EMP VALUES ('001', 'X', '01') ;
INSERT INTO EMP VALUES ('002', 'Y', '02') ;
INSERT INTO EMP VALUES ('003', 'Z', '02') ;
INSERT INTO EMP VALUES ('004', 'A', '03') ;
INSERT INTO EMP VALUES ('005', 'B', '04') ;
CREATE TABLE NEW_EMP
( EMPID VARCHAR2(3),
EMPNAME VARCHAR2(25),
DEPTID VARCHAR2(3));
expected output:
EMPID EMPNAME DEPTID STATUS
001 X 01 T
002 Y 02 F
003 Z 02 T
004 A 03 T
005 B 04 F
How can we do this using MERGE by comparing an array value list.
thanks
|
|
|
|
|
|
|
|
|
|
|
|
Re: Compare passing values of array with table column [message #389957 is a reply to message #389915] |
Wed, 04 March 2009 05:26 |
cookiemonster
Messages: 13920 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
I don't think you want to use merge for this.
I think you want to use insert as select and an outer join.
Coupled with a CASE statement to get the status.
Something like this:
INSERT INTO new_emp (<columns>)
SELECT <columns from array>, <columns from emp)
(CASE WHEN emp.emp_id IS NULL THEN 'F'
ELSE 'T'
END) status
FROM array, emp
WHERE emp.empid(+) = array.empid
EDIT: removed erroneous (+)
[Updated on: Wed, 04 March 2009 05:26] Report message to a moderator
|
|
|
|
|
|
|
Re: Compare passing values of array with table column [message #390151 is a reply to message #390121] |
Thu, 05 March 2009 03:56 |
|
Barbara Boehmer
Messages: 9090 Registered: November 2002 Location: California, USA
|
Senior Member |
|
|
I believe an outer join, as cookiemonster suggested, would be more efficient. You can use case or decode or nvl2 to derive the status.
SCOTT@orcl_11g> CREATE TABLE OLD_EMP
2 ( EMPID VARCHAR2(3),
3 EMPNAME VARCHAR2(25),
4 DEPTID VARCHAR2(3));
Table created.
SCOTT@orcl_11g>
SCOTT@orcl_11g> BEGIN
2 INSERT INTO old_EMP VALUES ('001', 'X', '01') ;
3 INSERT INTO old_EMP VALUES ('002', 'Y', '02') ;
4 INSERT INTO old_EMP VALUES ('003', 'Z', '02') ;
5 INSERT INTO old_EMP VALUES ('004', 'A', '03') ;
6 INSERT INTO old_EMP VALUES ('005', 'B', '04') ;
7 END;
8 /
PL/SQL procedure successfully completed.
SCOTT@orcl_11g>
SCOTT@orcl_11g> CREATE TABLE NEW_EMP
2 ( EMPID VARCHAR2(3),
3 EMPNAME VARCHAR2(25),
4 DEPTID VARCHAR2(3),
5 STATUS VARCHAR2 (1));
Table created.
SCOTT@orcl_11g>
SCOTT@orcl_11g>
SCOTT@orcl_11g> CREATE OR REPLACE PROCEDURE Test
2 (i_depts IN VARCHAR2)
3 AS
4 BEGIN
5 INSERT INTO new_emp (empid, empname, deptid, status)
6 SELECT old_emp.empid, old_emp.empname, old_emp.deptid,
7 NVL2 (array.COLUMN_VALUE, 'T', 'F')
8 FROM old_emp,
9 TABLE (CAST (str2tbl (i_depts) AS MyTableType)) array
10 WHERE old_emp.empid = array.COLUMN_VALUE (+);
11 END Test;
12 /
Procedure created.
SCOTT@orcl_11g> SHOW ERRORS
No errors.
SCOTT@orcl_11g> execute test ('001,003,004')
PL/SQL procedure successfully completed.
SCOTT@orcl_11g> SELECT * FROM new_emp ORDER BY empid
2 /
EMP EMPNAME DEP S
--- ------------------------- --- -
001 X 01 T
002 Y 02 F
003 Z 02 T
004 A 03 T
005 B 04 F
SCOTT@orcl_11g>
|
|
|