SQL Server 两条数据创建时自动关联

begin
update a set a.sglzbbh = b.sname+'-'+convert(char(200),(1+isnull(SUBSTRING(c.glh,CHARINDEX('-', c.glh)+1, LEN(c.glh)),0))) 
from workplan a join org_employee b on b.id = a.idowner 
join (select aa.idowner id,max(SUBSTRING(aa.sglzbbh, CHARINDEX('-', aa.sglzbbh)+1, LEN(aa.sglzbbh))) glh 
from workplan aa group by aa.idowner ) c on c.id = a.idowner
where a.id = '${targetID}'
--自动关联上周计划编号
--自动关联上周计划编号
update a set a.idszzb = b.id  
from workplan a join workplan b 
on left(a.sglzbbh,charindex('-',a.sglzbbh)-1)+'-'+convert(char(200),isnull(SUBSTRING(a.sglzbbh,CHARINDEX('-', a.sglzbbh)+1, LEN(a.sglzbbh)),0)-1) = b.sglzbbh 
where a.id = '${targetID}'
--自动上计划关联下周计划编号
update b set b.idxzjh = a.id  
from workplan a join workplan b 
on left(a.sglzbbh,charindex('-',a.sglzbbh)-1)+'-'+convert(char(200),isnull(SUBSTRING(a.sglzbbh,CHARINDEX('-', a.sglzbbh)+1, LEN(a.sglzbbh)),0)-1) = b.sglzbbh 
where a.id = '${targetID}'
end

 

posted @ 2019-05-17 10:17  RainHouse  阅读(450)  评论(0编辑  收藏  举报