Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Merge with inserting into the other table

I want to create a merge that will compare two tables and insert not matched values into another third table or table variable something like this:

MERGE Assets AS target
USING (@id, @name)FROM Sales AS source (id, name) ON (target.id = SOURCE.id)
WHEN MATCHED THEN 
    UPDATE SET target.Status = @status, target.DateModified = SYSUTCDATETIME()
WHEN NOT MATCHED THEN 
    INSERT INTO @tableVar (id, name, status, dateModified)  
    VALUES (@id, @name, @status, SYSUTCDATETIME())

Can this be done or are there other methods?

like image 725
iLemming Avatar asked Feb 22 '12 15:02

iLemming


People also ask

How do I MERGE data from one table to another?

If the rows in both tables match up, you can merge the columns of one table with another—by pasting them in the first empty cells to the right of the table. In this case also, the table will increase to accommodate the new columns.

Can we insert data from one table to another table?

The INSERT INTO SELECT statement copies data from one table and inserts it into another table. The INSERT INTO SELECT statement requires that the data types in source and target tables match. Note: The existing records in the target table are unaffected.

Can you insert into two tables SQL?

The T-SQL function OUTPUT, which was introduced in 2005, can be used to insert multiple values into multiple tables in a single statement. The output values of each row that was part of an INSERT, UPDATE or DELETE operation are returned by the OUTPUT clause.

How do I MERGE two tables in SQL MERGE?

Multiple tables can be merged by columns in SQL using joins. Joins merge two tables based on the specified columns (generally, the primary key of one table and a foreign key of the other). Below is the generic syntax of SQL joins. USING (id);


2 Answers

You just cannot do this. MERGE operates on two tables only - source and target.

For your requirement, you need to e.g. use a CTE (Common Table Expression) to find the rows that don't match - and insert those into the third table.

Something like:

;WITH NonMatchedData AS
(
   -- adapt this as needed - just determine which rows match your criteria,
   -- and make sure to return all the columns necessary for the subsequent INSERT
   SELECT (columns)
   FROM dbo.SourceTable
   WHERE ID NOT IN (SELECT DISTINCT ID FROM dbo.TargetTable)
)
INSERT INTO dbo.ThirdTable(Col1, Col2, ....., ColN)
  SELECT Col1, Col2, ....., ColN
  FROM NonMatchedData
like image 140
marc_s Avatar answered Oct 20 '22 18:10

marc_s


You CAN do this very easily...

You can wrap the MERGE statement within a INSERT INTO FROM:
http://technet.microsoft.com/en-us/library/bb510625.aspx#sectionToggle2

-OR-

You can do it directly within the merge statement:

Quick example:

WHEN NOT MATCHED THEN
    DELETE
OUTPUT Deleted.* INTO dbo.MyTable;

This will insert the non-matches into your existing destination table. You can use the Updated, Inserted, Deleted v-tables to direct data other places.

like image 27
nm1984 Avatar answered Oct 20 '22 18:10

nm1984