1. 程式人生 > >金蝶BIM開發 複雜 SQL更新語句

金蝶BIM開發 複雜 SQL更新語句

這幾天寫了幾個非簡單的SQL查詢語句,記錄下:

/*dialect*/update CT_EC_ServiceStaffTable set CFHandInjuryDate = SS.lastenddate, 
CFContrcatStatus = case when (SS.firstenddate>=getdate()) then '01' when 
(SS.lastenddate<getdate()) then '02' else '03' end from (select XX.fid as 
fid,max(X.planenddate)as lastenddate,min(X.planenddate)as firstenddate from 
CT_EC_ServiceStaffTable XX inner join (select b.CFLabourPersonID as fid,c.FEndDate as 
planenddate from T_EC_Roster a left join T_EC_RosterEntry b on a.fid = b.fbillid left join 
T_EC_Project c on c.fid = a.FProjectID union all select b.CFHeadmanNameID as fid,c.FEndDate 
as planedndate from T_EC_Roster a left join T_EC_ContructUnit b on b.fid = a.FWorkTeam left 
join T_EC_Project c on c.fid = a.FProjectID)X on X.fid = XX.fid group by xx.fid)SS  where 
CT_EC_ServiceStaffTable.fid = SS.fid

更新語句:

簡單:

update 表名字 set 欄位名 = '值'  where 條件

複雜點:

/*dialect*/ update CT_EC_ServiceStaffTable set CFHandInjuryDate = SS.lastenddate from 

(我這裡是一個子查詢,可以看出一張表

)SS

where CT_EC_ServiceStaffTable.fid = SS.fid

再複雜點

見第一個sql

主要表現一個case when語句

set CFContrcatStatus = case when (SS.firstenddate>=getdate()) then '01'
when (SS.lastenddate<getdate()) then '02'
else '03'
end