I’ve a table called data with columns ip, report_date, group, value. The primary key is ip, report_date and group together.
When the table is empty and I run the statement below there is nothing inserted. What’s wrong with my statement?
When there is a match the record is updates according plan…
MERGE bc_data2 AS Target
USING (SELECT ip, report_date, group, value FROM bc_data2 As b
WHERE b.ip = '1.1.1.2'
AND b.report_date = '2/29/2012'
AND b.group = 'EPO-Client-Update') AS Source
ON (Target.ip = Source.ip
AND Target.frequency = Source.frequency
AND Target.report_date = Source.report_date
AND Target.service = Source.service
AND Target.proxy_service = Source.proxy_service
AND Target.proxy = Source.proxy
AND Target.service_group = Source.service_group)
WHEN MATCHED THEN
UPDATE SET Target.value = Target.value + 1
WHEN NOT MATCHED BY Target THEN
INSERT (ip, report_date, group, value)
VALUES ('1.1.1.2', '2/29/2012', 'EPO-Client-Update', 119437142);
You don’t add any rows because your
usingclause is not returning any rows. Put your constants in theusingclause with column aliases and use the fields in thewhen not matchedSomething like this with a couple of fields removed simplicity.