[{"data":1,"prerenderedAt":574},["ShallowReactive",2],{"blog-\u002Fblog\u002Fsql\u002F01-merge\u002Fmerge_statement":3},{"id":4,"title":5,"body":6,"date":562,"description":563,"extension":564,"meta":565,"navigation":330,"path":566,"seo":567,"stem":568,"tags":569,"__hash__":573},"blog\u002Fblog\u002Fsql\u002F01-Merge\u002FMerge_Statement.md","Data synchronization with Merge statement",{"type":7,"value":8,"toc":557},"minimark",[9,13,18,26,29,36,38,43,50,63,69,189,197,272,274,299,302,306,309,427,431,434,550,553],[10,11,12],"p",{},"During your whole career as a software engineer, you will have to solve many business problems that will require you to write complex SQL's. You may need to improve your SQL skills by learning different functions\u002Ffeatures a database provide. In these series of article i will share some advance SQL statements that can be utilized to write more efficient SQL statements.",[14,15,17],"h1",{"id":16},"merge-statement","MERGE Statement",[10,19,20,21,25],{},"A merge statement can be used to insert, update and delete data from a table at same time. The ",[22,23,24],"code",{},"MERGE"," statement require a target table and a source table to match the records. If the match occurs you can update or delete the data, otherwise you can insert the missing rows.",[27,28],"hr",{},[10,30,31],{},[32,33],"img",{"alt":34,"src":35},"","\u002Fblog\u002Fsql\u002F01-Merge\u002Fdiagram.png",[27,37],{},[39,40,42],"h2",{"id":41},"real-world-scenario-to-understand-use-of-merge-statement","Real world scenario to understand use of MERGE statement",[10,44,45,46,49],{},"Suppose you have a distributed system that is composed of several services running independently. One of the service is responsible to publish Employee details on daily basis publishing information like new joiners, employees who resigned and updates in salary information etc. Regardless of the mechanism let just consider you receive the daily updated data and saved it in a temporary table ",[22,47,48],{},"Employee_Daily_Updates"," (source table). The application has to make sure that the updated employee information received should become part of the actual employee data (target table) in your database. Following  requirements should be met.",[51,52,53,57,60],"ol",{},[54,55,56],"li",{},"If a new employee has joined then it should be added into the actual employee table",[54,58,59],{},"if there is an update on employee information (email, salary, manager) then we should update the data accordingly.",[54,61,62],{},"if the employee left the company, then we should delete it, in this case let just suppose the department of the employee will be empty.",[10,64,65],{},[66,67,68],"strong",{},"Let us consider Employee Table (Target Table) has following Schema and initial Data",[70,71,72,100],"table",{},[73,74,75],"thead",{},[76,77,78,82,85,88,91,94,97],"tr",{},[79,80,81],"th",{},"Employee_Id",[79,83,84],{},"First_Name",[79,86,87],{},"Last_Name",[79,89,90],{},"Hire_Date",[79,92,93],{},"Salary",[79,95,96],{},"Manager_Id",[79,98,99],{},"Department_Id",[101,102,103,126,147,168],"tbody",{},[76,104,105,109,112,115,118,121,124],{},[106,107,108],"td",{},"1",[106,110,111],{},"Abdul",[106,113,114],{},"Rehman",[106,116,117],{},"11-Jan-2019",[106,119,120],{},"1000",[106,122,123],{},"4",[106,125,108],{},[76,127,128,131,134,137,140,143,145],{},[106,129,130],{},"2",[106,132,133],{},"Usman",[106,135,136],{},"Ali",[106,138,139],{},"15-May-2020",[106,141,142],{},"2000",[106,144,123],{},[106,146,108],{},[76,148,149,152,155,158,161,164,166],{},[106,150,151],{},"3",[106,153,154],{},"Owais",[106,156,157],{},"Ahmed",[106,159,160],{},"23-Nov-2022",[106,162,163],{},"3000",[106,165,130],{},[106,167,108],{},[76,169,170,172,175,178,181,184,187],{},[106,171,123],{},[106,173,174],{},"Tahir",[106,176,177],{},"Khan",[106,179,180],{},"23-Nov-2015",[106,182,183],{},"5000",[106,185,186],{},"null",[106,188,108],{},[10,190,191],{},[66,192,193,194,196],{},"And consider data we have in ",[22,195,48],{}," (Source Table) is as follows.",[70,198,199,217],{},[73,200,201],{},[76,202,203,205,207,209,211,213,215],{},[79,204,81],{},[79,206,84],{},[79,208,87],{},[79,210,90],{},[79,212,93],{},[79,214,96],{},[79,216,99],{},[101,218,219,235,252],{},[76,220,221,223,225,227,229,231,233],{},[106,222,108],{},[106,224,111],{},[106,226,114],{},[106,228,117],{},[106,230,120],{},[106,232,123],{},[106,234,186],{},[76,236,237,239,241,243,245,248,250],{},[106,238,130],{},[106,240,133],{},[106,242,136],{},[106,244,139],{},[106,246,247],{},"2500",[106,249,123],{},[106,251,108],{},[76,253,254,257,260,263,266,268,270],{},[106,255,256],{},"5",[106,258,259],{},"Danish",[106,261,262],{},"Ikram",[106,264,265],{},"18-Sep-2023",[106,267,163],{},[106,269,123],{},[106,271,108],{},[27,273],{},[275,276,277,287,293],"ul",{},[54,278,279,280,282,283,286],{},"The first record  above employee_id equals 1 highlights the employee has left the company ",[22,281,99],{}," is null (",[66,284,285],{},"DELETE case",").",[54,288,289,290,286],{},"The second reocrd above employee_id equals 2,  highlights the employee Salary is updated (from 2000 to 2500) (",[66,291,292],{},"UPDATE case",[54,294,295,296,286],{},"The third reocrd above employee_id equals 5,  highlights the employee is a new joiner (",[66,297,298],{},"INSERT case",[10,300,301],{},"To fulfill these requirements either we can write seperate SQL statements for INSERT, UPDATE and DELETE or we can use a better alternative i.e. MERGE.",[39,303,305],{"id":304},"merge-statement-syntax","MERGE Statement Syntax",[10,307,308],{},"The baic syntax for writing a MERGE statement is as follow for Oracle Database.",[310,311,315],"pre",{"className":312,"code":313,"language":314,"meta":34,"style":34},"language-sql shiki shiki-themes github-light github-dark","MERGE   INTO    *target_table* as *table_alias*\n\nUSING   (table|view|query)   src\n\nON  ( join codition )\n\nWHEN MATCHED THEN\n\n    UPDATE SET\n        column_name_1 = value1,\n        column_name_2 = value1\n\n    DELETE (condition)\n\nWHEN NOT MATCHED THEN\n\ninsert (columns)\n\nvalues (values);\n\n","sql",[22,316,317,325,332,338,343,349,354,360,365,371,377,383,388,394,399,405,410,416,421],{"__ignoreMap":34},[318,319,322],"span",{"class":320,"line":321},"line",1,[318,323,324],{},"MERGE   INTO    *target_table* as *table_alias*\n",[318,326,328],{"class":320,"line":327},2,[318,329,331],{"emptyLinePlaceholder":330},true,"\n",[318,333,335],{"class":320,"line":334},3,[318,336,337],{},"USING   (table|view|query)   src\n",[318,339,341],{"class":320,"line":340},4,[318,342,331],{"emptyLinePlaceholder":330},[318,344,346],{"class":320,"line":345},5,[318,347,348],{},"ON  ( join codition )\n",[318,350,352],{"class":320,"line":351},6,[318,353,331],{"emptyLinePlaceholder":330},[318,355,357],{"class":320,"line":356},7,[318,358,359],{},"WHEN MATCHED THEN\n",[318,361,363],{"class":320,"line":362},8,[318,364,331],{"emptyLinePlaceholder":330},[318,366,368],{"class":320,"line":367},9,[318,369,370],{},"    UPDATE SET\n",[318,372,374],{"class":320,"line":373},10,[318,375,376],{},"        column_name_1 = value1,\n",[318,378,380],{"class":320,"line":379},11,[318,381,382],{},"        column_name_2 = value1\n",[318,384,386],{"class":320,"line":385},12,[318,387,331],{"emptyLinePlaceholder":330},[318,389,391],{"class":320,"line":390},13,[318,392,393],{},"    DELETE (condition)\n",[318,395,397],{"class":320,"line":396},14,[318,398,331],{"emptyLinePlaceholder":330},[318,400,402],{"class":320,"line":401},15,[318,403,404],{},"WHEN NOT MATCHED THEN\n",[318,406,408],{"class":320,"line":407},16,[318,409,331],{"emptyLinePlaceholder":330},[318,411,413],{"class":320,"line":412},17,[318,414,415],{},"insert (columns)\n",[318,417,419],{"class":320,"line":418},18,[318,420,331],{"emptyLinePlaceholder":330},[318,422,424],{"class":320,"line":423},19,[318,425,426],{},"values (values);\n",[39,428,430],{"id":429},"merge-sql-for-employee-scenario","MERGE SQL For Employee Scenario",[10,432,433],{},"For our scenario the MERGE statement will be like this.",[310,435,437],{"className":312,"code":436,"language":314,"meta":34,"style":34},"MERGE   INTO    Employees   AS trg\n\nUSING   (SELECT * FROM Employee_Daily_Updates) AS   src\n\nON  (   trg.Employee_Id =   src.Employee_Id  )\n\nWHEM MATCHED THEN\n    UPDATE SET\n        TRG.SALARY = SRC.SALARY,\n        TRG.MANAGER_ID = SRC.MANAGER_ID\n\n    DELETE WHERE DEPARTMENT_ID IS NULL\n\nWHEN NOT MATCHED THEN\n\nINSERT values (src.Employee_Id,\n    src.First_Name,\n    src.Last_Name,\n    src.Hire_Date,\n    src.Salary,\n    src.Manager_Id,\n    src.Department_Id\n);\n",[22,438,439,444,448,453,457,462,466,471,475,480,485,489,494,498,502,506,511,516,521,526,532,538,544],{"__ignoreMap":34},[318,440,441],{"class":320,"line":321},[318,442,443],{},"MERGE   INTO    Employees   AS trg\n",[318,445,446],{"class":320,"line":327},[318,447,331],{"emptyLinePlaceholder":330},[318,449,450],{"class":320,"line":334},[318,451,452],{},"USING   (SELECT * FROM Employee_Daily_Updates) AS   src\n",[318,454,455],{"class":320,"line":340},[318,456,331],{"emptyLinePlaceholder":330},[318,458,459],{"class":320,"line":345},[318,460,461],{},"ON  (   trg.Employee_Id =   src.Employee_Id  )\n",[318,463,464],{"class":320,"line":351},[318,465,331],{"emptyLinePlaceholder":330},[318,467,468],{"class":320,"line":356},[318,469,470],{},"WHEM MATCHED THEN\n",[318,472,473],{"class":320,"line":362},[318,474,370],{},[318,476,477],{"class":320,"line":367},[318,478,479],{},"        TRG.SALARY = SRC.SALARY,\n",[318,481,482],{"class":320,"line":373},[318,483,484],{},"        TRG.MANAGER_ID = SRC.MANAGER_ID\n",[318,486,487],{"class":320,"line":379},[318,488,331],{"emptyLinePlaceholder":330},[318,490,491],{"class":320,"line":385},[318,492,493],{},"    DELETE WHERE DEPARTMENT_ID IS NULL\n",[318,495,496],{"class":320,"line":390},[318,497,331],{"emptyLinePlaceholder":330},[318,499,500],{"class":320,"line":396},[318,501,404],{},[318,503,504],{"class":320,"line":401},[318,505,331],{"emptyLinePlaceholder":330},[318,507,508],{"class":320,"line":407},[318,509,510],{},"INSERT values (src.Employee_Id,\n",[318,512,513],{"class":320,"line":412},[318,514,515],{},"    src.First_Name,\n",[318,517,518],{"class":320,"line":418},[318,519,520],{},"    src.Last_Name,\n",[318,522,523],{"class":320,"line":423},[318,524,525],{},"    src.Hire_Date,\n",[318,527,529],{"class":320,"line":528},20,[318,530,531],{},"    src.Salary,\n",[318,533,535],{"class":320,"line":534},21,[318,536,537],{},"    src.Manager_Id,\n",[318,539,541],{"class":320,"line":540},22,[318,542,543],{},"    src.Department_Id\n",[318,545,547],{"class":320,"line":546},23,[318,548,549],{},");\n",[10,551,552],{},"Look how easily we manage to fulfill all requirements using MERGE Statement instead of writing three different SQL's for INSERT, UPDATE, DELETE. I hope this article helped you in understanding MERGE statement, cheers :).",[554,555,556],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":34,"searchDepth":327,"depth":327,"links":558},[559,560,561],{"id":41,"depth":327,"text":42},{"id":304,"depth":327,"text":305},{"id":429,"depth":327,"text":430},"2023-02-01","State management in Angular","md",{},"\u002Fblog\u002Fsql\u002F01-merge\u002Fmerge_statement",{"title":5,"description":563},"blog\u002Fsql\u002F01-Merge\u002FMerge_Statement",[570,571,572],"SQL","SQL-Server","Oracle","POWW18IYyevJGxlfvt83vz1V6nimMW93bCGKPE5j1OY",1782221323127]