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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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:
----- 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: 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... |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |