I’m trying to merge “session” objects and get back the merged sessions (inserted or updated, to get their auto-increment primary key):
MERGE INTO [sessions] AS target
USING (
SELECT :created_at as created_at, :modified_at AS modified_at, :heuristic_id AS heuristic_id,
:account_key AS account_key, :source_key AS source_key, :account_name AS account_name,
:start_time AS start_time, :end_time AS end_time, :ip AS ip, :user_agent AS user_agent,
:location AS location, :country_code AS country_code, :region_code AS region_code
) AS source
ON target.heuristic_id = source.heuristic_id
AND target.end_time >= DATEADD(HOUR, -3, source.start_time)
WHEN MATCHED THEN
UPDATE SET
target.modified_at = source.modified_at,
target.end_time = source.end_time
WHEN NOT MATCHED THEN
INSERT (created_at, modified_at, heuristic_id, account_key, source_key, account_name, start_time,
end_time, ip, user_agent, location, country_code, region_code)
VALUES (source.created_at, source.modified_at, source.heuristic_id, source.account_key,
source.source_key, source.account_name, source.start_time, source.end_time, source.ip,
source.user_agent, source.location, source.country_code, source.region_code
)
OUTPUT
$action,
inserted.internal_key,
inserted.heuristic_id,
inserted.end_time;
"""
For some reason, I’m only getting back as output the last session merged, instead of all them. Why is that?