select forall array in sql statement(PL/SQL) - sql

Type tabArray IS TABLE OF TABLE%ROWTYPE;
tableArray tabArray ;
--fill array
SELECT *
BULK COLLECT INTO tableArray
FROM TABLE
WHERE TABLE.field = ....
--work
FOR pos IN 1..tableArray .count
LOOP
dbms_output.put_line(pos||' '||audArray(pos).field);
end loop;
--doesn't work
SELECT * TABLE2
WHERE TABLE2.field in (SELECT filed FROM FORALL tableArray );
Main question: how can I use my array in sql statement (in) ?

First you have to create a type in SQL then can use as given below
CREATE TYPE FRUIT_TT AS TABLE OF VARCHAR2(100)
SELECT column_value AS val
FROM TABLE(FRUIT_TT('Apple','Banana','Apricot'))
WHERE column_value NOT LIKE 'A%';
Here a type FRUIT_TT is created and using it in SQL query.

Here is an example, you just need to adjust your SQL statement.
CREATE TYPE col_ntt IS TABLE OF NUMBER;
CREATE TABLE num_tab
(
col NUMBER
);
INSERT INTO num_tab VALUES(1);
INSERT INTO num_tab VALUES(2);
INSERT INTO num_tab VALUES(4);
DECLARE
l_col1 col_ntt := col_ntt(1, 2, 3);
l_col2 col_ntt;
BEGIN
SELECT *
BULK COLLECT INTO l_col2
FROM num_tab
WHERE col IN (SELECT column_value FROM TABLE(l_col1));
FOR indx IN 1..l_col2.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(l_col2(indx));
END LOOP;
END;
/*
1
2
*/

Related

ORACLE update with returning OLD and NEW values

Is there equivalent to this T-SQL query in PL/SQL (Oracle 12c)?
UPDATE A SET A.columnA = 10 WHERE A.columnB < 30 OUTPUT INSERTED.*, DELETED.*
The query updates table A and at the same time returns the status of the record before the update and after the update.
Trigger is not a solution for me as well as SELECT records before and SELECT records after updating.
Not a direct one, but using RETURNING INTO you will be able to achieve the same effect:
CREATE TABLE A(columnA VARCHAR2(10), columnB INT);
INSERT INTO A(columnA, columnB) VALUES ('Test', 10);
INSERT INTO A(columnA, columnB) VALUES ('Row 2', 20);
CREATE TABLE audit_table(col_new VARCHAR2(10),col_old VARCHAR2(10));
DECLARE
TYPE rec IS RECORD (actual A.columnA%TYPE, old A.columnA%TYPE);
TYPE col_a_t IS TABLE OF rec;
v_a col_a_t;
BEGIN
UPDATE (SELECT A.*, (SELECT A.columnA FROM dual) AS old_columnA FROM A)
SET columnA = 'XYZ'
WHERE columnB < 30
RETURNING columnA, old_columnA BULK COLLECT INTO v_a;
COMMIT;
-- printing for debug
FOR i IN v_a.first .. v_a.last LOOP
dbms_output.put_line('Old =>' || v_a(i).old || ' new => ' || v_a(i).actual);
END LOOP;
-- additional
FORALL i IN v_a.first .. v_a.last
INSERT INTO audit_table VALUES v_a(i);
COMMIT;
END;
/
SELECT * FROM A;
SELECT * FROM audit_table;
DBFiddle Demo
Idea taken from: Returning Old value during update

PLSQL Generate a string list separated by comma in a cursor

Here's my problem: I'm working inside a pl/sql package where I have a loop that compares the rule with table of users. I need to build a comma separated list of strings (#1) in which I can obtain invalid users (users that doesn't match the rule) and then create a sql query (#2) to exclude them from the rule.
1 Comma separated list:
Z=table that results after comparing the rule with main table of users
Below i am not sure if any of the two commented lines are good
declare user_list varchar2(4000)
IF Z IS NULL THEN
-- build the comma separated list
User_list:= --how to buil a list of strings?
END IF;
2 SQL statement:
--Using the created user_list, now just to exclude it from the main rule
IF USER_LIST IS NULL THEN
'SELECT * FROM RULE WHERE USER NOT IN (:USER_LIST)'
ELSE
RETURN RESULT_FROM_RULE;
END IF;
You can achieve your requirement using dynamic sql. See below how you can do it. I have put the comments inline for understading the code.
create table table1 (id number,
ColumnName varchar2(100))
create table table2 (Id number,
col1 varchar2(100),
col2 varchar2(100),
col3 varchar2(100));
Insert all
into TABLE1 (ID, COLUMNNAME) Values (1, 'col1')
into TABLE1 (ID, COLUMNNAME) Values (2, 'col2')
into TABLE2 (ID, col1, col2, col3) Values (1, 'RRR', 'KKK', 'MMM')
into TABLE2 (ID, col1, col2, col3) Values (2, 'ZZZ', 'PPP', 'QQQ')
into TABLE2 (ID, col1, col2, col3) Values (3, 'LLL', 'NNN', 'DDD')
select * from dual;
Code:
DECLARE
var VARCHAR2 (1000);
v_sql VARCHAR2 (2000);
TYPE x_var IS TABLE OF table2%rowtype ;
z_var x_var;
num number:=0;
BEGIN
FOR rec IN ( SELECT columnname
FROM table1
)
LOOP
num := num +1;
if num = 1 then
var:= rec.columnname;
else
var := var || ' , '|| rec.columnname;
end if;
END LOOP;
---This is how the comma seperated list is generated. This answers your query `**1 Comma separated list:**`
var := RTRIM (LTRIM (var, ','), ',');
--This is how you pass the string to your query . This answers your query `**2 SQL statement:**`
v_sql := 'select * from table2 where segment1 in ('|| var||')';
---This is how you execute your query.
EXECUTE IMMEDIATE v_sql BULK COLLECT INTO z_var;
--displaying the result of the query. If you are using a function you can return (z_var).
FOR i IN 1 .. z_var.COUNT
LOOP
DBMS_OUTPUT.put_line (z_var(i).Id ||','||z_var(i).col1);
END LOOP;
END;
Output:
SQL> /
1,RRR
2,ZZZ
3,LLL
PL/SQL procedure successfully completed.
Note: Please note that the you can use a single query suggested by GurV and simplify your job as well.

Create and populate Varray in Oracle SQL

I'm trying to créate a Varray of beans type and populate it, but I'm in a hurry and don't find any usefull example.
arr=[[1,'A'],[2,'B'],[3,'C']]
This is my code:
create table my_table (NUM_OPERACIO NUMBER,TITULS varchar2(3)) ;
insert into my_table values(1,'A');
insert into my_table values(2,'B');
insert into my_table values(3,'C');
create TYPE item IS object( NUM_OPERACIO NUMBER, TITULS varchar2(3));
/
create TYPE arr IS VARRAY(10) OF item;
/
insert into arr values( select NUM_OPERACIO, TITULS from my_table);
FOR i IN 1..3 loop
dbms_output.put_line(arr (i));
END loop;
Help me achive this, please.
Thanks in advance
Oracle Setup:
create table my_table (NUM_OPERACIO NUMBER,TITULS varchar2(3)) ;
insert into my_table values(1,'A');
insert into my_table values(2,'B');
insert into my_table values(3,'C');
CREATE TYPE item IS object( NUM_OPERACIO NUMBER, TITULS varchar2(3));
/
CREATE TYPE item_array IS VARRAY(10) OF item;
/
PL/SQL:
DECLARE
arr item_array;
BEGIN
SELECT item( NUM_OPERACIO,TITULS )
BULK COLLECT INTO arr
FROM my_table;
FOR i IN 1..arr.COUNT loop
dbms_output.put_line(arr(i).NUM_OPERACIO || ', ' || arr(i).TITULS);
END loop;
END;
/

Create procedure for inserting the records, if exception, procedure needs start from exception line

I need to write the procedure for inserting the records in to multiple tables, for example I have 3 table,
CREATE TABLE SOURCE
(
SORT_CODE NUMBER,
FLAG CHAR(1)
);
INSERT INTO SOURCE VALUES(605096,5);
INSERT INTO SOURCE VALUES(605097,5);
INSERT INTO SOURCE VALUES(605098,5);
INSERT INTO SOURCE VALUES(605099,5);
INSERT INTO SOURCE VALUES(605100,5);
INSERT INTO SOURCE VALUES(605101,6);
INSERT INTO SOURCE VALUES(605102,6);
INSERT INTO SOURCE VALUES(605103,6);
INSERT INTO SOURCE VALUES(605104,6);
INSERT INTO SOURCE VALUES(605105,6);
SQL> SELECT * FROM SOURCE;
SORT_CODE F
---------- -
605096 5
605097 5
605098 5
605099 5
605100 5
605101 6
605102 6
605103 6
605104 6
605105 6
10 rows selected.
CREATE TABLE TARGET
(
SORT_CODE NUMBER,
TARGET_SORT_CODE NUMBER
);
Table created.
INSERT 5 VALUES
INSERT INTO TARGET VALUES(605101,189873);
INSERT INTO TARGET VALUES(605102,189874);
INSERT INTO TARGET VALUES(605103,189875);
INSERT INTO TARGET VALUES(605104,189876);
INSERT INTO TARGET VALUES(605105,'');
SELECT * FROM TARGET;
SORT_CODE TARGET_SORT_CODE
---------- ----------------
605101 189873
605102 189874
605103 189875
605104 189876
605105
CREATE TABLE NEWID
(
SORT_CODE NUMBER,
ID_SCODE NUMBER
);
Table created.
INSERT 2 VALUES
INSERT INTO TARGET VALUES(605103,189875);
INSERT INTO TARGET VALUES(605104,189876);
SELECT * FROM NEWID;
SORT_CODE ID_SCODE
---------- ----------------
605103 189875
605104 189876
Creating intermediate tables with existing table's structure.
CREATE TABLE SOURCE_TEMP AS (SELECT * FROM SOURCE WHERE 1=2);
CREATE TABLE TARGET_TEMP AS (SELECT * FROM TARGET WHERE 1=2);
CREATE TABLE NEWID_TEMP AS (SELECT * FROM NEWID WHERE 1=2);
My Procedure for inserting the records
CREATE OR REPLACE PROCEDURE insert_sql
is
BEGIN
DELETE FROM SOURCE_TEMP;
INSERT INTO SOURCE_TEMP SELECT * FROM SOURCE; --insert query 1
DELETE FROM TARGET_TEMP;
INSERT INTO TARGET_TEMP SELECT * FROM TARGET; --insert query 2
--due to some network issue or table error this procedure GOT EXEPCTION here and above insert query 2(TARGET_TEMP) and below --insert query 3(NEWID_TEMP) is not inserted the values or not executed procedure is came out from this line.
DELETE FROM NEWID_TEMP;
INSERT INTO NEWID_TEMP SELECT * FROM NEWID; --insert query 3
EXCEPTION
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('ERROR');
END;
Point 1: The above procedure is executed only one insert query 1 SOURCE_TEMP is got the values.
Point 1: TARGET_TEMP and NEWID_TEMP is not inserted the values or not execute.
My question: can I re-execute this procedure with starting point of '--insert query 2' line?
because I am inserting the 100 tables records in new tables, if 50 tables are inserted the values during this time if I am getting any error in the proc execution, remaining 50 tables needs to insert the values, for I don't wish to delete the previous 50 tables inserted the values it will be the time consuming activity. Any save point or boolean concepts is there for this type of issue in ORACLE (which is available in java and unix). if yes how to use this function?
CREATE OR REPLACE PROCEDURE insert_sql
is
v_new_rec_count int:=0;
BEGIN
select count(*)
into v_new_rec_count
from (
select * FROM SOURCE
minus
select * FROM SOURCE_TEMP
) ;
If v_new_rec_count >0 then
INSERT INTO SOURCE_TEMP SELECT * FROM SOURCE; --insert query 1
Commit;--permanently save the records in table and it wont be rolledback after - -- any subsequent failure.
v_new_rec_count :=0;
end if;
select count(*)
into v_new_rec_count
from (
select * FROM TARGET
minus
select * FROM TARGET_TEMP
) ;
If v_new_rec_count >0 then
INSERT INTO TARGET_TEMP SELECT * FROM TARGET; --insert query 2
Commit;--permanently save the records in table and it wont be rolledback after - -- any subsequent failure.
v_new_rec_count :=0;
end if;
select count(*)
into v_new_rec_count
from (
select * FROM NEWID
minus
select * FROM NEWID_TEMP
) ;
If v_new_rec_count >0 then
INSERT INTO NEWID_TEMP SELECT * FROM NEWID; --insert query 2
Commit;--permanently save the records in table and it wont be rolledback after - -- any subsequent failure.
v_new_rec_count :=0;
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('ERROR');
END;
CREATE OR REPLACE PROCEDURE insert_sql
IS
x int;
FLG FLAG.FLAG%type;
BEGIN
dbms_output.enable;
SELECT FLAG INTO FLG FROM FLAG;
if FLG='PASS01' then
DBMS_OUTPUT.PUT_LINE('PASS01');
DELETE FROM SOURCE_TEMP;
DBMS_OUTPUT.PUT_LINE('SOURCE_TEMP-DELETED');
INSERT INTO SOURCE_TEMP SELECT * FROM SOURCE;
DBMS_OUTPUT.PUT_LINE('SOURCE_TEMP-INSERTED');
UPDATE FLAG SET FLAG='PASS02';
DBMS_OUTPUT.PUT_LINE('PASS02-updated');
SELECT FLAG INTO FLG FROM FLAG;
DBMS_OUTPUT.PUT_LINE('PASS02-SELECT');
COMMIT;
end if;
if FLG='PASS02' then
DBMS_OUTPUT.PUT_LINE('PASS02');
DELETE FROM TARGET_TEMP;
DBMS_OUTPUT.PUT_LINE('TARGET_TEMP-DELETED');
INSERT INTO TARGET_TEMP SELECT * FROM TARGET;
DBMS_OUTPUT.PUT_LINE('TARGET_TEMP-INSERTEDD');
UPDATE FLAG SET FLAG='PASS03';
DBMS_OUTPUT.PUT_LINE('PASS03-updated');
SELECT FLAG INTO FLG FROM FLAG;
DBMS_OUTPUT.PUT_LINE('PASS03-FLG');
COMMIT;
end if;
--x :=1/0;
if FLG='PASS03' then
DBMS_OUTPUT.PUT_LINE('PASS03');
DELETE FROM NEWID_TEMP;
DBMS_OUTPUT.PUT_LINE('NEWID_TEMP-DELETED');
INSERT INTO NEWID_TEMP SELECT * FROM NEWID;
DBMS_OUTPUT.PUT_LINE('NEWID_TEMP-INSERTEDD');
UPDATE FLAG SET FLAG='PASS01';
DBMS_OUTPUT.PUT_LINE('PASS01-updated');
SELECT FLAG INTO FLG FROM FLAG;
DBMS_OUTPUT.PUT_LINE('PASS01-FLG');
COMMIT;
end if;
EXCEPTION WHEN NO_DATA_FOUND THEN DBMS_OUTPUT.PUT_LINE('NO_DATA_FOUND!');
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error Message: !'|| SQLERRM ||' Sql Code: ' || SQLCODE);
END;
CREATE OR REPLACE PROCEDURE insert_sql
is
v_new_rec_count int:=0;
CURSOR cur_src
IS
SELECT * FROM SOURCE ;
TYPE cur_src_typ IS TABLE OF cur_src%ROWTYPE;
cur_src_tbl cur_src_typ;
CURSOR cur_newid
IS
SELECT * FROM newid ;
TYPE cur_newid_typ IS TABLE OF cur_src%ROWTYPE;
cur_newid_tbl cur_newid_typ;
CURSOR cur_target
IS
SELECT * FROM target ;
TYPE cur_target_typ IS TABLE OF cur_src%ROWTYPE;
cur_target_tbl cur_target_typ;
BEGIN
cur_src_tbl :=cur_src_tbl();
cur_newid_tbl :=cur_newid_tbl();
cur_target_tbl :=cur_target_tbl();
OPEN cur_src;
LOOP
FETCH cur_src BULK COLLECT INTO cur_src_tbl LIMIT 500;
EXIT WHEN cur_src_tbl.count =0;
FOR i IN cur_src LOOP
BEGIN
INSERT INTO SOURCE_TEMP VALUES(cur_src(i).sort_code,cur_src(i).flag);
COMMIT;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
CLOSE cur_src;
OPEN cur_target;
LOOP
FETCH cur_target BULK COLLECT INTO cur_target_tbl LIMIT 500;
EXIT WHEN cur_target.count =0;
FOR i IN cur_target_tbl LOOP
BEGIN
INSERT INTO target_temp VALUES(cur_target_tbl(i).sort_code,cur_target_tbl(i).target_sort_code);
COMMIT;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
CLOSE cur_newid;
OPEN cur_newid;
LOOP
FETCH cur_newid BULK COLLECT INTO cur_newid_tbl LIMIT 500;
EXIT WHEN cur_newid_tbl.count =0;
FOR i IN cur_newid_tbl LOOP
BEGIN
INSERT INTO newid_TEMP VALUES(cur_newid_tbl(i).sort_code,cur_newid_tbl(i).flag);
COMMIT;
EXCEPTION
WHEN OTHERS THEN NULL;
END;
CLOSE cur_newid;
END;
Thanks to All ur support
I created the Proc, Finally I got PLSQL-PROCEDURE to execute my concepts.
create TABLE FLAG
(
FLAG VARCHAR2(6)
);
INSERT INTO FLAG VALUES('PASS01');

How to insert data into a table from a record variable holding multiple fields through a stored procedure

How to insert data into a table from a record variable holding multiple fields through a stored procedure??
scenario is:
create or replace
PROCEDURE CAN_PRICE AS
TYPE REC IS RECORD(FILLER VARCHAR2(20),PACKAGE_SIZE VARCHAR2(20),WHOLESALE_PRICE VARCHAR2(20),DEALERMBA VARCHAR2(20),DEALER_PRICE VARCHAR2(20),UNIT_OF_MEASURE VARCHAR2(20),PART_SYMBOL VARCHAR2(20),CORE_CHANGE VARCHAR2(20),FILLER1 VARCHAR2(20),PART_CLASS VARCHAR2(20),TRADE_PRICE VARCHAR2(20),RETAIL_PRICE VARCHAR2(20));
TYPE T1 IS TABLE OF NUMBER;
CURSOR C1 is SELECT ROW_NUMBER FROM TEMP1 WHERE TRADE_PRICE LIKE '%SEE%';
T T1;
R REC;
P VARCHAR2(20);
P1 NUMBER;
BEGIN
OPEN C1;
FETCH C1 BULK COLLECT INTO T;
CLOSE C1;
FOR i IN T.FIRST .. T.LAST
LOOP
SELECT PART_NUMBER INTO P FROM TEMP1 WHERE ROW_NUMBER =T(i);
SELECT FILLER,PACKAGE_SIZE,WHOLESALE_PRICE,DEALERMBA,DEALER_PRICE,UNIT_OF_MEASURE,PART_SYMBOL,CORE_CHANGE,FILLER1,PART_CLASS ,TRADE_PRICE,RETAIL_PRICE INTO R FROM TEMP1 WHERE ROW_NUMBER=T(i)+1;--FOR EXCHANGE ROW
SELECT TO_NUMBER(WHOLESALE_PRICE) INTO P1 FROM TEMP1 WHERE ROW_NUMBER=T(i)+2;--FOR CORE CHARGE
EXECUTE(' INSERT INTO CHR_CS_BL_1.BL_MACS_CAN_PRICE(FILLER,
PACKAGE_SIZE,
WHOLESALE_PRICE,
DEALERMBA,
DEALER_PRICE,
UNIT_OF_MEASURE ,
PART_SYMBOL,
CORE_CHANGE,
PART_NUMBER,
FILLER1,
PART_CLASS ,
RETAIL_PRICE ,
CORE_CHG )
VALUES
(R.FILLER,R.PACKAGE_SIZE,TO_NUMBER(R.WHOLESALE_PRICE) ,TO_NUMBER(R.DEALERMBA),TO_NUMBER(R.DEALER_PRICE),
R.UNIT_OF_MEASURE,R.PART_SYMBOL,R.CORE_CHANGE,P,R.FILLER1,R.PART_CLASS ,TO_NUMBER(R.TRADE_PRICE),TO_NUMBER(R.RETAIL_PRICE),P1)');
END LOOP;
END;
---I want to insert values HELD IN RECORD VARIABLE R into table BL_MACS_CAN_PRICE?
HOW DO ACHIEVE THAT?