0

誰かが会議スロットを要求できるようにするロジックを書いています。すべてのスロットは、会議の作成時にテーブルで事前に初期化されます (別のテーブル)。次に利用可能なスロットを自動的に取得する手順を作成する必要がありますが、レコードが同時実行性を維持し、ステートメントを同時に実行しても他のスロットが上書きされないようにする必要があります。

これが私がこれまでに持っているものですが、それを行うための最良の方法とは思えません。

DECLARE @MeetingSlotID int

BEGIN TRAN
    ;with ValidTeams as 
    (
      select teamcode
      from teamassignment with (nolock)
      where TeamType = 'A'
    )
    select top 1 @MeetingSlotID = meetingslotid 
    from ValidTeams v
      inner join meetings m with (nolock) on m.teamcode = v.teamcode
      inner join meetingslots s on m.meetingid = s.meetingid
    where isavailable = 'Y' agendaclosed = 'N'
    order by m.starttime, slotposition

    update meetingslots set IsAvailable = 'N', username = 'username' where meetingslotid = @MeetingSlotID
COMMIT TRAN
4

1 に答える 1

1

あなたは調査したいと思うでしょう:

(UPDLOCK、READPAST、ROWLOCK)

多分このようなもの:

 ;
WITH cte1 AS       
 (  
        Select TOP 1
            ord.OrderID
FROM   
    dbo.Orders ord WITH ( UPDLOCK, READPAST , ROWLOCK ) 
WHERE 
    ord.IsPestimisticLocked = 0
)


UPDATE ords 
SET    
    IsPestimisticLocked = 1
FROM dbo.Orders ords
    join cte1 cteAlias on ords.OrderID = cteAlias.OrderID
WHERE ord.IsPestimisticLocked = 0;

--------編集未テスト。

しかし、ここに私がすることの要点があります:

declare @RowsMarkedAsCheckedOutCount int

    BEGIN TRAN


    ;
    WITH cte1 AS       
     (  
        select top 1 meetingslotid 
        from 
            (
                select teamcode
                from teamassignment with (nolock)
                where TeamType = 'A'
            ) derived1
          inner join meetings m with (nolock) on m.teamcode = derived1.teamcode
          inner join meetingslots s WITH ( UPDLOCK, READPAST , ROWLOCK ) on m.meetingid = s.meetingid
        where isavailable = 'Y' agendaclosed = 'N'
        order by m.starttime, slotposition  
    )

    UPDATE meetingslots 
    SET    
        isavailable = 'N'
    FROM meetingslots mslots
        join cte1 cteAlias on mslots.MeetingSlotID = cteAlias.MeetingSlotID
    WHERE mslots.isavailable = 'Y';


Select @RowsMarkedAsCheckedOutCount = @@ROWCOUNT

    COMMIT TRAN

if (@RowsMarkedAsCheckedOutCount>0)
begin
print 'You got one !'
end

編集:

昔、完全な「UPDATE TOP」の例を書いたことを忘れていました。

http://granadacoder.wordpress.com/2009/07/06/update-top-n-order-by-example/

コードは次のとおりです。

———-START TSQL

if exists (select * from dbo.sysobjects where id = object_id(N’[dbo].[Television]‘) and OBJECTPROPERTY(id, N’IsUserTable’) = 1)
      BEGIN
            DROP TABLE [dbo].[Television]
      END
GO


CREATE TABLE [dbo].[Television] (
      TelevisionUUID [uniqueidentifier] not null default NEWSEQUENTIALID() , 
      TelevisionName varchar(64) not null , 
      TelevisionKey int not null , 
      IsCheckedOut bit default 0
)     
GO


ALTER TABLE dbo.Television ADD CONSTRAINT PK_Television_TelevisionUUID
PRIMARY KEY CLUSTERED (TelevisionUUID)
GO


ALTER TABLE dbo.Television ADD CONSTRAINT CK_Television_TelevisionName_UNIQUE 
UNIQUE (TelevisionName)
GO


set nocount on

declare @counter int
select @counter = 11000
declare @currentTVName varchar(24)
declare @TopSize int
select @TopSize = 10

while @counter > 10000  — this loop counter is ONLY here for fake data,….do not use this syntax for production code
begin

      select @currentTVName = ‘TV:     ‘+ convert(varchar(24) , @counter)

      INSERT into dbo.Television ( TelevisionName , TelevisionKey ) values ( @currentTVName , @counter)

      select @counter = @counter – 1      
end


select count(*) as TV_Total_COUNT from dbo.Television 

/*
–Does not Work!
Update TOP (10) dbo.Television
      Set IsCheckedOut = 1
FROM
      dbo.Television tv
ORDER BY tv.TelevisionKey
*/
declare @AuditTrail table ( TelevisionUUID uniqueidentifier , OldIsCheckedOut bit , NewIsCheckedOut bit )

;
WITH cte1 AS       
 (  SELECT 
      TOP (@TopSize) 

   TelevisionUUID , –&lt;<Note, the columns here must be available to the output
   IsCheckedOut        
      FROM   
            dbo.Television tv      
    WITH ( UPDLOCK, READPAST , ROWLOCK ) –&lt;<Optional Hints, but helps with concurrency issues   
      WHERE  
            IsCheckedOut = 0              
      ORDER BY
            tv.TelevisionKey DESC        
)
UPDATE cte1
      SET  IsCheckedOut = 1
output inserted.TelevisionUUID , deleted.IsCheckedOut , inserted.IsCheckedOut into @AuditTrail ( TelevisionUUID , OldIsCheckedOut , NewIsCheckedOut )
;
print ”
print ‘Newly Checked Out Items’
select * from dbo.Television tv where tv.IsCheckedOut <> 0

print ‘Output AuditTrail’
select * from @AuditTrail
print ‘Not checked out items’
select count(*) as TVCOUNTIsNOTCheckedOut from dbo.Television tv where tv.IsCheckedOut = 0
于 2013-04-30T18:16:58.560 に答える