Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You never mentioned what DBMS you are using but if you are using SQL Server, one really good one is the SQL <code>MERGE</code> statement. See: <a href="http://www.mssqltips.com/tip.asp?tip=1704" rel="nofollow noreferrer">http://www.mssqltips.com/tip.asp?tip=1704</a></p> <blockquote> <p>The MERGE statement basically works as separate insert, update, and delete statements all within the same statement. You specify a "Source" record set and a "Target" table, and the join between the two. You then specify the type of data modification that is to occur when the records between the two data are matched or are not matched. MERGE is very useful, especially when it comes to loading data warehouse tables, which can be very large and require specific actions to be taken when rows are or are not present.</p> </blockquote> <p>Example:</p> <pre><code>MERGE Products AS TARGET USING UpdatedProducts AS SOURCE ON (TARGET.ProductID = SOURCE.ProductID) --When records are matched, update --the records if there is any change WHEN MATCHED AND TARGET.ProductName &lt;&gt; SOURCE.ProductName OR TARGET.Rate &lt;&gt; SOURCE.Rate THEN UPDATE SET TARGET.ProductName = SOURCE.ProductName, TARGET.Rate = SOURCE.Rate --When no records are matched, insert --the incoming records from source --table to target table WHEN NOT MATCHED BY TARGET THEN INSERT (ProductID, ProductName, Rate) VALUES (SOURCE.ProductID, SOURCE.ProductName, SOURCE.Rate) --When there is a row that exists in target table and --same record does not exist in source table --then delete this record from target table WHEN NOT MATCHED BY SOURCE THEN DELETE --$action specifies a column of type nvarchar(10) --in the OUTPUT clause that returns one of three --values for each row: 'INSERT', 'UPDATE', or 'DELETE', --according to the action that was performed on that row OUTPUT $action, DELETED.ProductID AS TargetProductID, DELETED.ProductName AS TargetProductName, DELETED.Rate AS TargetRate, INSERTED.ProductID AS SourceProductID, INSERTED.ProductName AS SourceProductName, INSERTED.Rate AS SourceRate; SELECT @@ROWCOUNT; GO </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload