I have one table where I want to check if record exists leave it alone, if not insert new row and update previous row. I am wondering if I can use merge here like below ?
CREATE TABLE a
(keycol INT PRIMARY KEY,
col1 INT NOT NULL,
col2 INT NOT NULL,
col3 INT NOT NULL);
INSERT INTO a VALUES (1,0,0,0),(2,0,0,0);
MERGE INTO a
USING select 1 from a where col1 = 3
WHEN NOT MATCHED THEN
UPDATE SET
col2 = 2,
col2 = 2,
col3 = 2
where col1 = 3
WHEN NOT MATCHED THEN
INSERT (keycol, col1, col2, col3)
VALUES (4, 0, 0, 0)
Thanks,
WHEN MATCHED- whenNOT MATCHEDyou won't have a row to update and will need to insertbcome from? you do not have aoncondition, what do you want to use to match rows between source and target?