我正在考虑创建一个由多个进程访问的存储过程,并想知道在这两种解决方案之间,哪个更好还是有第三种解决方案?
目标是在字段status中找到具有最大值的记录。字段状态包含十进制值,但定义为 varchar。一旦找到合适的记录,然后更新相同的记录(存储过程只能更新一条记录)。
存储过程将以 select 语句的形式返回 cusip。
解决方案: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
解决方案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