MERGE statement without INSERT clause....is possible...?
Hi everybody...
MERGE statement without UPDATE or INSERT clause is possible or not
I want to select from one table and update in another table. So i dont want insert statement and i want to do it in single query....possible solutions are requested.
Thanks in advance
pal
Hi..
Thanks for ur reply. For MERGE statement, we have to give UPDATE and INSERT clause. this MERGE statement works without INSERT or UPDATE clause.
Why i am asking is, I want to select how many rows (count(*)) from table1 and based on this count, i have to update in table2
Both tables r different and for select and for update where clauses are different. Both tables r totally different.
I want to do it in single query, so i asked this is possible with MERGE statement without INSERT clause.
Thanks for ur reply
Similar Messages
-
Syntax for Merge statement to insert into target and update source
Hello All,
I want to use Merge statement to insert records when not matched in my target and update records in source when matched. Is it possible to do using Merge statement.
create table a (aa number)
create table b (bb number)
alter table a add flg char(1)
merge b as target
using a as source
on (target.bb = source.aa)
when matched then
update set source.flg = 'Y'
WHEN NOT MATCHED THEN
insert (target.bb)
values
(source.aa)
Thanks.Hi,
I have no idea about the version of DB, else some new features with respect version can be specified - just for informaitve purpose and to post across the verison of DB in future posts.
Coming to your issue and requirement.
if you check the syntax and functionality , then its on Merge on target base only - with respect to Update (matched o columns) and insert (for unmatched columns). Source - as the term is clear. It might not work out on source table.
- Pavan Kumar N
- ORACLE OCP - 9i/10g
https://www.oracleinternals.blogspot.com -
Merge statement and insert into a third table
I'm wondering if the merge statement will work in my case. I'm loading a file into a temporary table. I want to compare this temporary table with an existing source table (there are multiple source tables, I've thought of breaking them up into separate statements) and write out any differences to a third table. The third table would be used for future processing on the rows that are found to be different. Do you think this is possible using the merge command?
Why use the merge command for that purpose?
If you want to find out the differences of two tables and put the result into a third one then you should do
insert into table_c
query_a
minus
query_b
or
insert into table_c
(query_a
minus
query_b)
union all
(query_b
minus
query_a)
hth -
MERGE w/ conditional Insert clause
Hi--
Sum Up:Is it possible to run Merge syntax with update and a conditional Insert clauses ?
Question Details
Have a base table Emp and staging table New_Emp with a common identifier Emp_id.
The ideal would be to merge the 2 tables and update Emp using New_Emp values and
insert only some new emp, not all records.
Any ideas ? Thks
LamineAbsolutely. See the docs. Use the WHEN and WHERE clauses.
Tom Best -
Merge Mapping Without Update Clause
Hi,
I'm using OWB 9i to generate a mapping to be deployed in to a 10g database. I want to create a merge mapping that performs an insert only, i.e. no update. I'm not able to do this. The validation error I end up with is:
"One of the mapped attributes needs to be set for Update: Use for loading: Yes."
But if I do this, an update clause is appended to my merge.
Can anyone suggest how I can get around this ?Hi,
I am not sure whether OWB9i allows you to exclude either INSERT OR UPDATE, as this functionality is clearly a Oracle 10g feature.
You can certainly work around by updating any audit columns ( like last_updated_date etc) only for UPDATEs.
HTH
Nandoo -
Indivual error recording in Merge Statement !!!!
Is it possible to record indivudual error in MERGE statement (Update / Insert).
I am unable to record those error. instead of MERGE if I update table in the cursor loop then I am able to record individual error but the process is time consuming.
Thanks in advance.
DebaHi Deba,
DML Error Logging:
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>Regards
Peter -
How to use Inner join of table as Source in Merge statement in SQL
Hi All,
I am trying to make source as multiple tables output using Join while coding there is no any syntax error but when i am executing this statement is giving following error
Following is the query
Merge Into EmpDept Target
Using (select E.Address,e.Design,e.EmailId,e.EmpId,e.Ename,e.ManagerId, e.Salary,D.DeptId,D.DeptName,D.Location from Employee E Inner join Dept D on E.DeptId=D.DeptId )As Source (Address,Design,EmailId,EmpId,EName,ManagerId,Salary,DeptId,DeptName,Location)
On Source.EmpId=Target.EmpId
when not matched then
Insert (Target.Address,Target.Design,Target.EmailId,Target.EmpId,Target.Ename,Target.ManagerId, Target.Salary,Target.DeptId,Target.DeptName,Target.Location)
values
(Address,Design,EmailId,EmpId,EName,ManagerId, Salary,DeptId,DeptName,Location)
When matched then
Update set Target.Address = Source.Address ,Target.Design = Source.Design,Target.EmailId = Source.EmailId ,Target.Ename = Source.Ename ,Target.ManagerId = Source.ManagerId , Target.Salary = Source.Salary ,Target.DeptId = Source.DeptId ,Target.DeptName = Source.DeptName ,Target.Location = Source.Location;
This is error while executing the above merge statement
The insert column list used in the MERGE statement cannot contain multi-part identifiers. Use single part identifiers instead.
Please suggest me where i am wrong..
Niraj SevalkarMERGE INTO EmpDept Target
Using (SELECT E.Address,
e.Design,
e.EmailId,
e.EmpId,
e.Ename,
e.ManagerId,
e.Salary,
D.DeptId,
D.DeptName,
D.Location
FROM Employee E
INNER JOIN Dept D
ON E.DeptId = D.DeptId) AS Source (Address, Design, EmailId, EmpId, EName, ManagerId, Salary, DeptId, DeptName, Location)
ON Source.EmpId = Target.EmpId
WHEN NOT matched THEN
INSERT (Address,
Design,
EmailId,
EmpId,
Ename,
ManagerId,
Salary,
DeptId,
DeptName,
Location)
VALUES (Address,
Design,
EmailId,
EmpId,
EName,
ManagerId,
Salary,
DeptId,
DeptName,
Location)
WHEN matched THEN
UPDATE SET Address = Source.Address,
Design = Source.Design,
EmailId = Source.EmailId,
Ename = Source.Ename,
ManagerId = Source.ManagerId,
Salary = Source.Salary,
DeptId = Source.DeptId,
DeptName = Source.DeptName,
Location = Source.Location; -
Hi All,
I am using merge statement to insert and update rows. The merge statement takes a row level exclusive lock
Suppose the select statement returns 1 Million records does
1) The merge statement first tries to lock all the affected rows and then update / insert records.
OR
2) The merge statement aquires lock on an incremental basis as returned by the select statement.The merge works just like an insert or update statement in that locks are acquired "on an incremental basis", but as of the timestamp the merge began. When Oracle detects that another session has updated a row in the meantime, then it re-executes the entire merge statement as of that new timestamp. It is sometimes referred to as "write consistency".
Here is a good Asktom thread on this topic: http://asktom.oracle.com/pls/asktom/f?p=100:11:1694097844551766::::P11_QUESTION_ID:11504247549852
Regards,
Rob. -
Dear all
I am using oracle 9i(9.2.0.1.0). I have a Merge statement which inserts 14million records.
I am able to execute the merge statement in 4mins.
When i try to execute the same merge on oracle (9.2.0.6 ) with same hardware config on client machine, My merge statement hangs.
Please let me know if any patches are there and what all parameters do i need to look into to trace the problem.
Regards
SurendraHi
Do you have the same indexes and statistics in the two systems?
Ott Karesz
http://www.trendo-kft.hu -
Can we use where clause in Update on Merge statement?
Hi All,
I tried to execute the following Merge Query:
When this query is executed without ‘Where clause’ in Update statement its working fine. When executed with ‘Where clause’ it throwing the following error:
ORA-00905: missing keyword.
Following is the sample query which I tried to execute:
MERGE INTO TABLE_NAME
USING (SELECT COLUMN FORM TABLES)
ON (CONDITION)
WHEN MATCHED THEN
UPDATE SET
COLUMN UPATES
WHERE CONDITION -- Can we use where clause here?
WHEN NOT MATCHED THEN
INSERT
INSERT VALUES;
Can some one help on this?
Thanks in advance.
DariusYes:
SQL> drop table emp1;
Table dropped.
SQL> create table emp1 as select * from emp where deptno = 30;
Table created.
SQL> update emp1 set sal = sal*2;
6 rows updated.
SQL> commit;
Commit complete.
SQL> select ename,sal from emp1;
ENAME SAL
ALLEN 3200
WARD 2500
MARTIN 2500
BLAKE 5700
TURNER 3000
JAMES 1900
6 rows selected.
SQL> MERGE INTO emp1
2 USING(select * from emp) emp
3 ON (emp1.empno = emp.empno)
4 WHEN MATCHED THEN
5 UPDATE SET sal = emp.sal WHERE ename = 'TURNER'
6 WHEN NOT MATCHED THEN
7 INSERT(ename,sal) VALUES(emp.ename,emp.sal);
9 rows merged.
SQL> select ename,sal from emp1;
ENAME SAL
ALLEN 3200
WARD 2500
MARTIN 2500
BLAKE 5700
TURNER 1500
JAMES 1900
SMITH 800
JONES 2975
CLARK 2450
SCOTT 3000
KING 5000
ENAME SAL
ADAMS 1100
FORD 3000
MILLER 1300
14 rows selected.
SQL> SY. -
Question on passing string values to Partition clause in a merge statement
Hello All,
I am using the below code to update specific sub-partition data using oracle merge statements.
I am getting the sub-partition name and passing this as a string to the sub-partition clause.
The Merge statement is failing stating that the specified sub-partition does not exist. But the sub-partition do exists for the table.
We are using Oracle 11gr2 database.
Below is the code which I am using to populate the data.
declare
ln_min_batchkey PLS_INTEGER;
ln_max_batchkey PLS_INTEGER;
lv_partition_name VARCHAR2 (32767);
lv_subpartition_name VARCHAR2 (32767);
begin
FOR m1 IN ( SELECT (year_val + 1) AS year_val, year_val AS orig_year_val
FROM ( SELECT DISTINCT
TO_CHAR (batch_create_dt, 'YYYY') year_val
FROM stores_comm_mob_sub_temp
ORDER BY 1)
ORDER BY year_val)
LOOP
lv_partition_name :=
scmsa_handset_mobility_data_build.fn_get_partition_name (
p_table_name => 'STORES_COMM_MOB_SUB_INFO',
p_search_string => m1.year_val);
FOR m2
IN (SELECT DISTINCT
'M' || TO_CHAR (batch_create_dt, 'MM') AS month_val
FROM stores_comm_mob_sub_temp
WHERE TO_CHAR (batch_create_dt, 'YYYY') = m1.orig_year_val)
LOOP
lv_subpartition_name :=
scmsa_handset_mobility_data_build.fn_get_subpartition_name (
p_table_name => 'STORES_COMM_MOB_SUB_INFO',
p_partition_name => lv_partition_name,
p_search_string => m2.month_val);
DBMS_OUTPUT.PUT_LINE('The lv_subpartition_name => '||lv_subpartition_name||' and lv_partition_name=> '||lv_partition_name);
IF lv_subpartition_name IS NULL
THEN
DBMS_OUTPUT.PUT_LINE('INSIDE IF => '||m2.month_val);
INSERT INTO STORES_COMM_MOB_SUB_INFO T1 (
t1.ntlogin,
t1.first_name,
t1.last_name,
t1.job_title,
t1.store_id,
t1.batch_create_dt)
SELECT t2.ntlogin,
t2.first_name,
t2.last_name,
t2.job_title,
t2.store_id,
t2.batch_create_dt
FROM stores_comm_mob_sub_temp t2
WHERE TO_CHAR (batch_create_dt, 'YYYY') = m1.orig_year_val
AND 'M' || TO_CHAR (batch_create_dt, 'MM') =
m2.month_val;
ELSIF lv_subpartition_name IS NOT NULL
THEN
DBMS_OUTPUT.PUT_LINE('INSIDE ELSIF => '||m2.month_val);
MERGE INTO (SELECT *
FROM stores_comm_mob_sub_info
SUBPARTITION (lv_subpartition_name)) T1
USING (SELECT *
FROM stores_comm_mob_sub_temp
WHERE TO_CHAR (batch_create_dt, 'YYYY') =
m1.orig_year_val
AND 'M' || TO_CHAR (batch_create_dt, 'MM') =
m2.month_val) T2
ON (T1.store_id = T2.store_id
AND T1.ntlogin = T2.ntlogin)
WHEN MATCHED
THEN
UPDATE SET
t1.postpaid_totalqty =
(NVL (t1.postpaid_totalqty, 0)
+ NVL (t2.postpaid_totalqty, 0)),
t1.sales_transaction_dt =
GREATEST (
NVL (t1.sales_transaction_dt,
t2.sales_transaction_dt),
NVL (t2.sales_transaction_dt,
t1.sales_transaction_dt)),
t1.batch_create_dt =
GREATEST (
NVL (t1.batch_create_dt, t2.batch_create_dt),
NVL (t2.batch_create_dt, t1.batch_create_dt))
WHEN NOT MATCHED
THEN
INSERT (t1.ntlogin,
t1.first_name,
t1.last_name,
t1.job_title,
t1.store_id,
t1.batch_create_dt)
VALUES (t2.ntlogin,
t2.first_name,
t2.last_name,
t2.job_title,
t2.store_id,
t2.batch_create_dt);
END IF;
END LOOP;
END LOOP;
COMMIT;
end;
Much appreciate your inputs here.
Thanks,
MK.I've not used partitioning, but I do not see MERGE supporting a variable as a partition name in
MERGE INTO (SELECT *
FROM stores_comm_mob_sub_info
SUBPARTITION (lv_subpartition_name)) T1
USING ... I suspect it is looking for a partition called lv_subpartition_name.
I also don't see why you need that partition name - the ON clause should be able to identify the partition's criteria. -
Issue while using SUBPARTITION clause in the MERGE statement in PLSQL Code
Hello All,
I am using the below code to update specific sub-partition data using oracle merge statements.
I am getting the sub-partition name and passing this as a string to the sub-partition clause.
The Merge statement is failing stating that the specified sub-partition does not exist. But the sub-partition do exists for the table.
We are using Oracle 11gr2 database.
Below is the code which I am using to populate the data.
declare
ln_min_batchkey PLS_INTEGER;
ln_max_batchkey PLS_INTEGER;
lv_partition_name VARCHAR2 (32767);
lv_subpartition_name VARCHAR2 (32767);
begin
FOR m1 IN ( SELECT (year_val + 1) AS year_val, year_val AS orig_year_val
FROM ( SELECT DISTINCT
TO_CHAR (batch_create_dt, 'YYYY') year_val
FROM stores_comm_mob_sub_temp
ORDER BY 1)
ORDER BY year_val)
LOOP
lv_partition_name :=
scmsa_handset_mobility_data_build.fn_get_partition_name (
p_table_name => 'STORES_COMM_MOB_SUB_INFO',
p_search_string => m1.year_val);
FOR m2
IN (SELECT DISTINCT
'M' || TO_CHAR (batch_create_dt, 'MM') AS month_val
FROM stores_comm_mob_sub_temp
WHERE TO_CHAR (batch_create_dt, 'YYYY') = m1.orig_year_val)
LOOP
lv_subpartition_name :=
scmsa_handset_mobility_data_build.fn_get_subpartition_name (
p_table_name => 'STORES_COMM_MOB_SUB_INFO',
p_partition_name => lv_partition_name,
p_search_string => m2.month_val);
DBMS_OUTPUT.PUT_LINE('The lv_subpartition_name => '||lv_subpartition_name||' and lv_partition_name=> '||lv_partition_name);
IF lv_subpartition_name IS NULL
THEN
DBMS_OUTPUT.PUT_LINE('INSIDE IF => '||m2.month_val);
INSERT INTO STORES_COMM_MOB_SUB_INFO T1 (
t1.ntlogin,
t1.first_name,
t1.last_name,
t1.job_title,
t1.store_id,
t1.batch_create_dt)
SELECT t2.ntlogin,
t2.first_name,
t2.last_name,
t2.job_title,
t2.store_id,
t2.batch_create_dt
FROM stores_comm_mob_sub_temp t2
WHERE TO_CHAR (batch_create_dt, 'YYYY') = m1.orig_year_val
AND 'M' || TO_CHAR (batch_create_dt, 'MM') =
m2.month_val;
ELSIF lv_subpartition_name IS NOT NULL
THEN
DBMS_OUTPUT.PUT_LINE('INSIDE ELSIF => '||m2.month_val);
MERGE INTO (SELECT *
FROM stores_comm_mob_sub_info
SUBPARTITION (lv_subpartition_name)) T1 --> Issue Here
USING (SELECT *
FROM stores_comm_mob_sub_temp
WHERE TO_CHAR (batch_create_dt, 'YYYY') =
m1.orig_year_val
AND 'M' || TO_CHAR (batch_create_dt, 'MM') =
m2.month_val) T2
ON (T1.store_id = T2.store_id
AND T1.ntlogin = T2.ntlogin)
WHEN MATCHED
THEN
UPDATE SET
t1.postpaid_totalqty =
(NVL (t1.postpaid_totalqty, 0)
+ NVL (t2.postpaid_totalqty, 0)),
t1.sales_transaction_dt =
GREATEST (
NVL (t1.sales_transaction_dt,
t2.sales_transaction_dt),
NVL (t2.sales_transaction_dt,
t1.sales_transaction_dt)),
t1.batch_create_dt =
GREATEST (
NVL (t1.batch_create_dt, t2.batch_create_dt),
NVL (t2.batch_create_dt, t1.batch_create_dt))
WHEN NOT MATCHED
THEN
INSERT (t1.ntlogin,
t1.first_name,
t1.last_name,
t1.job_title,
t1.store_id,
t1.batch_create_dt)
VALUES (t2.ntlogin,
t2.first_name,
t2.last_name,
t2.job_title,
t2.store_id,
t2.batch_create_dt);
END IF;
END LOOP;
END LOOP;
COMMIT;
end;
Much appreciate your inputs here.
Thanks,
MK.
(SORRY TO POST THE SAME QUESTION TWICE).
Edited by: Maddy on May 23, 2013 10:20 PMDuplicate question
-
Returning clause in MERGE statement
Hi ,
I'm using Oracle 10g Version
I tried the below code using UPDATE with Returning Clause & MERGE with Returning Clause .
I found NO errors while working with UPDATE statement . The following is the code with UPDATE statement
DECLARE
TYPE empno_list IS TABLE OF emp.empno%TYPE;
vempno_list empno_list;
BEGIN
UPDATE emp
SET comm = 11
WHERE deptno IN (SELECT deptno FROM dept)
RETURNING empno
BULK COLLECT INTO vempno_list;
FOR i IN vempno_list.FIRST .. vempno_list.LAST
LOOP
DBMS_OUTPUT.put_line ('Values of EMP ' || vempno_list (i));
END LOOP;
END;
But getting the error PL/SQL: ORA-00933: SQL command not properly ended when working with MERGE Statement
declare
type empno_list is table of emp.empno%type;
vempno_list empno_list;
begin
merge into emp tgt
using dept src
on (src.deptno =tgt.deptno)
when matched then
update set tgt.comm=12
returning tgt.empno bulk collect into vempno_list ;
for i in vempno_list.first .. vempno_list.last loop
dbms_output.put_line('Values of EMP '||vempno_list(i) ) ;
end loop;
end;
Please suggest meProbably because the RETURNING INTO clause doesn't belong to MERGE statement. It's available only for INSERT, UPDATE and DELETE. Here is the quote from Oracle Documentation:
The static RETURNING INTO clause belongs to a DELETE, INSERT, or UPDATE statement. The dynamic RETURNING INTO clause belongs to an EXECUTEIMMEDIATE statement.
And here's the link.
RETURNING INTO Clause
Hope it helps.
Ishan -
2 MERGE statements in same trigger - is it possible ?
Hi guys,
I have an After Insert trigger (on table A) that takes the last record from table A and insert/update it on another table (B) then insert /update in same table (B) some records which depends on it. When trying to insert data in table A, the trigger raise this error: ORA-04092: cannot COMMIT in a trigger.
I understand the error, but I need expert's opinion to make the trigger works.
Here is the actual situation:
Table A has a trigger on after insert, and BASED ON THE LAST ROW inserted (only this data is subject to the trigger's actions) does the following:
1. MERGE the data (last row from the source table=A) with the data from the table destination=B. This data is specific to an employee;
2. Open a cursor that goes through all the ancestors of the employee (I have an employees hierarchy) and MERGE the same data (but for ancestors) with the table destination;
To be more specific :
EmpID LOB Day Status
12 1007 29 Solved
EmpID has ancestors 24 and 95. Therefore in the destination table I will have to do:
1. Merge data for EmpID 12;
2. Merge data for EmpID 24, 95:
EmpID LOB Day Status
24 1007 29 Just S (this is the status for ancestors)
95 1007 29 Just S
Steps 1 and 2 are inside a PL/SQL procedure that works fine by itself, but not within the trigger (since there are many transactions on the destination table). These 2 steps are required because for EmpID 12 I set a status and for the ancestors I set up a different status (this was the only way I could think of).
Can someone give me a hint how should I handle this situation ?
Thank you,
JohnBased on my understanding of what you are trying to do you are going about this in the wrong way. Try this approach instead:
1. Trigger fires initiating the process and passes relevant :NEW values to a stored procedure
2. The stored procedure does all the work and possibly should be an autonomous transaction
It looks very much like your code just doesn't belong in a trigger.
PS: Why a MERGE statement? If the rows most likely do not exist then INSERT and UPDATE in your exception handler: Otherwise UPDATE as the default and INSERT in the exception handler. It is not very likely the percentage of inserts and updates will be near 50% each. -
Error in Merge statement for Oracle 10gR2
Hi,
When I use the MERGE statement to copy data across a database link (from 10gR2 to 10gR2 database), if I have both an update and insert clause it works fine, but if I omit the insert clause and have just update on its own, I get error "ORA-02064: distributed operation not supported".
Can anyone help on thisThis came up in a thread last week, the 10g versions of MERGE (without INSERT or with DELETE) did not appear to work over DB links.
Maybe you are looking for
-
Phone control works in Jabber 9.1.4 doesn't work with 9.2 doesn't
I have a strange problem. Any Jabber for Window 9.1 works with phone control. If I download the latest Jabber for Windows 9.2.x then phone control stops working. This happens to all users and different PCs. I have checked my co
-
In segment IDOC attribute I occurred instead of SEGMENT
Hi I am trying to post an IDOC thru XI, using XSLT(ABAP Engine) to convert an incoming message to IDOC format. Though the IDOC output is well formed, the message fails with an error message(see below). Has anyone experienced this problem before? I am
-
I am trying to build a simple app with home page with a link to a page that has content presenter taskflow. If I navigate to the page with content presenter and click browser back button and then perform any operation, I see the following error : <Xm
-
Open new tab now opens a new window every time
Every time I try to open a new tab in my browser - either by using the keyword shortcut or the option under the file menu it just opens a new window. This must be a bug in the new Firefox as I just upgraded and it is a new issue. Very annoying! Does
-
WRE54G - DHCP Soundbridge not working
I am sure everyone has has had issues getting a WRE54G up and runing at home - not the easiest of products (when using the encryption option). I have an Organce LiveBox ADSL/Wireless router (Sagem badged Orange) and needed the range extended to get