So far I have something like the following.
However I'm not sure what to do when I perform UPDATE - from another question here I found that you need to store the OUTPUT INSERTED result to a table because the update (or insert) may affect multiple rows? I tried using SCOPE IDENTITY but it return NULL on the UPDATE. Anyway if I use the table - then how do I get an individual integer that I can pass to the out parameter? Or do I have change the out parameter to a different type like a collection?
ALTER PROCEDURE [Data].[UpdateRecord]
@theValue decimal(4,2) = NULL,
@updatetime datetimeoffset(7),
@maxintervaltime datetimeoffset(7),
@recordID int = NULL output
AS
declare @mytable as TABLE
(
Id int
)
begin tran
if exists (select * from Data.theValue with (updlock,serializable) where Data.theValue.maxintervaltime = @maxintervaltime)
begin
update Data.theValue set theValue = @theValue, updatetime = @updatetime, maxintervaltime = @maxintervaltime
where Data.theValue.maxintervaltime = @maxintervaltime
-- OUTPUT INSERTED.id into @mytable (this line is wrong)
end
else
begin
insert into Data.theValue(theValue, updatetime, maxintervaltime) values(@theValue, @updatetime, @maxintervaltime);
SET @recordID = SCOPE_IDENTITY();
end
commit tran