Help for merge statement?
I have a problem in Merge statement. My merge statement is following:
MERGE INTO hoadon hd
USING (
SELECT m.ma_ttoan, t.ma_ttoan ma_kh
FROM tai_anhxa_makh t
INNER JOIN hoadon m on t.ma_kh = m.ma_ttoan
WHERE m.thang_nam = '200908' and m.ma_ttoan not like 'DLC%' and t.ma_ttoan IS NOT NULL
GROUP BY m.ma_ttoan, t.ma_ttoan
) m ON (hd.ma_ttoan = m.ma_kh)
WHEN MATCHED THEN
UPDATE SET ma_ttoan = m.ma_ttoan
WHEN NOT MATCHED THEN
INSERT (thang_nam, ma_ttoan) VALUES('200908','thaodv')
After execute this query, PS/SQL show error message: "ORA-00904: hd.ma_ttoan invalid identifier"
I'm using Oracle version 9i
Can anyone help me to resolve this problem?
Thanks in advance
In 9i you can't use the columns from the ON clause in your UPDATE part of the MERGE statement
this is invalid:
UPDATE SET ma_ttoan = m.ma_ttoanuse a different column here.
Similar Messages
-
I have a MERGE statement, I am executing it from shell script, I want to print that how many rows has been updated and Inserted.
Can any one give me some idea.
Thanks in AdvanceI want to print that how many rows has been updated and Inserted. If you really want/need to print that information, then don't use MERGE but INSERT and UPDATE statements.
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:35615502072484
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:122741200346595110 -
Hi Experts
require your help for my one of requirements .My database is oracle 11g.table name is STG_TABLE.
table stucture is like below
select * from stg_tables where batch_id in('2806','2805')
BATCH_ID COMMITMENT_NUM SAP_CREDITMEMO_NUM SAP_ORDER_NUM CREDIT_AMOUNT SAP_CUST_NUM RECORD_STATUS RECORD_STATUS_MSG HANDSHAKE_DATE
2805 209427 81034559 30386865 34 1000035 S Billing document successfully posted 2/13/2013 18:42
2806 209427 85287754 40180808 60 1000035 S Billing document successfully posted 2/13/2013 20:33
2806 209534 85287755 40181806 60 1000037 S Billing document successfully posted 2/13/2013 14:42
2806 209534 85287755 40181806 90 1000037 S Billing document successfully posted 2/13/2013 14:42here for same commitment_num column value if batch_id are different then dont have to do anything ,but if batch_id are same ,I have to merge record
and credit_amount column value should be sum(credit_amount) and all records should be converted into one single record ..like below for above data .
BATCH_ID COMMITMENT_NUM SAP_CREDITMEMO_NUM SAP_ORDER_NUM CREDIT_AMOUNT SAP_CUST_NUM RECORD_STATUS RECORD_STATUS_MSG HANDSHAKE_DATE SIEBEL_PROCESS_DATE
2805 209427 81034559 30386865 34 1000035 S Billing document successfully posted 2/13/2013 18:42
2806 209427 85287754 40180808 60 1000035 S Billing document successfully posted 2/13/2013 20:33
2806 209534 85287755 40181806 150 1000037 S Billing document successfully posted 2/13/2013 14:42 Please help me on it and le me know if my requirement is not clear.
will it be possible using merge ? ..if I think using simple cursor approce ...like take data into cursor as per below and then insert the records but when I should delete existing records then .
CURSOR abc
IS
SELECT commitment_num AS commitment_num,
sap_cust_num AS customer_number, batch_id AS batch_id,
----CRQ000000161235-Allow Debit-Credit both
-- Max(Cust_Ref_Num) As Customer_Reference,--Commented on 30-aug-2011 madhuri,as per reqt cust-ref should be in *ctl table and invoice# should be in *adcoms table
COUNT (*) AS line_count,
--Sum(Commitment_Amount) As Committed_Amount, -- As Per Conversation With Greg Send The Sum Of Approved Amount As Committed Amount.
SUM (approved_amount) AS approved_amount,
sales_org AS sales_org
--CRQ124450 KS Canadian payments in 9AM feed
FROM stg_vg_credits
WHERE TRUNC (last_update_date) <= TRUNC (SYSDATE)
AND siebel_process_date IS NULL and record_status='S'
and sales_org='2403'
--CRQ000000161235-Allow Debit-Credit
GROUP BY commitment_num, sap_cust_num, sales_org, batch_id;You can use MERGE to update some rows and delete others. Here's the code, then I will explain.MERGE INTO stg_tables o
using (
SELECT rid, rn, sum_credit_amount FROM (
SELECT ROWID rid,
count(*) OVER(PARTITION BY commitment_num, batch_id) cnt,
row_number()
over(PARTITION BY commitment_num, batch_id order by handshake_date desc) rn,
sum(credit_amount)
OVER(PARTITION BY commitment_num, batch_id) sum_credit_amount
FROM stg_tables A
WHERE batch_id IN('2806','2805')
WHERE cnt > 1
) n
ON (o.ROWID = n.rid)
WHEN MATCHED THEN UPDATE SET credit_amount = sum_credit_amount
delete where rn > 1;Look at the inner query in the USING clause: CNT is the number of rows having the same commitment_num and batch_id. RN numbers those rows, with 1 for the newest row. I also get the credit amount sum and the ROWID of the row.
Now for the outer query in the USING clause: I only keep the rows with CNT > 1, because the others don't need to be merged. This is important, because you don't want to be updating rows that are already OK.
Now the MERGE statement matches the table and my USING clause on ROWID, which can be very efficient. It updates all the rows with the new credit amount, but then it DELETEs all the rows but the most recent one.
If you run the MERGE twice, you will see that the second time it merges "0 rows". No unnecessary updates!
Edited by: Stew Ashton on Feb 25, 2013 9:50 AM -
Instead of trigger is NOT firing for merge statements in Oracle 10gR2
The trigger fires fine for a update statement, but not when I use a merge statement
with an update clause. Instead I get the normal error for the view ( which is a union all view, and therefore not updatable.)
The error is :-
ORA-01733: virtual column not allowed here
oracle release is 10.2.0.2 for AIX 64L
Is this a known bug ?
I've used a multi-table insert statement to work around the problem for inserts, but
for updates, I'd really like to be able to use a merge statement instead of an update.
Mark.This is my cut-down version :-
In this case case I'm getting an :-
ORA-01446: cannot select ROWID from, or sample, a view with DISTINCT, GROUP BY, etc.
rather then the ora-01733 error I get in the real code ( which is an update from an involved
XML expression - cast to a table form)
create table a ( a int primary key , b char(30) ) ;
create table b ( a int primary key , b char(30) ) ;
create view vw_a as
select *
from a
union all
select *
from b ;
ALTER VIEW vw_a ADD (
PRIMARY KEY
(a) DISABLE);
DROP TRIGGER TRG_IO_U_ALL_AB;
CREATE OR REPLACE trigger TRG_IO_U_ALL_AB
instead of update ON vw_a
for each row
begin
update a targ
set b = :new.b
where targ.a = :new.a
if SQL%ROWCOUNT = 0
then
update b targ
set b = :new.b
where targ.a = :new.a
end if ;
end ;
insert into a values (1,'one');
insert into a values (2,'two');
insert into a values (3,'three');
insert into b values (4,'quatre');
insert into b values (5,'cinq');
insert into b values (6,'six');
commit;
create table c as select a + 3 as a, b from a ;
commit;
merge into vw_a targ
using (select * from c ) src
on ( targ.a = src.a )
when matched
then update
set targ.b = src. b
select * from vw_a ;
rollback ;
update vw_a b
set b = ( select c.b from c where b.a = c.a )
where exists ( select c.b from c where b.a = c.a ) ;
select * from vw_a ;
rollback ; -
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 -
Hi,
Need help to write sql statement.
create table t_dt ( dt_start date, dt_end date, amount number);
insert into t_dt values('1-Jan-10','10-Feb-10',12);
insert into t_dt values('11-Feb-10','10-Mar-10',10);
insert into t_dt values('11-Mar-10','20-Apr-10',8);
insert into t_dt values('21-Apr-10','28-Jun-10',10);
insert into t_dt values('29-Jun-10','20-Sep-10',10);
insert into t_dt values('21-Sep-10','10-Oct-10',10);
insert into t_dt values('11-Oct-10','31-Dec-10',8);
insert into t_dt values('1-Jan-11','10-Feb-11',8);
insert into t_dt values('11-Feb-11','10-Mar-11',7);
insert into t_dt values('11-Mar-11','20-Apr-11',6);
insert into t_dt values('21-Apr-11','28-Jun-11',6);
insert into t_dt values('29-Jun-11','20-Sep-11',6);
insert into t_dt values('21-Sep-11','10-Oct-11',4);
insert into t_dt values('11-Oct-11','31-Dec-11',8);
Result should be like below..
dt_start dt_end Amount
1-Jan-10 10-Feb-10 12
11-Feb-10 10-Mar-10 10
11-Mar-10 20-Apr-10 8
21-Apr-10 10-Oct-10 10
11-Oct-10 10-Feb-11 8
11-Feb-11 10-Mar-11 7
11-Mar-11 20-Sep-11 6
21-Sep-11 10-Oct-11 4
11-Oct-11 31-Dec-11 8
Just to explain the example, take a row with start date as 21-Apr-10 in the above insert statements, since it has the same amount for next two rows (i.e. with start date '29-Jun-10' and '21-Sep-10') these 3 rows should be converted to represent only 1 row in the result and the start date and end date should be changed per the result shown above.
Thanks.Hello
I think this gives yuo what you need....
SELECT
MIN(dt_start),
MAX(dt_end),
amount
FROM
( SELECT
dt_start,
dt_end,
MAX(marker) OVER(ORDER BY dt_start) marker,
amount
FROM
Select
dt_start,
dt_end,
amount,
CASE
WHEN LAG(amount) OVER(ORDER BY dt_start) <> amount THEN
ROW_NUMBER() OVER(ORDER BY dt_start)
END marker
from t_dt
GROUP BY
amount,
marker
order by
MIN(dt_start)
MIN(DT_START) MAX(DT_END) AMOUNT
01-JAN-2010 00:00:00 10-FEB-2010 00:00:00 12
11-FEB-2010 00:00:00 10-MAR-2010 00:00:00 10
11-MAR-2010 00:00:00 20-APR-2010 00:00:00 8
21-APR-2010 00:00:00 10-OCT-2010 00:00:00 10
11-OCT-2010 00:00:00 10-FEB-2011 00:00:00 8
11-FEB-2011 00:00:00 10-MAR-2011 00:00:00 7
11-MAR-2011 00:00:00 20-SEP-2011 00:00:00 6
21-SEP-2011 00:00:00 10-OCT-2011 00:00:00 4
11-OCT-2011 00:00:00 31-DEC-2011 00:00:00 8
9 rows selected.HTH
David
Edited by: Bravid on Feb 23, 2012 12:08 PM
Beaten to it by Frank! :-) -
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. -
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. -
hi,
I want to usefollowing sql statement by preparedstatment
select * from tbl_PreServiceManager where (trunc(Activation_Date+30)<trunc(Sysdate) or counter > 0) and CPS<>'FDE'";{code}
to convert it to prepared statement i did following
{code}String query="select * from tbl_Pre where (trunc(Activation_Date+30)<trunc(Sysdate) or counter >?) and CPS<>?;"
pstmt = conn.prepareStatement( query );
pstmt.setInt(1,0);
pstmt.setString(2,"FDE");
rset = pstmt.executeQuery();
{code}
but how i manage activation_date with trunc function, infact activation_date field is datetime and i only want to campare date.
ThanksEG:
datepart (dp):
dd -> day -> 1-31
dy -> day of year -> 1-366
hh -> hour -> 0-23
mi -> minute -> 0-59
mm -> month -> 1-12
qq -> quarter -> 1-4
ss -> second -> 0-59
ms -> millisecond -> 0-999
wk -> week -> 0-53
dw -> weekday -> 1-7(Sun - Mon)
yy -> year -> 1753
SELECT DATEPART(yy,GETDATE()),
DATEPART(mm,GETDATE()),
DATEPART(dy,GETDATE())Output
2008 | 07 | 28 -
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
palHi..
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 -
Help needed in MERGE statement
Hi,
I am new to PL/SQL, I want to update a table called "final_test" based on the below query result.
1. I want to check whether that particular record is present or not in my "final_test" table.
2. If its present in the "final_test" table and the process_status got changed then I want to update that alone in my "final_test" table.
3. If its not present then I want to insert that record into my "final_test" table.
Basically I am retrieving the report and its status for a particular date.
select
b.id,
a.name,
a.t_name,
c.process_status,
c.time_process
from rep_tab_map a, j_tab_map b, proc_status c
where a.t_name=b.t_name
and b.id=c.id (+)
and trunc(c.date_start)=trunc(sysdate -1)
group by a.name,b.id,c.process_status,c.time_process,a.t_name
order by 2
I thought of using Merge statement but i am not sure what i have to use in ":USING" and "ON" clause.
Please help me with MERGE or with someother way.
ThanksAssuming final_test has same structure as select list in your query:
merge
into final_test a
using (
select b.id,
a.name,
a.t_name,
c.process_status,
c.time_process
from rep_tab_map a,
j_tab_map b,
proc_status c
where a.t_name=b.t_name
and b.id=c.id(+)
and trunc(c.date_start)=trunc(sysdate -1)
group by a.name,b.id,c.process_status,c.time_process,a.t_name
) b
on (b.id = a.id)
when matched then update set a.name = case a.process_status
when b.process_status then a.name
else b.name
end,
a.t_name = case a.process_status
when b.process_status then a.t_name
else b.t_name
end,
a.process_status = b.process_status,
a.time_process = case a.process_status
when b.process_status then a.time_process
else b.time_process
end
when not matched then insert(
a.id,
a.name,
a.t_name,
a.process_status,
a.time_process
values(
b.id,
b.name,
b.t_name,
b.process_status,
b.time_process
/SY. -
Help needed in the Merge Statement
Hi All,
I am using MERGE statement in my program. I want to maintain the log for the duplicate reords mean maintain the log for those reocrds which are updated in the merge statement.
Can any one help me in this that how can i maintain the log?
Thanks for your help in advance.
kind Regards,http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:35615502072484
-
MERGE Statement Problem for Storing Old Data
Hi,
I am using MERGE statement to update as well as insert rows on ta table.
I have a data like in a table 'TABLEA' as 10 20 30 ABCD
I want to update the table using 10 20 30 DEFG but i want the old data i.e 10 20 30 ABCD
to store in a History table i.e TABLEA_H.
Is there any way to store the data
Any help will be needful for meHi,
Trigger usage may affect the performance as we are handling Production environment.
is there any way to implement the scenario without using Triggers?
Any help will be needful for me -
Problem in merge statement -ORA-27432 Step does not exist for chain
Hi
I m getting ORA-27432 Step does not exist for chain error in merge statement.Please explain the same.
MERGE INTO fos.pe_td_hdr_sd B
USING (
SELECT ACTIVE, ADDUID, ADDUIDTIME,TDKEY FROM pe.pe_td_hdr
WHERE (adduidtime like '20070104%' or edituidtime like '20070104%')
AND NVL(legacy_td,'N')<>'Y'
AND SUBSTR(adduidtime,1,4)='2007'
AND AMENDMENT_NO=0)A ON ( B.TDKEY = A.TDKEY)
WHEN MATCHED THEN
UPDATE SET B.ACTIVE=A.ACTIVE,
B.ADDUID=A.ADDUID,
B.ADDUIDTIME=A.ADDUIDTIME
WHEN NOT MATCHED THEN
INSERT
B.ACTIVE,
B.ADDUID,
B.ADDUIDTIME)
VALUES(
A.ACTIVE,
A.ADDUID,
A.ADDUIDTIME)This query is a short version of the main query.It is same but having 180 columns in original table.What version of Oracle are you using? This message does not appear in my 10.1 Error Messages document, but the other messages in that range seem to be about DBMS_SCHEDULER.
Are you using scheduler somewhere around where you are getting the error message?
John -
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
-
Photoshop/illustrator transfer issues and error
I've transferred my photoshop and illustrator from my old mac to another by using time machine, and I deregistered my old accounts on my old laptop to use it on my new set-up, but unfortunately I keep receiving 150:30 errors. I've tried scouring the
-
Observed Memory Leak When Invoking Controls in Workshop 8.1 sp3
Hello, We have developed a website using Bea Portal 8.1 sp3. In testing we have observed severe memory leaking problems which occur also in the production environment where WebLogic is not being run in debug mode. The troubleshooting was started by p
-
A custom filter or module, such as URLScan, restricts access to the file.
My btns do not work, I think becuase I added this: import flash.display.Sprite; import flash.net.navigateToURL; import flash.net.URLRequest; import flash.net.URLVariables; to my time line code in an attempt to get four btns to funtion out
-
Will not upgrade from snow leopard
have snow leopard 10.6.8 but it will not download mountain lion what could be the problem? Have and Imac
-
Parallel Port Interfacin​g
Hi, I'm working on an RF Design project. I'm working on LabVIEW to communicate with one of the evaluation boards used in the project. Parallel port interfacing is used and I would like to know the processes involved in parallel port interfacing. I'm