Insert bulk records in to remote database (dblink) using Bulk Collect - oracle

I want to insert huge records from different tables in to a destination remote table 'Audition_Detail' using DBLINK - #FMATLINK. I have used Bulk collect, but its throwing errors. I have gone through some links too:
Overcoming the restriction on bulk inserts over a database link
PLS-00394: Wrong number of values in the INTO list of a fetch statement
The code is as follows:
DECLARE
TYPE FETCH_ARRAY IS TABLE OF AUDITION_DETAIL#FMATLINK%ROWTYPE;
A_DATA FETCH_ARRAY;
CURSOR A_CUR IS
--------------------------------------------------------Address1--------------------------------------------------------------------------
SELECT A.PARTY_SITE_NUMBER FMAT_FMATID, B.ZADDRESSFMATID F4F_FMATID,
C.ADDRESS1 FMAT_VALUE, B.STREET F4F_VALUE , 'ADDRESS1'
FROM APPS.HZ_PARTY_SITES#FMATLINK A , f4f_corporateaccount B , APPS.HZ_LOCATIONS#FMATLINK C
WHERE 1=1
AND B.ROLECODETEXT = 'Site Account'
AND A.PARTY_SITE_NUMBER = B.ZADDRESSFMATID
AND A.STATUS = 'A'
UNION ALL
------------------------------------------------------Address2-----------------------------------------------------------------------------
SELECT A.PARTY_SITE_NUMBER FMAT_FMATID, B.ZADDRESSFMATID F4F_FMATID,
C.ADDRESS2 FMAT_VALUE, B.addressline1 F4F_VALUE , 'ADDRESS2'
FROM APPS.HZ_PARTY_SITES#FMATLINK A , f4f_corporateaccount B , APPS.HZ_LOCATIONS#FMATLINK C
WHERE 1=1
AND B.ROLECODETEXT = 'Site Account'
AND A.PARTY_SITE_NUMBER = B.ZADDRESSFMATID
AND A.STATUS = 'A'
BEGIN
OPEN A_CUR;
LOOP
FETCH A_CUR BULK COLLECT INTO A_DATA LIMIT 20;
FORALL IN 1..A_DATA.COUNT
INSERT INTO AUDITION_DETAIL#FMATLINK VALUES A_DATA(i);
EXIT WHEN A_CUR%NOTFOUND;
END LOOP;
CLOSE A_CUR;
COMMIT;
END;
Error report -
ORA-06550: line 39, column 3:
PLS-00394: wrong number of values in the INTO list of a FETCH
statement
ORA-06550: line 39, column 3:
PL/SQL: SQL Statement ignored
ORA-06550: line 40, column 4:
PLS-00739: FORALL INSERT/UPDATE/DELETE not supported on remote tables
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

The error message seems clear enough :
FORALL INSERT/UPDATE/DELETE not supported on remote tables.
Indeed you link to another question which explains that this is an implementation restriction. PL/SQL does not allow us to use FORALL statements across database links and that is that.
Fortunately you don't need to use bulk collect and FORALL in your code. A simple INSERT INTO .... SELECT statement should see you right:
INSERT INTO AUDITION_DETAIL#FMATLINK
SELECT A.PARTY_SITE_NUMBER FMAT_FMATID, B.ZADDRESSFMATID F4F_FMATID,
C.ADDRESS1 FMAT_VALUE, B.STREET F4F_VALUE , 'ADDRESS1'
FROM APPS.HZ_PARTY_SITES#FMATLINK A , f4f_corporateaccount B , APPS.HZ_LOCATIONS#FMATLINK C
WHERE 1=1
AND B.ROLECODETEXT = 'Site Account'
AND A.PARTY_SITE_NUMBER = B.ZADDRESSFMATID
AND A.STATUS = 'A'
UNION ALL
------------------------------------------------------Address2-----------------------------------------------------------------------------
SELECT A.PARTY_SITE_NUMBER FMAT_FMATID, B.ZADDRESSFMATID F4F_FMATID,
C.ADDRESS2 FMAT_VALUE, B.addressline1 F4F_VALUE , 'ADDRESS2'
FROM APPS.HZ_PARTY_SITES#FMATLINK A , f4f_corporateaccount B , APPS.HZ_LOCATIONS#FMATLINK C
WHERE 1=1
AND B.ROLECODETEXT = 'Site Account'
AND A.PARTY_SITE_NUMBER = B.ZADDRESSFMATID
AND A.STATUS = 'A'
Your code doesn't use the explicit ANSI 92 join syntax and you have scrunched up the code so it's hard to read. Consequently it is easy to miss that you haven't written a join condition for APPS.HZ_LOCATIONS#FMATLINK C. So both subqueries will produce a Cartesian Product for all records in C. You probably don't want this.
SELECT A.PARTY_SITE_NUMBER as FMAT_FMATID
, B.ZADDRESSFMATID as F4F_FMATID
, C.ADDRESS2 as FMAT_VALUE
, B.addressline1 as F4F_VALUE
, 'ADDRESS2'
FROM APPS.HZ_PARTY_SITES#FMATLINK A
inner join f4f_corporateaccount B on A.PARTY_SITE_NUMBER = B.ZADDRESSFMATID
inner join APPS.HZ_LOCATIONS#FMATLINK C on ** something goes here **
WHERE B.ROLECODETEXT = 'Site Account'
AND A.STATUS = 'A'
Easier to understand, easy to spot the missing join. Readability is a feature.

