【问题标题】:SQL Server: using stored procedures to dynamically alter a sequence of orders based on certain conditionsSQL Server:使用存储过程根据特定条件动态更改订单序列
【发布时间】:2020-06-19 17:35:08
【问题描述】:

我有一个通过INSERT INTO 从视图中生成的表。数据如下所示:

Priority    ID  Status
1   108999_1_S010   Planned
1   108999_1_S020   Planned
1   108996_1_S030   Planned
1   108996_1_S035   Planned
1   108996_1_S040   Planned
1   108996_1_S050   Planned
1   108996_1_S060   Planned
1   108996_1_S070   Planned
1   108996_1_S080   Planned
1   108996_1_S090   Planned
1   108996_1_S100   Planned
1   108996_1_S110   Planned
1   108996_1_S120   Planned
2   108999_2_S010   Planned
2   108999_2_S020   Planned
2   108996_2_S030   Planned
2   108996_2_S035   Planned
2   108996_2_S040   Planned
2   108996_2_S050   Planned
2   108996_2_S060   Planned
2   108996_2_S070   Planned
2   108996_2_S080   Planned
2   108996_2_S090   Planned
2   108996_2_S100   Planned
2   108996_2_S110   Planned
2   108996_2_S120   Planned
3   108999_3_S010   Planned
3   108999_3_S020   Planned
3   108996_3_S030   Planned
3   108996_3_S035   Planned
3   108996_3_S040   Planned
3   108996_3_S050   Planned
3   108996_3_S060   Planned
3   108996_3_S070   Planned
3   108996_3_S080   Planned
3   108996_3_S090   Planned
3   108996_3_S100   Planned
3   108996_3_S110   Planned
3   108996_3_S120   Planned
4   108999_4_S010   Planned
4   108999_4_S020   Planned
4   108996_4_S030   Planned
4   108996_4_S035   Planned
4   108996_4_S040   Planned
4   108996_4_S050   Planned
4   108996_4_S060   Planned
4   108996_4_S070   Planned
4   108996_4_S080   Planned
4   108996_4_S090   Planned
4   108996_4_S100   Planned
4   108996_4_S110   Planned
4   108996_4_S120   Planned
5   110225_1_S010   Planned
5   110225_1_S020   Planned
5   110224_1_S030   Planned
5   110224_1_S035   Planned
5   110224_1_S040   Planned
5   110224_1_S050   Planned
5   110224_1_S060   Planned
5   110224_1_S070   Planned
5   110224_1_S080   Planned
5   110224_1_S090   Planned
5   110224_1_S100   Planned
5   110224_1_S110   Planned
5   110224_1_S120   Planned

