我已经构建了一个触发器,我试图按顺序保持优先级,并且不允许重复的优先级值。有一些事情要考虑。多个SQL UPDATE语句或单个CASE在里面?
- 用户可以自由形式的优先级。
- 没有什么从采摘相同的优先级的另一项阻止他们。
当输入一个值等于另一个值的新优先项目应优先考虑的优先级和其他应增加。
CREATE TRIGGER dbo.trg_Priority ON dbo.Stories AFTER INSERT,UPDATE,DELETE AS BEGIN SET NOCOUNT ON; -- Insert statements for trigger here DECLARE @StoryId INT DECLARE @OldLocation INT DECLARE @NewLocation INT SELECT @NewLocation = Priority, @StoryId = StoryId FROM INSERTED SELECT @OldLocation = Priority FROM DELETED IF @NewLocation = @OldLocation RETURN; IF @NewLocation IS NULL BEGIN UPDATE Stories SET Priority = Priority - 1 WHERE Priority > @OldLocation END IF @NewLocation > @OldLocation BEGIN UPDATE Stories SET Priority = Priority + 1 WHERE Priority >= @NewLocation AND StoryId <> @StoryId END IF @NewLocation < @OldLocation BEGIN UPDATE Stories SET Priority = Priority + 1 WHERE Priority >= @NewLocation AND Priority < @OldLocation AND StoryId <> @StoryId END END GO
所以如果有关注的领域随意说话,我没有测试过这触发了一大堆。我最终想知道的是,如果我应该尝试使用case语句将其转换为单个更新。 (如果甚至可能的话)
如果将它作为单个UPDATE
声明会更高效,我真的可以用一只手来搞清楚它!