Related

Oracle delete from tableA where a duplicate row is in tableB

As the title says, I am looking for a way to remove all rows from TableA where there is a matching row in TableB.
the Tables A & B have about 30 columns in them so a WHERE A.col1 = B.col1 etc would be a little problematical. Ideally I was hoping for something like
DELETE FROM tableA WHERE IN TableB
(overly simplified by this type of thing)
IN clause can compare all columns returned from select
DELETE FROM tableA WHERE ( col1,col2,col3,.. ) IN ( select col1,col2,col3... FROM TableB );
The brute force way to establish if two records from each table are the same is to just compare every column:
DELETE
FROM tableA a
WHERE EXISTS (SELECT 1 FROM tableB b WHERE a.col1 = b.col1 AND a.col2 = b.col2 AND ...
a.col30 = b.col30);
You could create function which checks structures of tables and, if they are the same, creates string containing correct conditions to compare.
For example here are two tables:
create table t1 (id, name, age) as (
select 1, 'Tom', 67 from dual union all
select 2, 'Tia', 42 from dual union all
select 3, 'Bob', 16 from dual );
create table t2 (id, name, age) as (
select 1, 'Tom', 51 from dual union all
select 3, 'Bob', 16 from dual );
Now use function:
select generate_condition('T1', 'T2') from dual;
result:
T1.ID = T2.ID and T1.NAME = T2.NAME and T1.AGE = T2.AGE
Copy this, paste and run delete query:
delete from t1 where exists (select 1 from t2 where <<PASTE_HERE>>)
Here is the function, adjust it if needed. I used user_tab_columns so if tables are on different schemas you need all_tab_columns and compare owners too. If you have Oracle 11g you can replace loop with listagg(). Second table has to contain all columns of first table and they have to be same type and length.
create or replace function generate_condition(i_t1 in varchar2, i_t2 in varchar2)
return varchar2 is
v varchar2(1000) := '';
begin
for rec in (select column_name, u2.column_id
from user_tab_cols u1
left join (select * from user_tab_cols where table_name = i_t2) u2
using (column_name, data_type, data_length)
where u1.table_name = i_t1 order by u1.column_id)
loop
if rec.column_id is null then
v := 'ERR: incompatible structures';
goto end_loop;
end if;
v := v||' and '||i_t1||'.'||rec.column_name
||' = '||i_t2||'.'||rec.column_name;
end loop;
<< end_loop >>
return(ltrim(v, ' and '));
end;
If you want to avoid running process manually you need dynamic PL/SQL.
create table tableA (a NUMBER, b VARCHAR2(5), c INTEGER);
create table tableB (a NUMBER, b VARCHAR2(5), c INTEGER);
As you said
WHERE A.col1 = B.col1 etc would be a little problematical
you could intersect the tables and mention all columns from tableA one time, like this:
delete tableA
where (a,b,c) in (select * from tableA
intersect
select * from tableB);

using MAX function in HQL

The below query has some issues. The subquery that I use here runs without any errors when run separately. However if I run the whole query below it ends in error.
"Error while compiling statement: FAILED: ParseException line 13:30 cannot recognize input near 'select' 'max' '(' in expression specification"
select acct_num from table1
where ind = 'Y'
and b = (select max(yr_mth_num) from table2)
and st_dt = (select cast(max(st_dt) as date) from table2)
Try this:
SELECT T1.acct_num
FROM table1 T1
LEFT SEMI JOIN
(SELECT
max(yr_mth_num) as max_yr_mth_num,
cast(max(st_dt) as date) as max_st_dt
FROM table2) T2
ON T1.b = T2.max_yr_mth_num
AND T1.st_dt = T2.max_st_dt
AND T1.ind = 'Y'

subquery inside INSERT statement

