0

我想更新从 PI 表中选择的三列,即 LCID、LCNo、AmendmentStatus,所以我编写了以下成功执行的更新查询:

UPDATE  #TempTableOne  
    SET     
            LCID=(SELECT ISNULL(ExportLCID,0) FROM ExportLC WHERE ExportLCID= (SELECT LCID FROM [PI] WHERE PIID=(SELECT PIID FROM Job WHERE JobID=TT.OrderID))),
            LCNo=(SELECT ISNULL(ExportLCNo,'') FROM ExportLC WHERE ExportLCID= (SELECT LCID FROM [PI] WHERE PIID=(SELECT PIID FROM Job WHERE JobID=TT.OrderID))),
            AmendmentStatus=(SELECT AmendmentStatus FROM ExportLC WHERE ExportLCID= (SELECT LCID FROM [PI] WHERE PIID=(SELECT PIID FROM Job WHERE JobID=TT.OrderID))),      

    FROM    #TempTableOne  AS TT
    WHERE TT.OrderType=3

但是是否有可能更新查询执行单个查询,例如:

Update  #TempTableOne    
   SET  SELECT LCID = ISNULL(ExportLCID,0) ,
        LCNo=ISNULL(ExportLCNo,''), 
        AmendmentStatus=AmendmentStatus 
  FROM  ExportLC 
  WHERE ExportLCID = (SELECT LCID 
                        FROM [PI] 
                       WHERE PIID=(SELECT PIID 
                                     FROM Job 
                                     WHERE JobID=TT.OrderID
                                   )
                       )
               )

因为我想加快我的查询

4

1 回答 1

3

将查询更改为不使用 JOINS 应该会更快:

Update  #TempTableOne    
   SET  LCID = ISNULL(lc.ExportLCID,0) ,
        LCNo=ISNULL(lc.ExportLCNo,''), 
        AmendmentStatus=lc.AmendmentStatus 
  FROM #TempTableOne tt  
  JOIN  Job on Job.JobID = TT.OrderID
  JOIN  [PI] on [PI].PIID = Job.PIID
  JOIN ExportLC lc on [PI].LCID = lc.ExportLCID 
于 2012-05-14T03:54:33.877 回答