title: Stored Procedure IF Exist Update Else Insert, if match
body: <p>I got a lot of help form different articles, for which i thank you a lot.
  But, right now i have a case were i need your support namely related with an SQL procedure which not only is checking if data exists (for update or insert it, but i have to match it with other 2 tables to check if data is matching then insert the same row with different values for some columns on a separate table).</p>
  
  <p>I hope to be more explicit, so the example below I hope to help</p>
  
  <p>Main row data : <strong>db.Table1</strong></p>
  
  <pre>
  
  |rowID|PurchaseDate|ProducID  | ProductName |   CustomerID |    Qty |   UnitType|
  |row1 |09.09.2018  |206       | Prod1       |   1          |    10  |   bl.     |
  |row2 |09.09.2018  |207       | Prod2       |   2          |    15  |   bl.     |
  |row3 |12.09.2018  |203       | Prod5       |   5          |    5   |   lk.     |
  |row4 |15.09.2018  |207       | Prod2       |   6          |    10  |   lk.     |
  |row5 |20.09.2018  |207       | Prod2       |   8          |    3   |   Pk.     |
  |row6 |20.09.2018  |203       | Prod5       |   8          |    6   |   Pk.     |
  |row7 |20.09.2018  |205       | Prod0       |   2          |    5   |   J.      |
  
  </pre>
  
  <p>to match with: <strong>db.Table2</strong></p>
  
  <pre>
  |CustomerID|    CustomerName|
  |1         |    Customer1   |
  |2         |    Customer2   |
  |3         |    Customer3   |
  |4         |    Customer4   |
  |5         |    Customer5   |
  
      </pre>
  
  <p>to match with: <strong>db.Table3</strong></p>
  
  <pre>
  
  |ProducID|  ProductName|    SubProdNAME| |
  |205     |  Prod0      |    Prod101    |
  |205     |  Prod0      |    Prod202    |
  |204     |  Prod01     |    Prod1001   |
  |204     |  Prod01     |    Prod2002   |
  
      </pre>
  
  <p>to final table: <strong>db.TableFIN</strong></p>
  
  <pre>
  |rowID| PurchaseDate|ProducID|ProductName|CustomerID|Qty|UnitType |Stage|
  |row1 | 09.09.2018  |   206  |  Prod1    |  1       |10 |   bl.   | DONE|
  |row1 | 09.09.2018  |   206  |  Prod1    |  1       |10 |   bl.   | NONE|
  |row2 | 09.09.2018  |   207  |  Prod2    |  2       |15 |   bl.   | DONE|
  |row2 | 09.09.2018  |   207  |  Prod2    |  2       |15 |   bl.   | NONE|
  |row3 | 12.09.2018  |   203  |  Prod5    |  5       |5  |   lk.   | DONE|
  |row3 | 12.09.2018  |   203  |  Prod5    |  5       |5  |   lk.   | NONE|
  |row4 | 15.09.2018  |   207  |  Prod2    |  6       |10 |   lk.   | DONE|
  |row4 | 15.09.2018  |   207  |  Prod2    |  6       |0  |   lk.   | NONE|
  |row5 | 20.09.2018  |   207  |  Prod2    |  8       |3  |   Pk.   | DONE|
  |row5 | 20.09.2018  |   207  |  Prod2    |  8       |0  |   Pk.   | NONE|
  |row6 | 20.09.2018  |   203  |  Prod5    |  8       |6  |   Pk.   | DONE|
  |row6 | 20.09.2018  |   203  |  Prod5    |  8       |0  |   Pk.   | NONE|
  |row7 | 20.09.2018  |   205  |  Prod101  |  3       |5  |   bundle| DONE|
  |row7 | 20.09.2018  |   205  |  Prod101  |  3       |5  |   bundle| NONE|
  |row7 | 20.09.2018  |   205  |  Prod202  |  3       |5  |   bundle| DONE|
  |row7 | 20.09.2018  |   205  |  Prod202  |  3       |5  |   bundle| NONE|
  
      </pre>
  
  <p>So, basically what i need is to insert data by row depending on <strong>Stage</strong>, one row with stage <em>DONE</em> and second with <em>NONE</em> - plus, in case the consumerID it matches then <strong>Qty</strong> value it's equal in both cases, otherwise for <em>NONE</em> value = 0 and for <em>DONE</em> the original value.
  FOR <strong>ProducID</strong> if it matches the product, then we have to insert 4 rows. as on above table. Again matching consumerID &amp; Prod updating/inserting stage/values.</p>
  
  <p>YOur support, is highly appreciated.
  Thank you in advance!</p>