如何在UPDATE查询中使用JOIN

时间:2010-12-02 09:44:05

标签: sql-server sql-server-2005 tsql join sql

我在加入时非常弱,我需要知道如何使用连接转换以下查询。这个查询在子查询中运行良好,但我需要转换它并使用它进行连接。

update dbo.TFIN_Stmt_Line_Item_Dtl 
set Stmt_Line_Item_Dtl_Desc='Changes from Baseline' 
where Stmt_Line_Item_Dtl_Desc='Increased Hours' 
and Stmt_Line_Item_Id 
in (select tmt_Line_Item_Ref_Id 
    from dbo.TFIN_Stmt_Line_Item 
    where Stmt_Line_Item_Type_Id =
        (select Stmt_Line_Item_Type_Id  
         from dbo.TFIN_Stmt_Line_Item_Type 
         where Stmt_Line_Item_Desc like 'ABOX/RBOX Amendment 18 and 20'))

如果有任何身体可以帮助我。

由于

2 个答案:

答案 0 :(得分:3)

以下查询使用连接来更新您的表,但如果它更快还有待观察。

您可以发布查询计划,以便我们查看

update  dbo.TFIN_Stmt_Line_Item_Dtl 
set     Stmt_Line_Item_Dtl_Desc = 'Changes from Baseline' 
from    dbo.TFIN_Stmt_Line_Item_Dtl dtl
        inner join dbo.TFIN_Stmt_Line_Item li on li.tmt_Line_Item_Ref_Id = dtl.Stmt_Line_Item_Id
        inner join dbo.TFIN_Stmt_Line_Item_Type lit on lit.Stmt_Line_Item_Type_Id = li.Stmt_Line_Item_Type_Id
where   dtl.Stmt_Line_Item_Dtl_Desc='Increased Hours' 
        and lit.Stmt_Line_Item_Desc like 'ABOX/RBOX Amendment 18 and 20'

答案 1 :(得分:1)

update dbo.TFIN_Stmt_Line_Item_Dtl 
set Stmt_Line_Item_Dtl_Desc='Changes from Baseline' 
where Stmt_Line_Item_Dtl_Desc='Increased Hours' 
and Stmt_Line_Item_Id 
in (select tmt_Line_Item_Ref_Id 
    from dbo.TFIN_Stmt_Line_Item Item
    JOIN dbo.TFIN_Stmt_Line_Item_Type Type
    on Type.Stmt_Line_Item_Type_Id = Item.Stmt_Line_Item_Type_Id
    and Type.Stmt_Line_Item_Desc like 'ABOX/RBOX Amendment 18 and 20')

update dbo.TFIN_Stmt_Line_Item_Dtl 
set Stmt_Line_Item_Dtl_Desc = 'Changes from Baseline' 
from dbo.TFIN_Stmt_Line_Item_Dtl Detail
JOIN dbo.TFIN_Stmt_Line_Item Item
on Item.tmt_Line_Item_Ref_Id = Detail.Stmt_Line_Item_Id
JOIN dbo.TFIN_Stmt_Line_Item_Type Type
on Type.Stmt_Line_Item_Type_Id = Item.Stmt_Line_Item_Type_Id
and Type.Stmt_Line_Item_Desc like 'ABOX/RBOX Amendment 18 and 20')
where Detail.Stmt_Line_Item_Dtl_Desc = 'Increased Hours'