2

我在 SQL Server Management Studio 2008 中,我想设置多个 OrderID 以通过此查询运行它们。我听说我必须使用 TempTable,但我不知道如何设置多个表变量。这是我使用的原始查询:

DECLARE @OrderID int
DECLARE @Status int

-- FF nummer
SET @OrderID = 134
-- Huidige status
SET @Status = 4

BEGIN TRANSACTION

UPDATE FF_Task
SET              tsk_FK_CurrentStatus = NULL
WHERE     (tsk_FK_Order = @OrderID)

UPDATE    FF_Order
SET              ord_FK_CurrentOrderStatus  = NULL
WHERE     (ord_PK = @OrderID)

DELETE FROM FF_StatusLog
WHERE     (stl_PK IN
     (SELECT     TOP (1) FF_StatusLog_1.stl_PK
     FROM          FF_StatusLog AS FF_StatusLog_1 INNER JOIN
     FF_Task ON FF_StatusLog_1.stl_FK_Task = FF_Task.tsk_PK
     WHERE      (FF_Task.tsk_FK_Order = @OrderID)
     ORDER BY FF_StatusLog_1.stl_PK DESC))

DELETE
FROM         FF_OrderStatusLog
WHERE     (osl_Status = @Status) AND (osl_FK_Order = @OrderID)                            

UPDATE FF_Task
SET
tsk_FK_CurrentStatus = (SELECT     TOP (1) FF_StatusLog_1.stl_PK
                        FROM          FF_StatusLog AS FF_StatusLog_1 INNER JOIN
                        FF_Task ON FF_StatusLog_1.stl_FK_Task = FF_Task.tsk_PK
                        WHERE      (FF_Task.tsk_FK_Order = @OrderID)
                        ORDER BY FF_StatusLog_1.stl_PK DESC)
                        WHERE     (tsk_FK_Order = @OrderID)

UPDATE    FF_Order
SET              ord_FK_CurrentOrderStatus  = (SELECT TOP 1 osl_Status FROM
                FF_OrderStatusLog WHERE (osl_FK_Order = @OrderID) ORDER BY osl_CreationDateTime DESC)
WHERE     (ord_PK = @OrderID)
COMMIT TRANSACTION

谢谢你。

4

1 回答 1

2

您可以使用表变量或临时表

同样在 SQLServer2008+ 中,您可以将表值参数传递给 SP 和 UDF。表值参数允许您以表格格式将数据行传递给 SP 和 UDF。要创建表值参数,您必须首先创建定义表结构的表类型。

DECLARE @TableOfParemeters TABLE(OrderID int, Status int)
INSERT @TableOfParemeters
VALUES(134, 4),
      (135, 5)

BEGIN TRANSACTION

UPDATE FF_Task
SET tsk_FK_CurrentStatus = NULL
WHERE tsk_FK_Order IN (SELECT OrderID FROM @TableOfParemeters)

UPDATE FF_Order
SET ord_FK_CurrentOrderStatus  = NULL
WHERE ord_PK IN (SELECT OrderID FROM @TableOfParemeters)

;WITH cte AS
 (
  SELECT *, ROW_NUMBER() OVER(PARTITION BY stl_PK ORDER BY stl_PK DESC) AS rn
  FROM FF_StatusLog stl INNER JOIN FF_Task ff ON stl.stl_FK_Task = ff.tsk_PK
  WHERE ff.tsk_FK_Order IN (SELECT OrderID FROM @TableOfParemeters)
  )
  DELETE cte
  WHERE rn = 1  

DELETE
FROM FF_OrderStatusLog
WHERE EXISTS (
              SELECT 1
              FROM @TableOfParemeters t
              WHERE osl_Status = t.Status AND osl_FK_Order = t.OrderID
              )

;WITH cte AS
 (
  SELECT stl.stl_PK, ff.tsk_FK_CurrentStatus,
         ROW_NUMBER() OVER(PARTITION BY stl_PK ORDER BY stl_PK DESC) AS rn
  FROM FF_StatusLog stl INNER JOIN FF_Task ff ON stl.stl_FK_Task = ff.tsk_PK
  WHERE ff.tsk_FK_Order IN (SELECT OrderID FROM @TableOfParemeters)
  )
  UPDATE cte
  SET tsk_FK_CurrentStatus = stl_PK
  WHERE rn = 1

;WITH cte AS
 (
  SELECT o.ord_FK_CurrentOrderStatus, l.osl_Status,
         ROW_NUMBER() OVER(PARTITION BY osl_Status ORDER BY osl_CreationDateTime DESC) AS rn
  FROM FF_Order o INNER JOIN FF_OrderStatusLog l ON o.ord_PK = l.osl_FK_Order
  WHERE l.osl_FK_Order IN (SELECT OrderID FROM @TableOfParemeters)
  )
  UPDATE cte
  SET tsk_FK_CurrentStatus = osl_Status
  WHERE rn = 1 

COMMIT TRANSACTION
于 2013-04-18T11:16:14.560 回答