![]() |
The database management system (DBMS) stores, processes, and manipulates data in a database. All data in a database can be stored using the Insert command or updated using the Update command. These two data manipulation language (DML) commands play a key role in adding and maintaining the data. Sometimes we may need to use these commands in a single SQL Query or a sequence of SQL commands in a single process. So in PL/SQL, these two Insert and Update commands combined in a single process are called UPSERT. In this article, we will discuss in detail examples of the UPSERT concept of Update data if it already exists or Insert if it does not exist, in PL/SQL. UPSERT Operations in PL/SQLAn UPSERT is a data manipulation operation that combines two DML commands, UPDATE and INSERT. So UPSERT can be defined as a way to update data if it already exists or Insert a Row of Data if it does not exist already. There are different ways we can perform the UPSERT in PL/SQL. A few common methods can be as below: For example the different methods of Insert and Update, below the sample database and the initial data: create table EMPLOYEES ( Insert into Employees (empno,name,jobrole,technology,hiredate,salary) 1. Update … SQL%rowcount … Insert methodExample 1: UpdateIn the below example, we can that the where clause in update statement checks for empno 5 if it exists. Since this already exists, the existing data is updated to this employee data in table with the data provided to update. So the SQL%rowcount value will return 1 and the check for SQL%rowcount=0 will be false and the Insert statement inside the IF..Then bloc will not be executed. DECLARE Example 2: InsertThe example uses the same method as given in example 1 but now the Insert statement will be executed since the Empno 6 does not exist and the SQL%rowcount returns 0 and check for SQL%rowcount=0 returns True and the Insert statement inside IF…Then bloc will be executed. DECLARE If we run a Select query of the Employees table, below output can be seen Output: ![]() Data Updated and Inserted using SQL%rowcount check method In the output above we can see that data for empno 5 is updated with new Tecnhnology and Salary information as this exists already. And also a new employee record with empno 6 is added to the table as per the example 1 and 2 code above. 2. Select Count … If…Else…Example 1: UpdateDECLARE When we run the above code block, the record with empno 3 will be updated with new information in the Technology column as the iCount will return value 1 and n_count>0 will be True in the If condition and the Update statement under the IF block will be executed.. Example 2: InsertDECLARE When we run the above query, since there is no data for empno 7 in the table the n_count variable will return 0 value and based on the condition as there is 0 value, the if condition will return False and the Else part of the If statement above will be executed to Insert new data to the employees table. Output: ![]() Insert and Update using Count…If..Else method 3. MERGE methodThe merge method is used with 2 tables to Upate or Insert data between tables. We will create a backup table for the emloyees table. create table EMPLOYEES_Backup ( Example 1: Insert MERGE INTO EMPLOYEES_BackUp bk The above Merge command will Insert all data from Employees to Employees_backUp table as there will be no data in the EMPLOYEES_BackUp table initially. Example 2: Update and Insert In the Employees table below update and Insert are done to explain the Update and Insert Merge method: Update Employees set JobRole='Technical Manager', Salary=6000 where empno=7; When we run the Merge command again as below, the updated and Inserted data are added to the Employee_Backup table from Employees table in a single execution. MERGE INTO EMPLOYEES_BackUp bk Output: If we look at the Employees Backup table you can see the below result of data updated and inserted in the EMPLOYEES_BackUp table as per the above merge command: ![]() Merge command example result ConclusionIn this article we have discussed about the UPSERT concept of Updating and Inserting data in a database table in PL/SQL. Three methods of update if exists else Insert operation was explained in these 3 methods, with MERGE option of data update and/or Insert in a single execution. From these methods any method can be used based on need and need of the data being updated or inserted. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |