Merge statment
does the merge statement would support 2 inserts on NOT MATCHED & MATCHED
Please how to achieve this.
thanks.
MERGE INTO audit2 e
USING audit1 h
ON (e.no = h.no)
WHEN NOT MATCHED THEN
INSERT (
NO
,NAME
,CREATED_USER
,CREATED_DATE
,MODIFIED_USER
,MODIFIED_DATE
,ARCH_TS
,EVENT_CD
VALUES (h.no ,h.name ,h.CREATED_USER , h.CREATED_DATE ,h.MODIFIED_USER,h.MODIFIED_DATE,systimestamp,'C' )
WHEN MATCHED THEN
INSERT (
NO
,NAME
,CREATED_USER
,CREATED_DATE
,MODIFIED_USER
,MODIFIED_DATE
,ARCH_TS
,EVENT_CD
VALUES (h.no ,h.name ,h.CREATED_USER , h.CREATED_DATE ,h.MODIFIED_USER,h.MODIFIED_DATE,systimestamp,'U' );
COMMIT;
Hi,
In MERGE, you can only INSERT when NOT MATCHED.
Are you trying to insert all the data from audit2 into audit1, regardless of whether it is matched or not, but to have the event_cd column of the new rows be 'U' when the new no matched one of the old nos, and have ev event_cd = 'C' otherwise?
If so:
INSERT INTO audit2 (no, name, ... event_cd)
SELECT no
, name
, CASE
WHEN no IN (
SELECT no
FROM audit2
THEN 'U'
ELSE 'C'
END
FROM audit1 h
LEFT OUTER JOIN audit2 e ON e.no = h.no
;The query below requires Oracle 10.1 (or higher).
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Since you're asking about a DML statement, the sample data should be the contents of the tables before the DML, and the results will be state of the changed table(s) when everything is finished.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using.
Similar Messages
-
due to some issue its removed.
NILI heard that the MERGE statement inserts some rows and updates others in a single operation.It seems that doc confirm that you heard well, obviously.
Take a look to the documentation :
http://download-uk.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_9016.htm#SQLRF01606
In this way can the following query can be wrritten using the MERGE statment.Hmmm, how quite huge are your queries... I won't take all my (work) day to try to write a merge for you, maybe you can try it yourself ?
Nicolas. -
Hi all.
I have 2 tables for the merge:
CREATE TABLE DAM_ACCESOS_LASTLOGIN
FECHA DATE,
USUARIO VARCHAR2(100)
TABLESPACE COLOSO
CREATE TABLE DAM_ACCESOS_LASTLOGIN_STAGE
FECHA DATE,
USUARIO VARCHAR2(100)
TABLESPACE COLOSO
Atm DAM_ACCESOS_LASTLOGIN is empty and i im trying to populate it with the data from DAM_ACCESOS_LASTLOGIN_STAGE. For that im using a merge statment like this:
merge into dam_accesos_lastlogin f
using dam_accesos_lastlogin_stage s
on (f.usuario=s.usuario)
when matched then
update set f.fecha=s.fecha
when not matched
then insert (f.fecha,f.usuario)
values (s.fecha,s.usuario);
The DAM_ACCESOS_LASTLOGIN_STAGE have 29 millions rows, and sql said only 2millions have been merged. But when i select DAM_ACCESOS_LASTLOGIN instead of date,user only one time i have the same user with distinct dates.
For example:
30/10/2007|[email protected]
01/11/2007|[email protected]
28/10/2007|[email protected]
26/10/2007|[email protected]
24/10/2007|[email protected]
23/10/2007|[email protected]
25/10/2007|[email protected]
21/10/2007|[email protected]
22/10/2007|[email protected]
29/10/2007|[email protected]
16/10/2007|[email protected]
04/11/2007|[email protected]
Im trying to have only one date and one user on DAM_ACCESOS_LASTLOGIN , and update the date if the user existes in DAM_ACCESOS_LASTLOGIN_STAGE, but dunno why dont work
In DAM_ACCESOS_LASTLOGIN_STAGE i have same user, diferent dates and in DAM_ACCESOS_LASTLOGIN only want one date x user.
Any help will be awesome.
Thanks
Message was edited by:
user496863The merge statement does not row by row processing, it is doing all at once.
If you have the same user multiple times in your start table DAM_ACCESOS_LASTLOGIN_STAGE, then it will insert it multiple times.
To avoid it you should select each user only once, e.g.:
merge into dam_accesos_lastlogin f
using (select usuario, max(fecha) fecha from dam_accesos_lastlogin_stage group by usario) s
on (f.usuario=s.usuario)
when matched then
update set f.fecha=s.fecha
when not matched
then insert (f.fecha,f.usuario)
values (s.fecha,s.usuario);Btw: I like the name of your tablespace! COLOSO is much better than my usual DATA_HUGE. -
Hi There
I would like to compare two excel sheets
Sheet1 - Current day records
Sheet2 - Last day records
My requirement is this : Comparing sheet1 and sheet2
1. updating the changes and inserting new records into current day with respect to last day.
2. I have idea of using Merge. But, use it with PL/sql for comparing two excel sheets how?
Please help
Thanks
RajRangarajan wrote:
If you want SQL or PL/SQL to process the data then it really needs to be on the database.
__What ever it may be i need solution.*I am ok for creating two addtional tables to run the show.*Well you write the solution, we just give you the information you will need to do it. Unless of course you want to pay for our services? (hint: a little less of the bold agressive posturing would go down better; asking nicely works for most people)
So, although you say it's excel sheets, are you really talking about CSV files (which are not Excel sheets, but Character Seperated Values)?
its not csv its xls only_
If you are dealing with CSV's then you could create external tables that read the data from the files and then do the comparisons mentioned above, especially if you are looking to MERGE the data, because SQL commands such as insert, merge etc. don't work on flat files.
its ok* Ok, Excel (.xls) files are a proprietary Microsoft binary format file. These can't just be read into the database as they are. If your database was on a windows platform you could set up an Excel ODBC connection to the .xls files and then treat them as external databases. As you're on solaris you won't really be able to do that. You only option therefore is to export your data to CSV or other flat file format so that the database can read the data from those. CSV is preferable as you will be able to use Oracles External tables to load the data.
You don't have to share your actual data with us, just give us some idea of the infrastructure you are dealing with. You haven't even told us if you've got the data loaded into database tables yet...
its ora 9i / solaris*
Let me put some more clarity on the objective.
Currently, I am comparing two excel sheet manually(using vlookup, match etc,) Utimately, I am doing set minus, presence of previous run and not in current run. then,I will make the changes to current run and send excel sheet to email integration system. I would like to automate entire stuff.So here's the steps you need...
Export excel sheets to CSV files.
Have Oracle external tables defined to point at those flat files.
Use the aforementioned comparison methods to compare the two flat files by reading with SQL from the external tables.
Generate the output into another CSV file. (you can use writeable external tables in 10g onwards, but as you're on 9i you'll need to use UTL_FILE to write it out instead).
Take your written CSV file and load that back into Excel. (Excel likes reading CSV's)
The problems you've got in automating it are getting the data exported from Excel to CSV files and also reading the output CSV back into Excel. You may be able to do this using some OLE/DDE type stuff, but as you're on a solaris platform rather than Windows, that could prove a little tricky.
Could you arrange to have the data delivered to you already in CSV format rather than as Excel sheets? Could you provide CSV results rather than an Excel sheet in return? -
Any idea why this MERGE INTO statment fails?
I am using Oracle XE and trying to use a MERGE statment from an external table to a heap table.
MERGE INTO
ORDER_ITEM_INTELLI_LABS O
USING
ORDER_ITEM_INTELLI_LABS_STAGE S
ON (O.ACC_NO = S."Acc No",
O.DRAW_DATE = to_date(S."Draw Date",'MM/DD/RRRR'),
O.MED_REC_NO = S."Med Rec#",
O.CODE_CPT = S."CPT",
O.PROVIDER_ID = S."Provider ID",
O.CLIENT_ID = S."Client ID")
WHEN NOT MATCHED THEN
INSERT
(ORDER_ITEM_INTELLI_LABS_UID,
DEPARTMENT_UID,
DRAW_DATE,
ACC_NO,
MED_REC_NO,
PATIENT_NAME,
LAB_TEST_NAME,
CODE_CPT,
INS_CARRIER,
PROVIDER_ID,
CLIENT_ID,
UPLOAD_DATE)
VALUES
(null,
in_DEPARTMENT_UID,
to_date(S."Draw Date",'MM/DD/RRRR'),
S."Acc No",
S."Med Rec#",
S."Patient Name",
S."Lab Test Name",
S."CPT",
S."Ins Carrier",
S."Provider ID",
S."Client ID",
sysdate)
where
S."Draw Date" is NOT NULL;This is what I get:
1 MERGE INTO
2 ORDER_ITEM_INTELLI_LABS O
3 USING
4 ORDER_ITEM_INTELLI_LABS_STAGE S
5 ON (O.ACC_NO = S."Acc No",
6 O.DRAW_DATE = to_date(S."Draw Date",'MM/DD/RRRR'),
7 O.MED_REC_NO = S."Med Rec#",
8 O.CODE_CPT = S."CPT",
9 O.PROVIDER_ID = S."Provider ID",
10 O.CLIENT_ID = S."Client ID")
11 WHEN NOT MATCHED THEN
12 INSERT
13 (ORDER_ITEM_INTELLI_LABS_UID,
14 DEPARTMENT_UID,
15 DRAW_DATE,
16 ACC_NO,
17 MED_REC_NO,
18 PATIENT_NAME,
19 LAB_TEST_NAME,
20 CODE_CPT,
21 INS_CARRIER,
22 PROVIDER_ID,
23 CLIENT_ID,
24 UPLOAD_DATE)
25 VALUES
26 (null,
27 in_DEPARTMENT_UID,
28 to_date(S."Draw Date",'MM/DD/RRRR'),
29 S."Acc No",
30 S."Med Rec#",
31 S."Patient Name",
32 S."Lab Test Name",
33 S."CPT",
34 S."Ins Carrier",
35 S."Provider ID",
36 S."Client ID",
37 sysdate)
38 where
39* S."Draw Date" is NOT NULL
SQL> /
ON (O.ACC_NO = S."Acc No",
ERROR at line 5:
ORA-00907: missing right parenthesis
SQL>This?
MERGE INTO ORDER_ITEM_INTELLI_LABS O
USING ORDER_ITEM_INTELLI_LABS_STAGE S
ON (O.ACC_NO = S."Acc No"
AND O.DRAW_DATE = TO_DATE (S."Draw Date", 'MM/DD/RRRR')
AND O.MED_REC_NO = S."Med Rec#"
AND O.CODE_CPT = S."CPT"
AND O.PROVIDER_ID = S."Provider ID"
AND O.CLIENT_ID = S."Client ID")
WHEN NOT MATCHED THEN
INSERT (ORDER_ITEM_INTELLI_LABS_UID, DEPARTMENT_UID, DRAW_DATE, ACC_NO, MED_REC_NO, PATIENT_NAME, LAB_TEST_NAME,
CODE_CPT, INS_CARRIER, PROVIDER_ID, CLIENT_ID, UPLOAD_DATE)
VALUES (NULL, in_DEPARTMENT_UID, TO_DATE (S."Draw Date", 'MM/DD/RRRR'), S."Acc No", S."Med Rec#", S."Patient Name",
S."Lab Test Name", S."CPT", S."Ins Carrier", S."Provider ID", S."Client ID", SYSDATE)
WHERE S."Draw Date" IS NOT NULL; -
HElp in MERGE statement.
I want to user MERGE statment. I want to merge values in another database of table on the netwrok. When i am going to run my script the following error araise.
ORA-02071 error initializing capabilities for remote database string
How can u user MERGE.
scott@ORA92> MERGE into emp@DB_LINK c
USING scott.emp e
on (c.empno = e.empno)
WHEN MATCHED THEN
UPDATE SET
c.ename = e.ename,
c.job = e.job,
c.mgr = e.mgr,
c.hiredate = e.hiredate,
c.sal = e.sal,
c.comm = e.comm,
c.deptno = e.deptno
WHEN NOT MATCHED THEN
insert values(e.empno,e.ename,e.job,e.mgr,e.hiredate,e.sal,e.comm,e.deptno)ORA-02071: error initializing capabilities for remote database string
Cause: Could not load a remote-specified capability table.
Action: Contact Oracle Support Services for the remote SQL*Connect product. -
Merge with ampersand (&)
Hi
I exporting and importing some data of 2 tables to other schema, I used Merge Statment, but there are some data with ampersand (&), is there some way to bypass , see data below in column T$NOSN$O there are a ampersand
MERGE INTO TRITON.TTCCOM994501 A USING
(SELECT
'ufeferna ' as "T$CDUS$O",
844 as "T$NCMP$O",
'7647 ' as "T$CCAR$O",
'FERNANDO MAMBRINE FERRI ' as "T$NOUS$O",
'ADM VENDAS ' as "T$NODP$O",
' ' as "T$NUTL$O",
TO_DATE('07/26/2006 00:00:00', 'MM/DD/YYYY HH24:MI:SS') as "T$DHCT$O",
TO_DATE('01/01/9999 00:00:00', 'MM/DD/YYYY HH24:MI:SS') as "T$DHVD$O",
'_m\l&E ' as "T$NOSN$O",
'657892962' as "T$CGCC$O",
' ' as "T$CGCE$O",
20 as "T$CGCD$O",
2 as "T$AUTO$O",
' ' as "T$CCUS$O",
' ' as "T$LCUS$O",
' ' as "T$SUNO$O",
1 as "T$ASAM$O",
' ' as "T$EFUL$O",
' ' as "T$EINT$O",
' ' as "T$FNAM$O",
'NAO ' as "T$LNAM$O",
0 as "T$REFCNTD",
0 as "T$REFCNTU"
FROM DUAL) B
ON (A.T$CDUS$O = B.T$CDUS$O)
WHEN NOT MATCHED THEN
INSERT (
T$CDUS$O, T$NCMP$O, T$CCAR$O, T$NOUS$O, T$NODP$O,
T$NUTL$O, T$DHCT$O, T$DHVD$O, T$NOSN$O, T$CGCC$O,
T$CGCE$O, T$CGCD$O, T$AUTO$O, T$CCUS$O, T$LCUS$O,
T$SUNO$O, T$ASAM$O, T$EFUL$O, T$EINT$O, T$FNAM$O,
T$LNAM$O, T$REFCNTD, T$REFCNTU)
VALUES (
B.T$CDUS$O, B.T$NCMP$O, B.T$CCAR$O, B.T$NOUS$O, B.T$NODP$O,
B.T$NUTL$O, B.T$DHCT$O, B.T$DHVD$O, B.T$NOSN$O, B.T$CGCC$O,
B.T$CGCE$O, B.T$CGCD$O, B.T$AUTO$O, B.T$CCUS$O, B.T$LCUS$O,
B.T$SUNO$O, B.T$ASAM$O, B.T$EFUL$O, B.T$EINT$O, B.T$FNAM$O,
B.T$LNAM$O, B.T$REFCNTD, B.T$REFCNTU)
WHEN MATCHED THEN
UPDATE SET
A.T$NCMP$O = B.T$NCMP$O,
A.T$CCAR$O = B.T$CCAR$O,
A.T$NOUS$O = B.T$NOUS$O,
A.T$NODP$O = B.T$NODP$O,
A.T$NUTL$O = B.T$NUTL$O,
A.T$DHCT$O = B.T$DHCT$O,
A.T$DHVD$O = B.T$DHVD$O,
A.T$NOSN$O = B.T$NOSN$O,
A.T$CGCC$O = B.T$CGCC$O,
A.T$CGCE$O = B.T$CGCE$O,
A.T$CGCD$O = B.T$CGCD$O,
A.T$AUTO$O = B.T$AUTO$O,
A.T$CCUS$O = B.T$CCUS$O,
A.T$LCUS$O = B.T$LCUS$O,
A.T$SUNO$O = B.T$SUNO$O,
A.T$ASAM$O = B.T$ASAM$O,
A.T$EFUL$O = B.T$EFUL$O,
A.T$EINT$O = B.T$EINT$O,
A.T$FNAM$O = B.T$FNAM$O,
A.T$LNAM$O = B.T$LNAM$O,
A.T$REFCNTD = B.T$REFCNTD,
A.T$REFCNTU = B.T$REFCNTU;Hi,
You have two options:
1. You can concatenate string after &:
SQL> select '_m\l&'||'E ' as "T$NOSN$O" from dual;
T$NOSN$O
_m\l&E 2. On SqlPlus you can SET DEFINE OFF
SQL> set define off
SQL> select '_m\l&E ' as "T$NOSN$O" from dual;
T$NOSN$O
_m\l&EMiguel -
i would like to know if we could use the MERGE statment with Views ?
thanks.See the restrictions for 9i release 2.
http://download-west.oracle.com/docs/cd/B10501_01/server.920/a96540/statements_915a.htm#2080942
See the restrictions for 10g.
http://download-west.oracle.com/docs/cd/B14117_01/server.101/b10759/statements_9016.htm#sthref7015 -
Help on Merge Statement ,I got 'ORA-00904: invalid column name' Error
Pls help
In Oracle 9i i implement the following qry
MERGE INTO jobs A
USING (select order_no,jOB_SEQ_NO from jobs_dlt) B
ON (A.ORDER_NO = B.ORDER_NO and A.JOB_SEQ_NO =B.JOB_SEQ_NO )
WHEN MATCHED THEN
UPDATE SET
A.ORDER_NO= B.ORDER_NO ,
A.JOB_SEQ_NO= B.JOB_SEQ_NO
WHEN NOT MATCHED THEN
INSERT (
A.JOB_SEQ_NO ,
A.ORDER_NO
VALUES (
B.JOB_SEQ_NO ,
B.ORDER_NO
but i got 'ORA-00904: invalid column name' Error
JOBS table Contain the above Column
how i implement the Merge Statment
Thanks in advance
By
SekarI seem to recall this error being spuriously (well unhelpfully) thrown if you tried to UPDATE a key that you used in the ON clause, but I could be mistaken.
For us to recreate this you would need to supply the exact version and scripts to create the tables in question. -
Hi,
Is it possible to insert and update at the same time in the when not matched part of the MERGE statement.
Pl provide me the information how to do it in MERGE statment.
thanks in advance.Hi,
Post an example of what you want to do.
Include:
(1) the version of Oracle you are using
(2) the contents of the tables (both the destination table you are modifying and the soure table or tables) before the MERGE
(3) the required contents of the destination table after the MERGE
Executable SQL statements (like "CREATE TABLE AS ..." or "INSERT ..." statements) are best for (2).
Formatted tabular output is okay for (3). Type {code} before and after the tabular text, to preserve spacing. -
What is the main difference between Merge Statment(only update) with simple Update statement based on Select query ?
Which one is faster ?There are no vanilla answers to such a question especially when asked by someone who doesn't have time to post version number or DML.
The only honest answer ... build a test environment and test it using EXPLAIN PLAN and SET TIMING ON.
That said: A merge statement should only be used where it is appropriate. -
Hi All
I have MERGE statment to peform update or insert operation.
I popluate about 1 million records using sqlldr to a staging table.
I have to compare the record exists in main table then update with values in staging table, or else insert the record in main table.
I am using Oracle Database 10g Enterprise Edition Release 10.1.0.2.0.
The problem is the execution of this statement takes more than 3 hours to complete. The volume of main table in 6 million records.
Please help how I can tune this query or process to make it work faster.
The query is as below
merge into t1
using (
select /*+ INDEX(st1 c_PK) */
* from st1
where (c1)
in (
select /*+ INDEX(st1 c_PK) */
max(c1) from st1 where c1 >= pStat and mo_id < pEnd
group by c2
)) t2
on (t1.c1 = t2.c1 and t1.c2=t2.c2)
when matched then
update set
c4 = t2.c4,
c5 = t2.c5,
-- all other columns here
when not matched then
insert ( c1, c2, c3, c4 ... )
values
(t2.c1, t2.c2, t2.c3, t2.c4 ..);Just as an after thought oracle 10g allows you to know place slaves on the tables themselves.Do you mean setting a default degree of parallelism on the table? Because this "place slaves on the tables" way of describing it is new to me.
Judging by the size of the one table I would assign 4 slaves to this. I don't think you have nearly enough information to judge this.
Between this and the temp group tablespace setup should help you a great deal.There seems to be a question mark over this also. -
Merge statement required with Logic
Table Name: F_SCENARIO
System : Dataware house
Oracle version : 11g
Record Count : 2 Million records
Correct scenario records
F_Key
F_Bridge_key
Record_type
1
1
1
2
1
2
3
1
3
Wrong scenario records
F_Key
F_Bridge_key
Record_type
1
1
1
2
-5
2
3
-6
3
I want to write a Merge statment to update the nagative values into 1, can you please help me to provide some suggestion and query
Please let know if you any confusion in the above data'sHi Tubby,,
I have 2 million records like above set of records
you have hardcoded f_bridge_key = 1, it not always same values of 1, why because the f_bridge_key is sequnce number
Merge statment required becasue client is expecting with merge statment, the below highleted bold color is one set of records
Wrong scenario records
F_Key
F_Bridge_key
Record_type
1
1
1
2
-5
2
3
-6
3
4
4
1
5
-8343
2
6
-3534
3 -
Difference between MERGE and Update/Insert
Can anybody pls tell me what is the difference between MERGE and Update/Insert statements.
hi vivek,
merge is totally different than normal update and insert.Merge is a combination of update+inserrt.
Merge statment first checks the data in your table, if data is already exists then it will try to update the data otherwise it will go for insertion.In case of normal update/insert it will not check the data directly it does the specified operation.
So based on your requirement you have to select the best option.
Thanks. -
Inserted | updated | deleted | merged
Hello,
If a table has been set to update/insert and a row arrives with a matching column then the corresponding row in the target table is updated. In the job details window shouldnt it show a 1 under the updated section. My question really is, what does the merged tab indicate ?
ThanksHi,
As per my knowledge
If map runs in row based mode-- insert,update tabs are filled
If map runs in set based mode- merge will come
Row Based mode: In the pl/sql pkg generated by the map
insert and update operation will happen for each row
For insert/update option --First insert statement will run if the record exists then exception will come , then in the exception block update statment will be there and thee the record will be udpated..
For update/insert option -- First it tries to update the record.. if it doestnt find the record it raises an exception , in the exception block insert statment will be ther
that will insert the record..
In Set based Mode:
Instead of separate insert/update statements one merge stament will be there it will run...
So set based mode is faster than row based mode..
If any errors comes, then its difficult to find the source record causing the error in set based mode as it is in merge statment
but it is easy in row based as it will do insert/update for each record --- each record will be inserted in the audit tables.
Hope this will give u an idea
Than you
Maybe you are looking for
-
MSI X48C Platinum and DDR3 corsair 2x4 GB @ 1600mhz WILL IT WORK??
Hi , I truly appreciate the mere existence of this forum . my system is : PSU : Corsair 650HX CPU : Intel Core2Duo E6750 GPU : Geforce GTX 260 RAM : (currently 2x1gig DDR2 @ 667 Mhz) and i just bought 2x4gb dims of DDR3 ram (corsair vengeance) it doe
-
Hi all: I am still seeing an issue on our BM3.9sp2 box running on NW6.5sp8 and eDir 8.8.5. I have two credit card machines, one in subnet 10.1.x.x and one in subnet 10.2.x.x which is across a wan. Here are the filters I setup: 10.1.x.x subnet outboun
-
Can any one tell me where I can find the methodtracer class
I am trying to compile the Bean Box withing the BDK and have got to a stage where I can not compile further because I am missing the class MethodTracer. Does anyone know where I can find this class. Thank you
-
Oject List Data Provider is not working
I am having trouble using an Object List Data Provider. The Field Guide books says when I drop an Object List Data Provider, then select the list property, to bind it to a list, there is suppose to be an edit box to the right of the property that lau
-
How to point to the HelpSet file which is inside a Jar
Hi All, Can anyone please help me out on as to how I incorporate my help files (which are inside a jar) with JavaHelp. They are working fine when they are in a folder. But giving an error when i am trying to reference it from a jar file. Thanx in adv