dbTalk Databases Forums  

Oracle Merge Statement -sample query

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss Oracle Merge Statement -sample query in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
idey
 
Posts: n/a

Default Oracle Merge Statement -sample query - 05-20-2008 , 04:59 AM






Hi All,
I have a simple question but i am in a fix coz of it.
This is regarding Oracle Merge statement.

The question is:

I have a table say

Employee
- Name varchar2(20)
- ID pk number
- salary number

I want to check whether ID = 20 exists or not. If yes, then update
salary = salary +10000;
else insert the row with default values.

Can someone give me the SQL for this? I have tried all the seemingly
normal approaches.

Regards,
Indranil.

Reply With Quote
  #2  
Old   
fitzjarrell@cox.net
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-20-2008 , 07:39 AM






On May 20, 4:59*am, idey <dey.indra... (AT) gmail (DOT) com> wrote:
Quote:
Hi All,
I have a simple question but i am in a fix coz of it.
This is regarding Oracle Merge statement.

The question is:

I have a table say

Employee
- Name varchar2(20)
- ID pk number
- salary number

I want to check whether ID = 20 exists or not. If yes, then update
salary = salary +10000;
else insert the row with default values.

Can someone give me the SQL for this? I have tried all the seemingly
normal approaches.

Regards,
Indranil.
Then show us what you've tried, as this appears to be an assignment
for school. We won't do your homework for you , but we WILL offer
assistance when we see what you've tried.


David Fitzjarrell


Reply With Quote
  #3  
Old   
fitzjarrell@cox.net
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-20-2008 , 07:39 AM



On May 20, 4:59*am, idey <dey.indra... (AT) gmail (DOT) com> wrote:
Quote:
Hi All,
I have a simple question but i am in a fix coz of it.
This is regarding Oracle Merge statement.

The question is:

I have a table say

Employee
- Name varchar2(20)
- ID pk number
- salary number

I want to check whether ID = 20 exists or not. If yes, then update
salary = salary +10000;
else insert the row with default values.

Can someone give me the SQL for this? I have tried all the seemingly
normal approaches.

Regards,
Indranil.
Then show us what you've tried, as this appears to be an assignment
for school. We won't do your homework for you , but we WILL offer
assistance when we see what you've tried.


David Fitzjarrell


Reply With Quote
  #4  
Old   
fitzjarrell@cox.net
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-20-2008 , 07:39 AM



On May 20, 4:59*am, idey <dey.indra... (AT) gmail (DOT) com> wrote:
Quote:
Hi All,
I have a simple question but i am in a fix coz of it.
This is regarding Oracle Merge statement.

The question is:

I have a table say

Employee
- Name varchar2(20)
- ID pk number
- salary number

I want to check whether ID = 20 exists or not. If yes, then update
salary = salary +10000;
else insert the row with default values.

Can someone give me the SQL for this? I have tried all the seemingly
normal approaches.

Regards,
Indranil.
Then show us what you've tried, as this appears to be an assignment
for school. We won't do your homework for you , but we WILL offer
assistance when we see what you've tried.


David Fitzjarrell


Reply With Quote
  #5  
Old   
fitzjarrell@cox.net
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-20-2008 , 07:39 AM



On May 20, 4:59*am, idey <dey.indra... (AT) gmail (DOT) com> wrote:
Quote:
Hi All,
I have a simple question but i am in a fix coz of it.
This is regarding Oracle Merge statement.

The question is:

I have a table say

Employee
- Name varchar2(20)
- ID pk number
- salary number

I want to check whether ID = 20 exists or not. If yes, then update
salary = salary +10000;
else insert the row with default values.

Can someone give me the SQL for this? I have tried all the seemingly
normal approaches.

Regards,
Indranil.
Then show us what you've tried, as this appears to be an assignment
for school. We won't do your homework for you , but we WILL offer
assistance when we see what you've tried.


David Fitzjarrell


Reply With Quote
  #6  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-21-2008 , 07:31 AM



http://download.oracle.com/docs/cd/B...htm#SQLRF01606

--

Regards,
Frank van Bortel

Reply With Quote
  #7  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-21-2008 , 07:31 AM



http://download.oracle.com/docs/cd/B...htm#SQLRF01606

--

Regards,
Frank van Bortel

Reply With Quote
  #8  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-21-2008 , 07:31 AM



http://download.oracle.com/docs/cd/B...htm#SQLRF01606

--

Regards,
Frank van Bortel

Reply With Quote
  #9  
Old   
Frank van Bortel
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-21-2008 , 07:31 AM



http://download.oracle.com/docs/cd/B...htm#SQLRF01606

--

Regards,
Frank van Bortel

Reply With Quote
  #10  
Old   
idey
 
Posts: n/a

Default Re: Oracle Merge Statement -sample query - 05-22-2008 , 02:25 AM



On May 21, 5:31 pm, Frank van Bortel <frank.van.bor... (AT) gmail (DOT) com>
wrote:
Quote:
http://download.oracle.com/docs/cd/B.../b10759/statem...

--

Regards,
Frank van Bortel
Hi All,
Thanks for your replies. But I think you missed my point. The query I
am doing is not a school homework but quite a complex one and since I
have never used a Merge statement before I have sought your help with
an example.

The problem with Merge I think is that its trying to merge from a
source table/ view/ dataset into the taget table. My problem is when I
have a single table to work on i.e. source and target tables are same.
In this case the predicate inside the using clause filters all records
so no rows are returned.
If I do not use the predicate then there is a different problem
altogether.

--with predicate filter in using
MERGE INTO EMPLOYEE E
USING (SELECT D.ID, D.NAME, D.SALARY FROM employee D
WHERE D.ID = 80) S
ON (E.ID = S.ID)
WHEN MATCHED THEN UPDATE SET E.SALARY = S.SALARY + 10000
WHEN NOT MATCHED THEN INSERT (E.ID, E.NAME, E.SALARY)
VALUES (80,'ENAME',20000);

--without preidicate filter in using
MERGE INTO EMPLOYEE E
USING (SELECT D.ID, D.NAME, D.SALARY FROM employee D) S
ON (E.ID = S.ID)
WHEN MATCHED THEN UPDATE SET E.SALARY = S.SALARY + 10000
WHEN NOT MATCHED THEN INSERT (E.ID, E.NAME, E.SALARY)
VALUES (80,'ENAME',20000)
where S.id=80;


Both of them do not work.

There are other use cases when i use either when the record exists or
not and that doesnt seem to give the expected results either. I would
not want to bother you buys with them here unless you want my full
analysis too.

Kindly point out if i have written the query wrong

Thanks,
Indranil.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.