Merger of Sql - doubt
Hi all
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod PL/SQL Release 10.2.0.1.0 - Production CORE 10.2.0.1.0 Production TNS for 32-bit Windows: Version 10.2.0.1.0 - Production NLSRTL Version 10.2.0.1.0 - Production
can we use SQL MERGE like that
CREATE or REPLACE PROCEDURE SAVE_PCS_SUMMARY (vENTRYDATE IN VARCHAR2, vCELLID IN VARCHAR2, vMACHINEID IN VARCHAR2, vFREQUENCY IN TANK)
IS
l_sqlstr varchar2 (32000);
BEGIN
l_sqlstr: = PCS_PC_SUMMARY. GETSQLFORPLDAY_TIME (vENTRYDATE, vCELLID, vMACHINEID, vFREQUENCY); -RETUEN SQL STATEMENT
MERGE INTO PCS_TL_MONTH_COMP P
USING (SELECT * FROM l_sqlstr) S - HERE I HAVE PROBLEM
WE (P.COMPANYID = S.FACT_COMPANYID AND P.ENTRYDATE = S.PRLM_ENTRYDATE)
WHEN NOT MATCHED THEN
INSERT of VALUES (l_sqlstr); -HERE I HAVE PROBLEM
END;
My problem is that I can't use the sql retail chain in the USING clause and INSERT values.
or is there another way?
You can't have a SQL statement that is the part regular SQL and SQL dynamic. You will need to make the whole dynamic MERGE statement.
Something like
merge_stmt: = ' MERGE IN pcs_tl_month_comp p using (SELECT * FROM ' | l_sqlstr |)
') s ON (p.company_id = s.fact_companyid AND p.entrydate = s.prlm_entrydate) ' |
' WHEN NOT MATCHED THEN INSERT VALUES ('| l_sqlstr |') ' ;
immediately run merge_stmt;
But I'm sure that this does not work either as if l_sqlstr has the correct syntax for the first use, it will not have the correct syntax for the second.
Tags: Database
Similar Questions
-
Merger of SQL in writeback OBIEE 11 g
Hello
I have a requirement to insert again record of OBIEE 11 g report and the table has (TKT_ID) primary key defined in the table. I can't insert null check in the DB table. Not be able to achieve my requirement using SQL INSERT/UPDATE statements.
I tried with the merger of the SQL statement, but I am unable to reach the requirement. Here's my query.
MERGE INTO A_WB HAS
USING (SELECT * FROM A_WB where TKT_ID = '12343') B
ON (A.TKT_ID = B.TKT_ID)
WHEN MATCHED THEN
UPDATE SET OUT_CAUSE = "VZ".
WHEN NOT MATCHED THEN
INSERT (TKT_ID, OUT_CAUSE, CUSTOMER_ID) VALUES ('12343',' OTHER', ' 21')
Please correct me if I missed something...
Thank you
Prasanna
SET VARIABLE DISABLE_CACHE_HIT = 1; has solved my problem and get the updated records to date.
-
any body can help me to merg below of the sql statements for the information
No. 1
Select S.USERNAME, s.sid, s.osuser, t.sql_id, sql_text
de v$ sqltext_with_newlines t, V$ SESSION s
où t.Address = s.sql_address
et t.hash_value = s.sql_hash_value
et s.status = 'ACTIVE'
et s.UserName <> 'SYSTEM'
order by s.sid, t.piece
/
Statements #2
SELECT SID, to_char (start_time,'hh24:mi:ss') stime,
message, (sofar/totalwork) * 100%
DE v$ session_longops
OÙ sofar/totalwork < 1
/
I tried
Select S.USERNAME, s.sid, s.osuser, t.sql_id, sql_text, to_char (sl.start_time,'hh24:mi:ss') stime,
- SL.message, (sl.sofar/sl.totalwork)* 100%
de v$ sqltext_with_newlines t, V$ SESSION s, v$ session_longops sl
where s.sid = sl.sid
and where t.Address = s.sql_address
et t.hash_value = s.sql_hash_value
et s.status = 'ACTIVE'
et s.UserName <> 'SYSTEM'
et OÙ sl.sofar/sl.totalwork < 1
order by s.sid, t.piece
but it gives me error.
SQL > select S.USERNAME, s.sid, s.osuser, t.sql_id, sql_text, to_char(sl.start_time,'hh24:mi:ss') stime,.
2 sl.message, (sl.sofar/sl.totalwork)* 100%
3 v $ sqltext_with_newlines t, V$ SESSION s, v$ session_longops sl
4 where s.sid = sl.sid
5 and where t.address = s.sql_address
and t.hash_value = s.sql_hash_value
6 and 7 s.status = 'ACTIVE '.
8 and s.username <>'SYSTEM '.
9 and WHERE the sl.sofar/sl.totalwork < 1
10 decree by s.sid, t.piece
11.
and where t.address = s.sql_address
*
ERROR on line 5:
ORA-00936: lack of expression
any body can help out?
Not tested, but you can try:
SELECT s.sid,
s.Serial #.
s.machine,
Round (SL.elapsed_seconds/60). ':' || MOD(SL.elapsed_seconds,60) has elapsed,
Round (SL.time_remaining/60). ':' || MOD(SL.time_remaining,60) remaining.
ROUND (sl.sofar/sl.totalwork*100, 2) progress_pct.
t.sql_id,
t.sql_text
V $ session s,.
v$ session_longops sl.
v$ sqltext_with_newlines t
WHERE s.sid = sl.sid
AND s.serial # sl.serial = #.
and t.address = s.sql_address
and t.hash_value = s.sql_hash_value
and s.status = 'ACTIVE '.
and s.username <> 'SYSTEM '.
order of s.sid, t.piece;
Or see if you can find a useful script here:
-
Merge 2 SQL statements.
I want to merge these 2 instructions 1 sql, only different between 1 and 2 is a where clause.
+ 1. +
Select count (*) aa
from table_x
where id = "1";
+ 2. +
Select count (*) bb
from table_x;
Right now, I have merged this way.
with the temp as)
Select count (*) bb
from table_x
+)+
Select count (*) aa, temp.bb
from table_x, temp
where id = "1"
Kindly help with filing your suggestion if it a better way to merge.
given that the above statement is rather slow.
Thank you!!
JessHello
a possible solution:
SELECT SUM (CASE id WHEN '1' THEN 1 END) aa , COUNT (*) bb FROM table_x;
That is to say:
WITH table_x AS ( SELECT '1' id FROM DUAL UNION ALL SELECT '2' id FROM DUAL ) SELECT SUM (CASE id WHEN '1' THEN 1 END) aa , COUNT (*) bb FROM table_x; AA BB ---------- ---------- 1 2 1 row selected.
When you put a code or output it join between two lines starting with {noformat}
{noformat} i.e.: {noformat}
{noformat}
SELECT...
{noformat}{noformat} Regards. Al Edited by: Alberto Faenza on Apr 8, 2013 4:51 PM
-
Merge no SQL using analytical functions
Hi, the Sql tuning specialists:
I have a question about the merger of view inline.
I have a simple vision with the analytical functions inside. When questioning him, he does not index.
VIEW to CREATE or REPLACE ttt
AS
SELECT EmpNo, deptno,
ROW_NUMBER() over (PARTITION BY deptno ORDER BY deptno desc NULLS last) part_seq
EMP AAA
-That will do full table for emp scan
Select * from TT
WHERE empno = 7369
-If I do not view use, I use the query directly, the index is used
SELECT EmpNo, deptno,
ROW_NUMBER() over (PARTITION BY deptno ORDER BY deptno desc NULLS last) part_seq
EMP aaa
WHERE empno = 7369
question is: How can I force the first query to use indexes?
Thank youMScallion wrote:
What happens if you use the push_pred flag:Nothing will happen. And it would be a bug if he would.
select * from ttt WHERE empno=7369
and
SELECT empno,deptno, row_number() OVER (PARTITION BY deptno ORDER BY deptno desc NULLS last) part_seq FROM emp aaa WHERE empno=7369
are two logically different queries. Analytical functions are applied after + * resultset is common. So first select query all rows in the emp table then assign ROW_NUMBER() to recovered lines and only then select a line with empno = 7369 her. Second query will select the table emp with empno = 7369 line and only then apply ROW_NUMBER() - so since emp.empno is unique ROW_NUMBER returned by second query will always be equal to 1:
SQL> select * from ttt 2 WHERE empno=7369 3 / EMPNO DEPTNO PART_SEQ ---------- ---------- ---------- 7369 20 4 SQL> SELECT empno,deptno, 2 row_number() OVER (PARTITION BY deptno ORDER BY deptno desc NULLS last) part_seq 3 FROM emp aaa 4 WHERE empno=7369 5 / EMPNO DEPTNO PART_SEQ ---------- ---------- ---------- 7369 20 1 SQL>
SY.
-
Hi gurus,
I have two SQLs that are;
Select bbe
of alu_sdhnm.dwdm_logical_stats dis_1
where bbe < = (select max (bbe) in alu_sdhnm.dwdm_logical_stats ic_1 where ic_1.datetime = dis_1.datetime)
order by bbe desc;
Select are
of alu_sdhnm.dwdm_logical_stats dis_2
where are < = (select max (es) in alu_sdhnm.dwdm_logical_stats ic_2 where ic_2.datetime = dis_2.datetime)
order by are desc;
Those who demonstrate on top and bbe values within dwdm_logical_stats table separately by using a correlated subquery. My question is, how can I see them within a single query? In addition, in the same line if bbe has the highest value, is not the highest.
Thank you
OganI do not forget the small details of the data ;)
All of them give you?
SELECT bbe, DENSE_RANK() OVER (ORDER BY BBE DESC) bbe_rank, ROW_NUMBER() OVER (ORDER BY BBE DESC) row_num FROM alu_sdhnm.dwdm_logical_stats;
And:
SELECT es, DENSE_RANK() OVER (ORDER BY ES DESC) es_rank, ROW_NUMBER() OVER (ORDER BY ES DESC) row_num FROM alu_sdhnm.dwdm_logical_stats)
And finally:
SELECT bbe, bbe_rank, es, es_rank FROM ( SELECT bbe, DENSE_RANK() OVER (ORDER BY BBE DESC) bbe_rank, ROW_NUMBER() OVER (ORDER BY BBE DESC) row_num FROM alu_sdhnm.dwdm_logical_stats) ilv1 JOIN ( SELECT es, DENSE_RANK() OVER (ORDER BY ES DESC) es_rank, ROW_NUMBER() OVER (ORDER BY ES DESC) row_num FROM alu_sdhnm.dwdm_logical_stats) ilv2 ON (ilv1.row_num = ilv2.row_num)
-
Rows in the table LT until merger of all workspaces
Database 11.2.0.4
In our environment, we ran a few important updates in the workspaces and then merged these live workspaces. Up to 25% of the data has been updated in those workspaces. When these update batch workspaces have been created, previous workspaces exist who are not yet ready for the merger.
I noticed that the underlying tables of the LT now have about 25% more lines than the live view. We have now discovered that dbms_wm.refreshWorkspace works really poor. This statement is used to take seconds or minutes to the maximum, but can now take half an hour or more.
We recorded a support with Oracle request and provided details of the plan to see if we can get some resolution/advice explaining.
In the meantime, I have a question for the forum help my understanding of the works of the Manager of the workspace.
In the example below, I have table "wm_test_table", "test_workspace1" and "test_workspace2". In "test_workspace1", I update all rows in the table - the table LT now has 2 rows for each record. I merge 'test_workspace1' live and LT still has 2 rows for each record. The other lines are not removed until "test_workspace2" is merged and removed.
SQL> -- Create a test table SQL> create table wm_test_table ( 2 id integer primary key, 3 name varchar2(100)); Table created. SQL> -- Insert 1,000 rows SQL> insert into wm_test_table ( 2 select rownum, 'before' 3 from dual 4 connect by level <= 1000); 1000 rows created. SQL> commit; Commit complete. SQL> -- Version enable it SQL> execute dbms_wm.enableVersioning('wm_test_table'); PL/SQL procedure successfully completed. SQL> -- Create two workspaces SQL> execute dbms_wm.createWorkspace('test_workspace1'); PL/SQL procedure successfully completed. SQL> execute dbms_wm.createWorkspace('test_workspace2'); PL/SQL procedure successfully completed. SQL> -- Go to test_workspace1 and update all the rows SQL> execute dbms_wm.goToWorkspace('test_workspace1'); PL/SQL procedure successfully completed. SQL> update wm_test_table set name = 'after'; 1000 rows updated. SQL> commit; Commit complete. SQL> -- Check how many rows in wm_test_table_lt - we expect 2,000 as we haven't merged yet SQL> select count(*) from wm_test_table_lt; COUNT(*) ---------- 2000 SQL> -- Merge and remove test_workspace1 SQL> execute dbms_wm.goToWorkspace('LIVE'); PL/SQL procedure successfully completed. SQL> execute dbms_wm.mergeWorkspace('test_workspace1', remove_workspace=>true); PL/SQL procedure successfully completed. SQL> -- Check how many rows in wm_test_table_lt SQL> -- I'd hope there'd only be 1,000 but there is 2,000 SQL> select count(*) from wm_test_table_lt; COUNT(*) ---------- 2000 SQL> -- Let's see if compressWorkspace will get rid of them SQL> execute dbms_wm.compressWorkspace( - > workspace => 'LIVE', - > auto_commit => true, - > remove_latest_deleted_rows => true); PL/SQL procedure successfully completed. SQL> -- They're still there SQL> select count(*) from wm_test_table_lt; COUNT(*) ---------- 2000 SQL> -- Now merge test_workspace2 SQL> execute dbms_wm.mergeWorkspace('test_workspace2', remove_workspace=>true); PL/SQL procedure successfully completed. SQL> -- and the additional 1,000 rows are removed SQL> select count(*) from wm_test_table_lt; COUNT(*) ---------- 1000
Is it possible to safely remove the extra lines in LT without waiting until we merged the workspaces that existed before the update batch has been done? I hope that it will so improve the performance of refreshWorkspace.
I thought that something like this can do, but it does not remove the other ranks, is:
execute dbms_wm.PurgeTable( table_id => 'test.wm_test_table', archive_table => null, where_clause => 'id not in (select id from test.wm_test_table)', instant=>null, workspace => 'LIVE', purgeAfter => false);
This question is similar to
Lines left in the table LT after removal of the direct workspace and the removal of all workspaces
and
but I'm still not sure.
Thank you
John
Hello
This behavior is expected and it is due to the test_workspace2 workspace. A compression operation will not allow compressed range to include a backup implicit point that is created for a child workspace. This is done to preserve the integrity of this workspace. The initial 1000 lines are all lines that are visible from test_workspace2. Removal of these lines would leave test_workspace2 will 0 lines in there. Lines 1000 updates are the lines that do not appear on LIVE. So, given that the two sets of lines are needed, none of the lines can be deleted physically. A compression operation will never change the view of data for any workspace. It only removes the unnecessary records. The two options in order to be able to compress these lines is to either delete (as you did) or refresh test_workspace2. The refresh operation would move the implicit savepoint for the workspace to a version that isn't after all 2000 lines in the table _LT and allow the dressing to remove first half of them because they are no longer needed by any child workspace.
With respect to the performance of RefreshWorkspace, I would need to see a trace of the transaction file. But who should rather get through the SR you filed.
Kind regards
Ben
-
Hi all
Can I combine the records in the nested table
I have a table with 2 colum: col_1 and col_2
col_1: corresponds to the id (varchar2)
col_2: is a column nested with the type of table (sub_col_1 (number), sub_col_2 (number))
for example: I have 2 accounts table ('a', (1,2)) and ('b', (3,4)))
what I want is merged into this table with values ('a', '4.5')) + ('c', (6,7)) IN a single statement
What I should have after this operation ('a', (1,2,4,5)) + ('b', (3,4)) + ('c', (6,7)).
Can I do this?
Oracle version: 11.2.0.3CREATE OR REPLACE TYPE TEST_TYPE AS OBJECT(SUB_COL_1 NUMBER, SUB_COL_2 NUMBER) ; CREATE OR REPLACE TYPE TEST_TYPE_TABLE IS TABLE OF TEST_TYPE; CREATE TABLE TEST_MERGE_NESTED_TABLE (COL_1 VARCHAR2(1 CHAR), COL_2 TEST_TYPE_TABLE) NESTED TABLE COL_2 STORE AS COL_2_NESTED; INSERT INTO TEST_MERGE_NESTED_TABLE VALUES('a',TEST_TYPE_TABLE(TEST_TYPE(1,2) )); INSERT INTO TEST_MERGE_NESTED_TABLE VALUES('b',TEST_TYPE_TABLE(TEST_TYPE(3,4) ));
Thank you all.
Published by: 966205 on 20:18 21/02/2013
Published by: 966205 on 20:42 21/02/2013966205 wrote:
That means he does the same thing as what I want (adding? do not recreate this column in the physical layer).Actually, no.
All content of the nested table is first removed (for the given FK), then the result of the MULTISET UNION is reinserted.The evidence on:
SQL> alter session set events '10046 trace name context forever, level 12'; Session altered. SQL> merge into test_merge_nested_table t 2 using ( 3 select 'a' col_1, TEST_TYPE_TABLE(TEST_TYPE(4,5)) col_2 from dual union all 4 select 'c' , TEST_TYPE_TABLE(TEST_TYPE(6,7)) from dual 5 ) v 6 on ( t.col_1 = v.col_1 ) 7 when matched then update 8 set t.col_2 = t.col_2 multiset union v.col_2 9 when not matched then insert (col_1, col_2) 10 values (v.col_1, v.col_2) ; 2 rows merged. SQL> alter session set events '10046 trace name context off'; Session altered. SQL> select * from test_merge_nested_table; C COL_2(SUB_COL_1, SUB_COL_2) - -------------------------------------------------------------------------------- c TEST_TYPE_TABLE(TEST_TYPE(6, 7)) a TEST_TYPE_TABLE(TEST_TYPE(1, 2), TEST_TYPE(4, 5)) b TEST_TYPE_TABLE(TEST_TYPE(3, 4))
TKPROF output:
DELETE, the nested table statement objectives associated with 'a' and deletes the line after line that it contains (1,2).
The later INSERTION is performed twice (run count = 2) and target the two nested table 'a' by inserting 2 ranks: former one (1,2) + the new one (4.5) and the nested table 'c' by inserting 1 row (6,7).SQL ID: 6bjc2z2t53csn Plan Hash: 132214516 DELETE /*+ REF_CASCADE_CURSOR */ FROM "DEV"."COL_2_NESTED" WHERE "NESTED_TABLE_ID" = :1 call count cpu elapsed disk query current rows ------- ------ -------- ---------- ---------- ---------- ---------- ---------- Parse 1 0.00 0.00 0 0 0 0 Execute 1 0.00 0.00 0 1 3 1 Fetch 0 0.00 0.00 0 0 0 0 ------- ------ -------- ---------- ---------- ---------- ---------- ---------- total 2 0.00 0.00 0 1 3 1 Misses in library cache during parse: 0 Optimizer mode: CHOOSE Parsing user id: SYS (recursive depth: 1) Number of plan statistics captured: 1 Rows (1st) Rows (avg) Rows (max) Row Source Operation ---------- ---------- ---------- --------------------------------------------------- 0 0 0 DELETE COL_2_NESTED (cr=1 pr=0 pw=0 time=44 us) 1 1 1 INDEX RANGE SCAN SYS_FK0000023444N00002$ (cr=1 pr=0 pw=0 time=10 us)(object id 23446) ********************************************************************************
******************************************************************************** SQL ID: 0fzd5yk23jyas Plan Hash: 0 INSERT /*+ NO_PARTIAL_COMMIT REF_CASCADE_CURSOR */ INTO "DEV"."COL_2_NESTED" ("NESTED_TABLE_ID","SYS_NC_ROWINFO$") VALUES (:1, :2) call count cpu elapsed disk query current rows ------- ------ -------- ---------- ---------- ---------- ---------- ---------- Parse 2 0.00 0.00 0 0 0 0 Execute 2 0.00 0.00 0 2 10 3 Fetch 0 0.00 0.00 0 0 0 0 ------- ------ -------- ---------- ---------- ---------- ---------- ---------- total 4 0.00 0.00 0 2 10 3 Misses in library cache during parse: 0 Optimizer mode: ALL_ROWS Parsing user id: 50 (recursive depth: 1) Number of plan statistics captured: 1 Rows (1st) Rows (avg) Rows (max) Row Source Operation ---------- ---------- ---------- --------------------------------------------------- 0 0 0 LOAD TABLE CONVENTIONAL (cr=1 pr=0 pw=0 time=113 us) ******************************************************************************** -
MERGE operator. Logic of work.
Dear Oracle experts,
I noticed that in some examples logical MERGER work differs from the standard against which tool you use.
We will check following example in TOAD, sqlplus and you will see that the priority of deletion in the MERGER is higher than the priority of the update.
create the t_72 table (id1 number, varchar2 (10) Text1, num1);
insert into t_72 values (1, 'ok', 105);
insert into t_72 values (2, 'nok', 106);
insert into t_72 values (3, 'nok', 107);
create table t_73 (id2 number, Text2 varchar2 (10), num2);
insert into t_73 values (1, 'nok', 110);
insert into t_73 values (2, 'ok', 111);
insert into t_73 values (4, 'w', 112);
commit;
merge into t_72
using t_73
on (id1 = id2)
when matched, then update set text1 Text2 =
Delete where text1 = "nok".
When not matched then insert values (id2, text2, num2);
Select * from t_72;
What do you see? Line with id1 has not been deleted, but was stored with id2.
Now please check this one in PL/SQL - it will work correctly.
Could not explain why I'm dealing with 2 other MERGE logic?The correct behavior is that the deletion should be applied after the update has been applied.
In the example, id1 should be deleted because 'ok' should have been upgrade "nok".
Considering that id2 should not be deleted as "nok" should have been updated for 'ok '.
(And id3 should be left alone and inserted id4)
This is obviously a bug as it was discussed in 11.2.0.3, but having had a look in the Support of Oracle online, it is not obvious that he is one of the most exposed. However, as do a search on "bad results" and "outer join" or "fusion" has produced a good number of results...
In 11.2.0.2:
SQL> select * from v$version; BANNER -------------------------------------------------------------------------------- Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production PL/SQL Release 11.2.0.2.0 - Production CORE 11.2.0.2.0 Production TNS for Linux: Version 11.2.0.2.0 - Production NLSRTL Version 11.2.0.2.0 - Production SQL> select * from t_72; ID1 TEXT1 NUM1 ---------- ---------- ---------- 1 ok 105 2 nok 106 3 nok 107 SQL> @merge 3 rows merged. SQL> select * from table(dbms_xplan.display_cursor); PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------------------------------------ SQL_ID 4jjmpqv9muw31, child number 0 ------------------------------------- merge into t_72 using t_73 on (id1=id2) when matched then update set text1=text2 delete where text1='nok' when not matched then insert values (id2,text2,num2) Plan hash value: 50174811 ------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------ | 0 | MERGE STATEMENT | | | | 12 (100)| | | 1 | MERGE | T_72 | | | | | | 2 | VIEW | | | | | | | 3 | NESTED LOOPS OUTER | | 3 | 234 | 12 (0)| 00:00:01 | | 4 | TABLE ACCESS FULL | T_73 | 3 | 99 | 3 (0)| 00:00:01 | | 5 | VIEW | | 1 | 45 | 3 (0)| 00:00:01 | |* 6 | TABLE ACCESS FULL| T_72 | 1 | 45 | 3 (0)| 00:00:01 | ------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 6 - filter("ID1"="ID2") SQL> select * from t_72; ID1 TEXT1 NUM1 ---------- ---------- ---------- 1 nok 105 3 nok 107 4 w 112 SQL>
Whereas a merger without deleting seems to do the right stuff:
SQL> select * from t_72; ID1 TEXT1 NUM1 ---------- ---------- ---------- 1 ok 105 2 nok 106 3 nok 107 SQL> @merge_nodel 3 rows merged. SQL> select * from table(dbms_xplan.display_cursor); PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------------------------------------ SQL_ID 4x0ry1a5yg555, child number 0 ------------------------------------- merge into t_72 using t_73 on (id1=id2) when matched then update set text1=text2 when not matched then insert values (id2,text2,num2) Plan hash value: 50174811 ------------------------------------------------------------------------------ | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------ | 0 | MERGE STATEMENT | | | | 12 (100)| | | 1 | MERGE | T_72 | | | | | | 2 | VIEW | | | | | | | 3 | NESTED LOOPS OUTER | | 3 | 234 | 12 (0)| 00:00:01 | | 4 | TABLE ACCESS FULL | T_73 | 3 | 99 | 3 (0)| 00:00:01 | | 5 | VIEW | | 1 | 45 | 3 (0)| 00:00:01 | |* 6 | TABLE ACCESS FULL| T_72 | 1 | 45 | 3 (0)| 00:00:01 | ------------------------------------------------------------------------------ Predicate Information (identified by operation id): --------------------------------------------------- 6 - filter("ID1"="ID2") SQL> select * from t_72; ID1 TEXT1 NUM1 ---------- ---------- ---------- 1 nok 105 2 ok 106 3 nok 107 4 w 112 SQL>
The merger, without deleting has the same execution plan, but there are a number of bugs around FUSION and that can be merged if VIEWS this plan faced with views that may be non-merged, we are in a ballpark with known problems/bugs "bad results".
But base on these correct update of the results, I would expect the delete clause to remove line 1 NOT row 2.
Which is what you see in 11.2.0.3, as others have noticed
SQL> select * from t_72; ID1 TEXT1 NUM1 ---------- ---------- ---------- 1 ok 105 2 nok 106 3 nok 107 SQL> @merge 3 rows merged. SQL> select * from table(dbms_xplan.display_cursor); PLAN_TABLE_OUTPUT ------------------------------------------------------------------------------------------------------------------------ SQL_ID 4jjmpqv9muw31, child number 0 ------------------------------------- merge into t_72 using t_73 on (id1=id2) when matched then update set text1=text2 delete where text1='nok' when not matched then insert values (id2,text2,num2) Plan hash value: 659680654 ----------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ----------------------------------------------------------------------------- | 0 | MERGE STATEMENT | | | | 7 (100)| | | 1 | MERGE | T_72 | | | | | | 2 | VIEW | | | | | | |* 3 | HASH JOIN OUTER | | 3 | 234 | 7 (15)| 00:00:01 | | 4 | TABLE ACCESS FULL| T_73 | 3 | 99 | 3 (0)| 00:00:01 | | 5 | TABLE ACCESS FULL| T_72 | 3 | 135 | 3 (0)| 00:00:01 | ----------------------------------------------------------------------------- Predicate Information (identified by operation id): --------------------------------------------------- 3 - access("ID1"="ID2") SQL> select * from t_72; ID1 TEXT1 NUM1 ---------- ---------- ---------- 2 ok 106 3 nok 107 4 w 112 SQL>
The execution plan change is perhaps significant? Perhaps.
Now please check this one in PL/SQL - it will work correctly.
I tried this in the 11.2.0.2 environment. Same behavior as SQL.
If you get a difference however, investigate all the differences in the execution plan, etc.But, "bad results" bugs - far too many of them.
-
Person registering in the Merge statement error!
Is it possible to save indivudual error in the MERGE statement (Update / Insert).
I'm unable to save these error. instead of MERGING, if I update the table in the cursor loop then I am able to record the individual error but the process takes time.
Thanks in advance.
DebaHi Deba,
Logging of DML errors:
SQL> create table tab1 (x number(1)); Table created. SQL> exec dbms_errlog.create_error_log('tab1') PL/SQL procedure successfully completed. SQL> SQL> merge into tab1 t 2 using (select 1 x from dual union all 3 select 112 x from dual) s 4 on (t.x = s.x) 5 when not matched 6 then insert (x) values (s.x) 7 log errors into err$_tab1 reject limit unlimited; 1 row merged. SQL> SQL> COL x for 9999 SQL> select * from tab1; X ----- 1 SQL> COL x for a4 SQL> select ora_err_number$, X from err$_tab1; ORA_ERR_NUMBER$ X --------------- ---- 1438 112 SQL>
Concerning
Peter -
Merge the question of the declaration
Hello
I have a situation:
Table == > EM
SQL > desc em
Structure
---------------
Name Null? Type
----------------------------------------------------------------- -------- --------------------------------------------
EMPNO NUMBER 4
ENAME VARCHAR2 (10)
USE VARCHAR2 (12)
MGR NUMBER 4
HIREDATE DATE
SAL NUMBER (7.2)
NUMBER (7.2) COMM
DEPTNO NUMBER (2)
DNAME VARCHAR2 (10)
Reviews
------------
SQL > select * from em.
EMPNO, ENAME, JOB HIREDATE DEPTNO DNAME COMM SAL MGR
---------- ---------- ------------ ---------- --------- ---------- ---------- ---------- ----------
Onkar 1211 s/n T 7839 8099 21 April 08 0 10 ACCOUNTING
Jr. Ananth s/n T 1211 1212 3 - Sep - 09 8099 0 10 ACCOUNTING
9999 KING CLERKS 1211 1000 23 March 10 100 10 ACCOUNTING
9977 7499 24 March test 10 1000 100 40 OPERATIONS
7369 SMITH T CLERK T 7902 17 December 80 999 400 20 RESEARCH
ALLEN R SALESMAN T 7698 1899 February 20 7499 81 300 30 SALES
7521 WARD R SALESMAN T 7698 1549 22 February 81 500 30 SALES
7566 JONES MANAGER T 7839 2 April 81 3174 1487.5 SEARCH 20
7654 MARTIN R SALESMAN 7698 28 - Sep - 81 1549 1400 30 SALES T
7698 BLAKE R MANAGER 7839 3149 1 may T 81 1425 30 SALES
7782 CLARK MANAGER T 7839 2549 9 June 81 1225 10 ACCOUNTING
ANALYST SCOTT 7788 T 7566 3199 April 19 87 1500 RESEARCH 20
7839 KING PRESIDENT T November 17 81 5099 2500 10 ACCOUNTING
7844 TURNER R T of 7698 8 - Sep - 81 1799 0 30 SALES SALESMAN
7876 ADAMS CLERK T 7788 1299 23 May 87 550 20 RESEARCH
JAMES R CLERK T 7698 1249 3 December 7900 81 475 30 SALES
7902 FORD T ANALYST 7566 3199 3 December 81 1500 RESEARCH 20
7934 MILLER CLERK T 7782 1399 23 January 82 650 10 ACCOUNTING
12 asdf asdf 7934 1000 13 July 10 90
Table == > DEPT
SQL > desc dept
Name Null? Type
----------------------------------------------------------------- -------- --------------------------------------------
DEPTNO NOT NULL NUMBER (2)
DNAME VARCHAR2 (14)
LOC VARCHAR2 (13)
record
---------
SQL > select * from the Department;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTS NEW YORK
SEARCH 20 DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Here's the MERGE statement:
SQL > ed
A written file afiedt.buf
1 merger in e em
2 using d dept
3 on(e.deptno=d.deptno)
4 when matched then
Update 5 set e.dname = d.dname
6 when not matched then
7 * Insert (e.dname) values ('test')
8.
18 lines merged.
The requirement is if a record is relevant then col DNAME update EM table regarding the DEPT table but if there is no matching record in the EM table then update unmatched record by 'TEST '. The command is successful but deptno = 90 in EM is not updated.
All of the suggestions.Right button correct Please push or useful.
Here's the merge statement.
However, I recommend to use the update statement to Fred.merge into em e using (select a.Rowid rid,b.dname from em a,dept b where a.deptno=b.deptno(+)) d on e.rowid=d.rid when matched then update set e.dname =nvl(d.dname,'test');
-
Merge stmt tries to insert two copies while the clause is based on the index
Hello
I use a merge statement to insert the record in a table. The table has a unique index based on four columns. The merge statement matches on all four columns (a.column1 = b.column1 and a.column2 = b.column2... etc) in clause ONE. If a match is found then it does nothing if it is not found corresponding record is inserted.
But for some reason, it attempts to insert duplicate and I'm getting exception violated a unique constraint.
The database is huge and the source table contains more than 200,000 records.
No idea how this can happen?How can it happen?
Easily if there is a duplicate in the data you want to merge, keeping in mind that this is an operation based on a game with the two sets of data that are evaluated using the standard model consistency to the beginning of the query.
Connected to: Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options SQL> DROP TABLE t1; Table dropped. SQL> SQL> CREATE TABLE t1 2 (col1 NUMBER PRIMARY KEY); Table created. SQL> SQL> MERGE 2 INTO t1 3 USING (SELECT 1 x 4 FROM DUAL 5 UNION ALL 6 SELECT 1 7 FROM DUAL) x 8 ON (x.x = t1.col1) 9 WHEN NOT MATCHED THEN 10 INSERT 11 VALUES 12 ( x.x); MERGE * ERROR at line 1: ORA-00001: unique constraint (ROS.SYS_C0088944) violated SQL> rollback; Rollback complete. SQL> MERGE 2 INTO t1 3 USING (SELECT DISTINCT x 4 FROM ( 5 SELECT 1 x 6 FROM DUAL 7 UNION ALL 8 SELECT 1 9 FROM DUAL)) x 10 ON (x.x = t1.col1) 11 WHEN NOT MATCHED THEN 12 INSERT 13 VALUES 14 ( x.x); 1 row merged. SQL>
Published by: DomBrooks on January 15, 2010 09:36
-
I am doing the following things:
(1) to get the values for db to HTML form (for the end-user through my java code) and make a query to select already existing values in the db for a specific user to compare the newly received values
(3) if exists then do not insert. If not exists then insert
some time before an Expert favoured the insertion of the unique values and provided the solution as Oracle FUSION application but it throw errors.
Thank you in anticipationSQL> desc expertise Name Null? Type ----------------------------------------- -------- ---------------------------- EX_ID NOT NULL NUMBER(14) EMP_ID NUMBER(14) EXPERTISE NUMBER(2) SQL> select * from expertise; EX_ID EMP_ID EXPERTISE ---------- ---------- ---------- 249 1 1 252 1 3 251 1 2 261 1 4 SQL> ed Wrote file afiedt.buf 1 merge 2 into expertise t1 3 using (select 5 expertise,1 emp_id from expertise) t2 4 on (t1.emp_id = t2.emp_id and t1.expertise = t2.expertise) 5* when not matched then insert (expertise,emp_id) values(t2.expertise,t2.emp_id) SQL> / 0 rows merged. SQL> ed Wrote file afiedt.buf 1 merge 2 into expertise t1 3 using (select 5 expertise,1 emp_id from expertise) t2 4 on (t1.emp_id = t2.emp_id and t1.expertise = t2.expertise) 5* when not matched then insert (expertise,emp_id) values(t2.expertise,t2.emp_id) SQL> / 4 rows merged. SQL> select * from expertise; EX_ID EMP_ID EXPERTISE ---------- ---------- ---------- 249 1 1 252 1 3 251 1 2 262 1 5 261 1 4 263 1 5 264 1 5 265 1 5 8 rows selected. SQL> ed Wrote file afiedt.buf 1 merge 2 into expertise t1 3 using (select 5 expertise,1 emp_id from expertise) t2 4 on (t1.emp_id = t2.emp_id and t1.expertise = t2.expertise) 5* when not matched then insert (expertise,emp_id) values(t2.expertise,t2.emp_id) SQL> / 0 rows merged. SQL> ed Wrote file afiedt.buf 1 merge 2 into expertise t1 3 using (select 6 expertise,1 emp_id from expertise) t2 4 on (t1.emp_id = t2.emp_id and t1.expertise = t2.expertise) 5* when not matched then insert (expertise,emp_id) values(t2.expertise,t2.emp_id) SQL> / 8 rows merged. SQL> ed Wrote file afiedt.buf 1 merge 2 into expertise t1 3 using (select 7 expertise,1 emp_id from expertise) t2 4 on (t1.emp_id = t2.emp_id and t1.expertise = t2.expertise) 5* when not matched then insert (expertise,emp_id) values(t2.expertise,t2.emp_id) SQL> / 16 rows merged.
Raakh wrote:
I am doing the following things:Ponder the difference between my solution
Fusion
in expertise t1
using (expertise to select 5, 1 doubleemp_id) t2
on (t1.emp_id = t2.emp_id and t1.expertise = t2.expertise)
When not matched then insert (expertise, emp_id) values (t2.expertise, t2.emp_id)and your code:
Fusion
in expertise t1
using (expertise to select 5, 1 expertiseemp_id) t2
on (t1.emp_id = t2.emp_id and t1.expertise = t2.expertise)
When not matched then insert (expertise, emp_id) values (t2.expertise, t2.emp_id)SY.
-
Problem with MERGE in Oracle 9i R2
CREATE TABLE SUBRONOW_LOAD_LOG
(
LOG_ID NUMBER (10) NOT NULL,
PROCESSED_FLG VARCHAR2 (1 BYTE) NOT NULL,
DATE OF SN_LOAD_START_DATE,
DATE OF SN_LOAD_END_DATE,
DATE OF ST_LOAD_START_DATE,
DATE OF ST_LOAD_END_DATE,
STATUS NVARCHAR2 (20).
DESCRIPTION VARCHAR2 (30 BYTE)
);
MERGE INTO subronow_load_log JOURNAL
With the HELP of DOUBLE d
WE (log_id & LOG.log_id = AND & e_count > 0)
WHEN MATCHED THEN
UPDATE
SET LOG.processed_flg = 'P '.
WHEN NOT MATCHED THEN
INSERT (LOG.log_id, LOG.processed_flg, LOG.status)
VALUES (subronow_load_log_id_seq. NEXTVAL, 'P', 'IN PROGRESS');
SQL > MERGE IN subronow_load_log JOURNAL
2 with the HELP of DOUBLE d
3. WE (log_id & LOG.log_id = AND & e_count > 0)
4. WHEN MATCHED THEN
UPDATE 5
6 SET LOG.processed_flg = 'P '.
7 WHEN NOT MATCHED THEN
8 INSERT (LOG.log_id, LOG.processed_flg, LOG.status)
9 VALUES (subronow_load_log_id_seq. NEXTVAL, 'P', 'IN PROGRESS');
Enter the value for log_id: 85
Enter the value of e_count: 9
old 3: WE (log_id & LOG.log_id = AND & e_count > 0)
new 3: WE (LOG.log_id = 85 AND 9 > 0)
0 lines merged.
SQL > /.
Enter the value for log_id: 27
Enter the value of e_count: 5
old 3: WE (log_id & LOG.log_id = AND & e_count > 0)
new 3: WE (LOG.log_id = 27 AND 5 > 0)
1 row merged.
Update the above statement to merge part works fine.
but the INSERT statement doesnot work.
Can you please help me in this regard?With the HELP of DOUBLE d
WE (log_id & LOG.log_id = AND & e_count > 0)It should be:
USING (select &log_id as log_id, &e_count as e_count from DUAL) d ON (LOG.log_id = d.log_id AND d.e_count > 0)
Nicolas.
-
How to insert only new registrations in GR 11, 1 material
Hi friends,
Every hour, I have a few records in a table that has a unique primary key.
I want to insert only new records up to and ignore duplicate keys (meaning it shouldn't fail with "forced to uniqueness (TEST.) TEST_PK) violated" . Is there an easy way to do it? (I read that we can use MERGE, but did not understand how to rewrite my request accordingly)
Note: DB is on 11 GR 1 material so I can't use IGNORE_ROW_ON_DUPKEY_INDEX (introduced in 11 GR 2).
Query : for example hard-coded Date
Insert / * + append parallel(TEST12,8) * / into TEST TEST12
SELECT
/*+
Leading (a.c)
Full (a.c) parallel (a.c, 2)
parallel of full (a.m.) (a.m., 2)
Full (a.d) parallel (a.d, 2)
Full (a.n) parallel (a.n, 2)
PQ_DISTRIBUTE (hash of hash a.n)
*/ *
OF DATA_VIEW one
WHERE the st_date BETWEEN TO_DATE (16 March 14 00:00:00 ',' DD-MON-RR HH24:MI:SS')
AND TO_DATE (16 MARCH 14 23:59 ',' DD-MON-RR HH24:MI:SS');
What did I try?
DBMS_ERRLOG.create_error_log, but still it gives the same error.
The ERRORS IN err LOG $ _dest2 ('1 comment') REJECTS LIMIT UNLIMITED;
Insert / * + append parallel(TEST12,8) * / into TEST TEST12
*
ERROR on line 1:
ORA-12801: error reported in the parallel query P005 Server
ORA-00001: unique constraint (TEST.) TEST_PK) violated
Can you please suggest what is the effective way to do it?
This should work for you (change column lists to match the column names in your version of these tables):
SQL> create table test12( 2 st_id number not null, 3 st_stuff varchar2(40), 4 st_date date); Table created. SQL> SQL> create table test1( 2 st_id number not null, 3 st_stuff varchar2(40), 4 st_date date); Table created. SQL> SQL> create view data_view as select * from test1; View created. SQL> SQL> begin 2 for i in 1..100 loop 3 insert into test1 4 values(i,'Stuff '||i, sysdate-mod(i,9)); 5 end loop; 6 7 commit; 8 end; 9 / PL/SQL procedure successfully completed. SQL> SQL> merge into test12 t12 2 using (SELECT 3 /*+ 4 leading(a.c) 5 full(a.c) parallel (a.c,2) 6 full(a.m) parallel (a.m,2) 7 full(a.d) parallel (a.d,2) 8 full(a.n) parallel (a.n,2) 9 pq_distribute(a.n hash hash) 10 */ * 11 FROM DATA_VIEW a 12 WHERE st_date BETWEEN TO_DATE('16-MAR-14 00:00:00','DD-MON-RR HH24:MI:SS') 13 AND TO_DATE('16-MAR-14 23:59:00','DD-MON-RR HH24:MI:SS')) dv 14 on (dv.st_id = t12.st_id) 15 when not matched then 16 insert (t12.st_id, t12.st_stuff, t12.st_date) 17 values (dv.st_id, dv.st_stuff, dv.st_date); 12 rows merged. SQL> SQL> select count(*) from test12; COUNT(*) ---------- 12 SQL>SQL> begin 2 for i in 101..200 loop 3 insert into test1 4 values(i,'Stuff '||i, sysdate-mod(i,9)); 5 end loop; 6 7 commit; 8 end; 9 / PL/SQL procedure successfully completed. SQL> SQL> merge into test12 t12 2 using (SELECT 3 /*+ 4 leading(a.c) 5 full(a.c) parallel (a.c,2) 6 full(a.m) parallel (a.m,2) 7 full(a.d) parallel (a.d,2) 8 full(a.n) parallel (a.n,2) 9 pq_distribute(a.n hash hash) 10 */ * 11 FROM DATA_VIEW a 12 WHERE st_date BETWEEN TO_DATE('16-MAR-14 00:00:00','DD-MON-RR HH24:MI:SS') 13 AND TO_DATE('16-MAR-14 23:59:00','DD-MON-RR HH24:MI:SS')) dv 14 on (dv.st_id = t12.st_id) 15 when not matched then 16 insert (t12.st_id, t12.st_stuff, t12.st_date) 17 values (dv.st_id, dv.st_stuff, dv.st_date); 11 rows merged. SQL> SQL> select count(*) from test12; COUNT(*) ---------- 23 SQL>
David Fitzjarrell
Maybe you are looking for
-
Find a DVD player that is compatible with my remote
Hello My DVD player is broken, and I would like to make its remote available on E - Bay or other. As usual, the remote control is compatible with several models. How to know the list of these models? The reference on the remote is SE-R0049. Thank you
-
".dll" and problems with my satellite A30 101 Power Saver
Hello world Who can help me, I have two doubts. The first is, I don't always get on three messages when it starts. Will take you to my second problem. The power mangement. It is said 'E-Power Power Management' when I click ok, another message comes t
-
Hello I have a T540p which is around the age of 6 months. I use it with an external monitor. I noticed that the screen backlight is always on. However, if I set the camera on standby and then bring back (always connected to external websites) the scr
-
Write on the worksheet while loop problem
Hello dear Labviewers. I have a 'writing on spreadsheet"with a table 2D DBL connected on it, but the problem occurs inside the while loop. I want exactly those time six values in my excel document, but it restarts after every "two seconds" and I cann
-
I am trying to download an upgrade for UB Funkeys hidden realm and I get an error message. I talked to people from the UB Funkeys and they say I have to let the UB Funkeys pass in and out of my firewall with my Windows Live One Care but I do not unde