3

複数のプロセスからアクセスされるストアド プロシージャの作成を検討していますが、2 つのソリューションのうち、どちらが優れているか、または 3 つ目のソリューションがあるかを考えています。

目標は、フィールドstatusで最大値を持つレコードを見つけることです。フィールドstatusには 10 進数値が含まれていますが、varchar として定義されています。適切なレコードが見つかったら、同じレコードを更新します (ストアド プロシージャで更新できるレコードは 1 つだけです)。

ストアド プロシージャは、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
4

1 に答える 1