使用多个Table变量在SQL中填充Local #temptable

时间:2013-04-18 10:21:13

标签: sql sql-server-2008

我在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

谢谢。

1 个答案:

答案 0 :(得分:2)

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

同样在SQLServer2008 +中,您可以将table-valued parameters传递给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