This Stored PROCEDURE get input and upsert into the table.
It's faster than do select count(1) before insert/update.
CREATE PROCEDURE [dbo].[PROC_Heartbeat_Save]
-- Add the parameters for the stored procedure here
@session_id varchar(50),
@user_id int,
@system_id varchar(32)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Saving Session info
MERGE [dbo].[Heartbeat] AS T
USING
(
SELECT @session_id
,@user_id
,@system_id
) AS S
( session_id
,user_id
,system_id
)
ON (T.session_id = S.session_id and T.user_id = S.user_id)
WHEN MATCHED THEN
UPDATE SET last_uptime = GETDATE()
WHEN NOT MATCHED THEN
INSERT
([session_id]
,[user_id]
,[system_id]
,[usb_serial]
,[version]
,[firmware_slot])
VALUES
(@session_id
,@user_id
,@system_id
,@usb_serial
,@version
,@firmware_slot)
;
END
No comments:
Post a Comment