If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Stored Procedure Insert or Update

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-07-11, 04:54
stahorse1 stahorse1 is offline
Registered User
 
Join Date: Nov 2011
Posts: 13
Stored Procedure Insert or Update

Hi

Can anybody assist me with a store procedure code, I want to insert data in a database in this fashion,I want to check first if the record exist, if not Insert or else Update.
Reply With Quote
  #2 (permalink)  
Old 12-07-11, 06:35
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by stahorse1 View Post
I want to check first if the record exist, if not Insert or else Update.
No need to use a stored procedure, use the MERGE Statement instead.
Reply With Quote
  #3 (permalink)  
Old 12-07-11, 06:46
Mr.Vivek Mr.Vivek is offline
Registered User
 
Join Date: Dec 2011
Posts: 15
Insert or update

Use MERGE Statement.
E.g
CREATE TABLE CITY (CITY_ID NUMBER, CITY_NAME VARCHAR2(50 BYTE) ) ;

E.g

CREATE OR REPLACE
PROCEDURE ADD_OR_UPDATE_CITY
(
IN_CITY_ID IN NUMBER,
IN_CITY_NAME IN VARCHAR2
)
AS

BEGIN

BEGIN

MERGE INTO City ct
USING ( SELECT IN_CITY_ID AS CITY_ID,
IN_CITY_NAME AS CITY_NAME
FROM DUAL
)SRC
ON
(
ct.CITY_ID = SRC.CITY_ID
)
WHEN MATCHED THEN
UPDATE SET
ct.CITY_NAME = SRC.CITY_NAME
WHEN NOT MATCHED THEN
INSERT
(
ct.CITY_ID , ct.CITY_NAME
)
VALUES
(
SRC.CITY_ID ,SRC.CITY_NAME
);

END;

--Exeption handling
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
RAISE;
END ADD_OR_UPDATE_CITY;
Reply With Quote
  #4 (permalink)  
Old 12-07-11, 06:48
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Please use [code] tags the next time you post SQL scripts!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On