I want to insert the not matched output to two tables. When I try to insert the output of the stored procedure it takes all the updated values as well to the new table not only the not matched values to the 'table'.
ALTER PROCEDURE [dbo].[identify]
AS
BEGIN
SET NOCOUNT ON;
MERGE INTO [test].[dbo].[warehouse] AS dim
USING [test].[dbo].[staging] AS stg
ON dim.[first_name] = stg.first_name
WHEN MATCHED THEN
UPDATE SET
dim.[first_name] = stg.first_name,
dim.last_name = stg.last_name,
dim.created_date = stg.created_date,
dim.modified_date = stg.modified_date,
dim.gender = stg.gender
WHEN NOT MATCHED THEN
INSERT(first_name, last_name, created_date, modified_date, gender)
VALUES(first_name, last_name,created_date, modified_date,gender)
OUTPUT Inserted.first_name, Inserted.last_name INTO test (first_name,last_name);
END
Here a table variable
@UpdatedRowsis declared which records the action performed and can then be used to insert into multiple tables as required.