I am basically trying to do the thing from MYSQL where it is INSERT and ON DUPLICATE, etc.
However, I am getting an error with my ‘merge’ syntax. This stored procedure takes a variable “@ID” which is a guid and looks in the table to see if it exists already — if it does, insert a new entry, otherwise update the existing. I can’t figure out what is wrong here!
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE usp_DigitalTool_InsertUpdate2
(@ID uniqueidentifier, @TOOL_ID uniqueidentifier, @INPUT_ID uniqueidentifier)
AS
MERGE
INTO DigitalTool as target
USING (SElECT @ID,@TOOL_ID,@INPUT_ID) as source(id, tool_id, input_id)
ON (target.ID = source.id)
when matched then
update
set TOOL_ID = source.tool_id,
INPUT_ID = source.input_id
when not matched then
insert ( ID, TOOL_ID, INPUT_ID)
values ( NEWID(), source.tool_id, source.input_id)
END
GO
Help is appreciated. Thanks!
Using TSQL You can use the EXISTS keyword to determine whether a record is found as follows.