IT Community - Software Programming, Web Development and Technical Support

Update / Insert in a single query

This is a discussion on Update / Insert in a single query within the Database Support forums, part of the Web Development category; We can able to perform insert or update in a single query using MERGE statement in oracle. This merge command ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 07-25-2007, 07:03 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Update / Insert in a single query

We can able to perform insert or update in a single query using MERGE statement in oracle. This merge
command is also called as UPSERT.

Oracle
------
DROP TABLE test;
CREATE TABLE test (sno INTEGER PRIMARY KEY, name VARCHAR(100), age INTEGER);
INSERT INTO test VALUES (1, 'Raj',18);
INSERT INTO test VALUES (2, 'Mohan',20);
INSERT INTO test VALUES (3, 'Suresh',19);

-- This query will check the table "test" whether the SNO(4) already exists in the tables, if exists
then will do update otherwise will insert the details into table.

MERGE INTO test t1
USING (SELECT 4 SNO,'John' Name, 20 Age FROM DUAL ) t2
ON (t1.SNO = t2.SNO)
WHEN MATCHED THEN -- if the record already exists just update the name
UPDATE SET t1.name = t2.name
WHEN NOT MATCHED THEN -- If the record not available means, insert the record into the table.
INSERT (sno, name, age) VALUES (t2.sno, t2.name, t2.age);

Quote:
SELECT * FROM test;
-----------------------
sno name age
-----------------------
1 Raj 18
2 Mohan 20
3 Suresh 19
4 John 20
----------------------
MySQL
-----
In MySQL we can use INSERT statment to act as UPSERT(UPDATE/INSERT) comand.

Refer the below given example.

DROP TABLE test;
CREATE TABLE test (sno INTEGER PRIMARY KEY, name VARCHAR(100), age INTEGER);
INSERT INTO test VALUES (1, 'Raj',18);
INSERT INTO test VALUES (2, 'Mohan',20);
INSERT INTO test VALUES (3, 'Suresh',19);


-- If the record already exists then just update the record as it's otherwise insert new row
INSERT INTO test(sno,name,age) VALUES(4,'Mohan', 20)
ON DUPLICATE KEY UPDATE sno = sno, name =name, age = age;

SELECT * FROM test;
PHP Code:
-----------------------
sno    name    age    
-----------------------
1    Raj    18    
2    Mohan    20    
3    Suresh    19    
4    Mohan    20    
---------------------- 
-- If the record already exists then update the existing record values otherwise insert new row
INSERT INTO test(sno,name,age) VALUES(4,'Mohan', 20)
ON DUPLICATE KEY UPDATE sno = sno+1, name ='kumar', age = 29

SELECT * FROM test;

----------------------
sno name age
----------------------
1 Raj 18
2 Mohan 20
3 Suresh 19
5 kumar 29
----------------------


Is there any fucntion available in Sql Server like this ?
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-25-2007, 08:17 AM
Murali Murali is offline
D-Web Master
 
Join Date: Feb 2007
Location: India-Chennai.
Posts: 386
Murali is on a distinguished road
Send a message via AIM to Murali
Default Re: Update / Insert in a single query

Hi,

Mee too want to know iss that MERGE INTO Command available in MYSQL?
__________________
-Murali..
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert data from a csv file into a SQL server database? $enthil Database Support 7 05-01-2008 10:45 PM
Insert Multiple records using single Insert Statement vadivelanshanmugam Database Support 0 03-04-2008 10:26 PM
How do I insert javascript in php code? itbarota PHP Programming 1 09-12-2007 01:08 AM
Single XML documents? devarajan.v XML and SOAP 1 08-11-2007 01:25 AM
What is Bulk Insert in sql? oxygen Database Support 7 08-03-2007 01:31 PM


All times are GMT -7. The time now is 06:02 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0