I just recently found out that subqueries are not allowed in INSERT statements that are inside stored procedures. This is my script:
begin
execute immediate 'truncate table itcustadm.GL_DTPJ_TEST2';
insert into GL_DTPJ_TEST2
(rule_no,
posted_by_user_id,
transaction_id,
transaction_sr_no,
dr_amount,
cr_amount,
tran_crncy_code,
bkdt_tran_flg,
bank_desc
)
select
tq.rule_no,
tq.posted_by_user_id,
tq.transaction_id,
tq.transaction_sr_no,
tq.dr_amount,
tq.cr_amount,
tq.tran_crncy_code,
tq.bkdt_tran_flg,
(select ent.bank_desc from crmuser.end ent where ent.bank_id = gam.bank_id);
But since the (select ent.bank_desc from crmuser.end ent where ent.bank_id = gam.bank_id) at the bottom of the SELECT statement is not allowed by Oracle, what's the best way to accomplish this?
I actually have this code right before the INSERT statement, but I don't know how to exactly use it:
get_bank_desc := '(select ent.bank_desc from crmuser.end ent ' ||
'where ent.bank_id = gam.bank_id)';
I am not sure what you are exactly trying for, but below code may be useful for you, you can achieve inserting a SubQuery output into a table using below query sample, but make sure output of the SubQuery is a single row o/p, so that you can escape from "ORA-01427: single-row SubQuery returns more than one row" ERROR.
insert into test_ins1
values(1,(SELECT COL2 FROM TEST_INS WHERE COL1=1 ));
Even then you can use rownum in where condition and take the single value.
Please let me know in case of any doubts
declare
bank_desc_temp bank_desk_type; /* the type defined in crmuser.ent for bank_desc*/
begin
select ent.bank_desc into bank_desc_temp from crmuser.end ent where ent.bank_id = gam.bank_id;
execute immediate 'truncate table itcustadm.GL_DTPJ_TEST2';
insert into GL_DTPJ_TEST2
(rule_no,
posted_by_user_id,
transaction_id,
transaction_sr_no,
dr_amount,
cr_amount,
tran_crncy_code,
bkdt_tran_flg,
bank_desc
)
select
tq.rule_no,
tq.posted_by_user_id,
tq.transaction_id,
tq.transaction_sr_no,
tq.dr_amount,
tq.cr_amount,
tq.tran_crncy_code,
tq.bkdt_tran_flg,
bank_desc_temp;
end;
When you say "not allowed" what do you mean? Did you get an error?
I ask, because subqueries are definitely allowed inside an insert as select statement, providing you have the syntax correct (and the subquery returns at most one row), e.g.:
create table test_tab (col1 number, col2 varchar2(10));
begin
insert into test_tab
select 1,
(select 'Yes' from dual d2 where d.dummy = d2.dummy)
from dual d;
commit;
end;
/
select * from test_tab;
COL1 COL2
---------- ----------
1 Yes
There are some syntax issues with the code you provided - where is the from clause, and where are the tq and gam aliases defined?
There are two syntax you can use in your insert statement:
(I)
INSERT INTO table_name( column1, column2....columnN)
VALUES ( value1, value2....valueN);
(II)
INSERT INTO table (column1, column2, ... )
SELECT expression1, expression2, ...
FROM source_table(s)
WHERE conditions;
In your example, you should choose the second approach:
insert into GL_DTPJ_TEST2 (rule_no,
posted_by_user_id,
transaction_id,
transaction_sr_no,
dr_amount,
cr_amount,
tran_crncy_code,
bkdt_tran_flg,
bank_desc
)
select tq.rule_no,
tq.posted_by_user_id,
tq.transaction_id,
tq.transaction_sr_no,
tq.dr_amount,
tq.cr_amount,
tq.tran_crncy_code,
tq.bkdt_tran_flg,
ent.bank_desc
from crmuser.gam
join crmuser.end ent
on ent.bank_id = gam.bank_id
;
basically, if you want to add records using an insert statement, you should use a full select statement first. Here is how I would do it:
(1)
select *
from table1;
(2)
select column1
,column2
,column3
from table1;
(3)
select t1.column1
,t1.column2
,t1.column3
,t2.column4
,t2.column5
from table1 t1
join table2 t2
on t2.id = t1.id
;
(4)
insert into table3 (col1
,col2
,col3
,col4
,col5)
select t1.column1
,t1.column2
,t1.column3
,t2.column4
,t2.column5
from table1 t1
join table2 t2
on t2.id = t1.id
;

loop based on query result get error

