I'm looking at creating a stored proc which is being accessed by multiple processes and wondering, between the two solutions, which is a better one or is there a third solution?
The goal is find the record that has the maximum value in the field status. The field status contains decimal values but defined as a varchar. Once the appropriate record is found, then update the same record (only one record can be updated by the stored proc).
The stored proc will return the cusip in the form of select statement.
Solution:01
Declare @Cusiptable(Cusip varchar(100));
UPDATE cusiptbl
SET processingdate = GetDate() ,
Status = 'In Progress',
batchid = @batchid_given,
Output Inserted.Cusip into @Cusiptable
From
(
select top(1) Cusip from cusiptbl where batchid = -1
order by cast(status as decimal(18,4)) desc
) Seconds
where cusiptbl.cusip = Seconds.cusip
select Cusip from @Cusiptable
Solution 02:
select top(1) @CusipToBeUpdated = Cusip from cusiptbl
with (UPDLOCK)
where batchid = -1
order by
(case when isnumeric(status) = 1 then
cast(status as decimal(18,7)) end)
desc
-- Update the status to given batchid and status as 'In Progress'
UPDATE cusiptbl
SET batchid = @batchid_given,
processingdate = GetDate() ,
Status = 'In Progress'
where cusiptbl.cusip= @CusipToBeUpdated
and batchid = -1
Select @CusipToBeUpdated Cusip