Priority 是在使用视图上的 ROW_NUMBER() 函数插入时生成的。从ID 列中可以看到,任何给定顺序都有 14 个子站,因此每个优先级将有 14 个等效数字。这一切都很好,但是该表正在通过 PowerApps 和 Power Automate 不断更新。我对这张表有两个要求:

  • 如果一组新订单来自底层源视图,并且它们的 priority 是,例如 3,那么我需要将当前订单与 priority (3, 4, 5) 全部转换为(4, 5, 6)。
  • 如果特定ID 的status 列从“已计划”更改为“已完成”,则该行的Priority 变为0,并且该站的下一个Priority 订单转移到该位置。 (比如ID“108996_2_S030”的订单(priority为2)已经完成,则该订单的priority变为0,订单“108996_3_S030”变为2,后续订单全部变为@ 987654335@。

我想我需要两个单独的存储过程。由视图中的插入和status 列的更改触发。但是,我不确定如何编写它们,以免它们相互干扰。

最初,我打算在每次这些触发器之一时完全重新计算 priority,但需要注意的是,我们还需要能够通过 PowerAppss 手动更改 priority,所以我不想重置这些更改。

有人知道如何编写存储过程来为我实现结果吗?

【问题讨论】:

  • 如果一组新的订单以优先级 3 进入,并且几乎在同一时间,另一组以优先级 4 进入的订单,如果第二个更新语句(更新(4,5,6) 条记录)在第一个之前运行?在这种情况下,您可能会得到不同的结果,具体取决于首先运行的更新,这可能不是您想要的。
  • 很好,@dcp。这些更新声明将集中在一起,并且必须基于最高的priority。因此,如果优先级为 3 和 4,则 (3,4,5) 将变为 (5,6,7)。
  • 处理一组新订单听起来像是instead of insert 触发器可以优雅地管理的事情。如果新数据和现有数据在Priority 上存在冲突,那么它将调整现有行的Priority,然后执行insert。如果没有冲突,它可以简单地insert 新数据。我无法理解您的第二个案例Status“已完成”真正想要做什么。
  • @HABO,不幸的是,SQL 触发器不能很好地与 PowerApps 配合使用 :(。我现在必须使用存储过程。我正在考虑将“插入”结果存储在一个临时表中存储过程,然后使用它来构造变量以确定插入表的最后一个priority。然后我可以使用它来更新顺序超出该变量的任何顺序的priority。想法?
  • @HABO,我的问题的第一个要点是指订单的“流”。操作员应该处理第一个订单,然后是第二个订单,依此类推。但是,每个订单都在不同的子装配站单独处理。 “S030”作为一个站会先处理它,然后是“S035”,然后是“S040”等等。所以对于每个单独的工作站,我还想根据正在执行的订单来调整流程。

标签: sql-server tsql ssms powerapps power-automate


【解决方案1】:

这就是我目前所拥有的:

SELECT 
       ROW_NUMBER() OVER (PARTITION BY [Workstation] ORDER BY [Ship Date], [RootID], [Workstation]) as Priority
      ,bm.[Customer]
      ,[ProductionOrder]
      ,[RootID]
      ,[SalesOrder]
      ,[Line]
      ,[QtyID]
      ,[TotalQty]
      ,[Material]
      ,[Config]
      ,[Description]
      ,[Workstation]
      ,[ConfigGroup]
      ,[Business Unit]
      ,[StartDate]
      ,[FinishDate]
      ,[ShipDate]
INTO #OrderInsert
FROM 
[BoomsBuildMaster] as bm
INNER JOIN
[BoomsBuildScheduleTEST] as bs
ON bm.RootID = bs.[Sales Order] + '_' + bs.[Line No]
WHERE bm.ProductionOrder NOT IN (SELECT [ProductionOrder] FROM [BoomsBuilder])
AND bs.Status = 'Ready'

UPDATE [BoomsBuilder]
SET Priority = Priority + (SELECT (MAX(Priority) - MIN(Priority) + 1) FROM #OrderInsert)
WHERE Priority >= (SELECT MIN(Priority) FROM #OrderInsert)

INSERT INTO [BoomsBuilder]
(
       [Priority]
      ,[Customer]
      ,[ProductionOrder]
      ,[RootID]
      ,[SalesOrder]
      ,[Line]
      ,[QtyID]
      ,[TotalQty]
      ,[Material]
      ,[Config]
      ,[Description]
      ,[Workstation]
      ,[ConfigGroup]
      ,[Business Unit]
      ,[StartDate]
      ,[FinishDate]
      ,[ShipDate]
)
SELECT * FROM #OrderInsert;

这里发生了什么:当表[BoomsBuildScheduleTEST] 将一行更新为Status“准备好”时,将触发此存储过程。

假设同时输入三个新订单,优先级为(5、6、7)。然后当前的 (5, 6, 7) 订单将被推送到 (8, 9, 10),之后的任何订单都将被推送到 (11, 12, 13...)。然后将插入新的 (5, 6, 7)。

这适用于插入的单个订单,甚至是多个顺序优先的订单。但是,例如,如果添加了两个新订单并且优先级分别为 (3,7),则它将不起作用。不知道在这种情况下该怎么做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 2015-02-21
    相关资源
    最近更新 更多