I have the result of a first query, and then based on that result I want to join with other tables so I can select the fields in the query result and other tables.
I construct the following
BEGIN
FOR a_rec in (select col1, min(col2) as col2,col3 from a_tbl group by (col1, col3))
LOOP
select a_rec.col2, d_tbl.col4
from b_tbl b, c_tbl c, d_tbl d
where b.col1 = a_rec.col1 and c.col5 = b.col5 and d.col6 = c.col6;
END LOOP;
END;
then it reminds me that PLS-00428: an INTO clause is expected in this SELECT statement, so I create a temp table and the code becomes:
BEGIN
FOR a_rec in (select col1, min(col2) as col2,col3 from a_tbl group by (col1, col3))
LOOP
select a_rec.col2, d_tbl.col4 into tmp_tbl
from b_tbl b, c_tbl c, d_tbl d
where b.col1 = a_rec.col1 and c.col5 = b.col5 and d.col6 = c.col6;
END LOOP;
END;
then I get error like this PLS-00403: expression 'TMP_TBL' cannot be used as an INTO-target of a SELECT/FETCH statement
Maybe I should put the first query result into a temp table and then join it with other tables, although I wonder whether I can do it in such a procedure?
A SELECT INTO throws an error if the SELECT statement returns anything other than a single row. So that's clearly not what you want here. Since tmp_tbl is a global temporary table, you could do an INSERT SELECT in your loop
BEGIN
FOR a_rec in (select col1, min(col2) as col2,col3
from a_tbl
group by (col1, col3))
LOOP
INSERT INTO tmp_tbl( col2, col4 )
select a_rec.col2, d_tbl.col4
from b_tbl b,
c_tbl c,
d_tbl d
where b.col1 = a_rec.col1
and c.col5 = b.col5
and d.col6 = c.col6;
END LOOP;
END;
Since you say that you are just turning around and exporting the resulting data in SQL Developer, however, the temporary table doesn't seem particularly useful. You could remove all the PL/SQL and just write a single SELECT statement
select a.col2, d.col4
from (select col1, min(col2) as col2, col3
from a_tbl
group by col1, col3) a,
b_tbl b,
c_tbl c,
d_tbl d
where b.col1 = a.col1
and c.col5 = b.col5
and d.col6 = c.col6;

Oracle Optimizer Unexpected Results

I have a co worker who wrote the following query. The first one works and the second one does not. Also if you remove the aggregate function from the subquery, it works. The oracle optimizer is doing something weird. Any thoughts? Running in SQL Developer 3.1 against 11.1.0.6.0 64 bit.
This works:
SELECT
a.fd_customer_key
, b.fd_customer_key
, b.counter
FROM FETCH_CUSTOMER a
, (select fd_customer_key, count(*) as counter from fetch_customer_order group by fd_customer_key) b
where a.fd_customer_key = b.fd_customer_key (+)
and b.counter is null
This doesn’t:
SELECT
a.fd_customer_key
, b.fd_customer_key
, b.counter
FROM FETCH_CUSTOMER a
, (select fd_customer_key, count(*) as counter from fetch_customer_order group by fd_customer_key) b
where a.fd_customer_key = b.fd_customer_key (+)
and b.fd_customer_key is null
Actually yes, both of the queries you provided are supposed to wrok the same way, but if i understand your need well, you are trying to select the fd_customer_key which has no Order?
I suggest the following query for your need, its more simple and less consuming :
SELECT a.fd_customer_key
FROM FETCH_CUSTOMER a
WHERE NOT EXISTS (SELECT 1
FROM fetch_customer_order b
WHERE a.fd_customer_key = b.fd_customer_key)
It seems like you are trying to make an anti-join (find the rows from FETCH_CUSTOMER that have no corresponding rows in FETCH_CUSTOMER_ORDER).
With Oracle you do not have to use this clever OUTER JOIN trick to write an anti-join, you could use a NOT IN or NOT EXISTS operator and let the optimizer find the best plan. This will be just as efficient and easier to read.
Anyway, I can't reproduce your findings, here's my setup:
CREATE TABLE a (ID NUMBER PRIMARY KEY);
CREATE TABLE b (a_id NUMBER NOT NULL, DATA VARCHAR2(30));
INSERT INTO a (SELECT object_id FROM all_objects);
INSERT INTO b (SELECT object_id, object_name
FROM all_objects WHERE object_type = 'VIEW');
SELECT a.id, b.a_id, b.cnt
FROM a, (SELECT a_id, COUNT(*) cnt FROM b GROUP BY a_id) b
WHERE a.id = b.a_id (+)
AND b.cnt IS NULL;
SELECT a.id, b.a_id, b.cnt
FROM a, (SELECT a_id, COUNT(*) cnt FROM b GROUP BY a_id) b
WHERE a.id = b.a_id (+)
AND b.a_id IS NULL;
You will find that both queries return rows. What is your DB version?

Resources