假設遇到以下情形:(執行SQL更新語法)
update dbo.pre_case
set
pc_handle_ext=(select com_telext from common..comper cc
inner join common..orgcod co on cc.com_orgcd=co.org_orgcd
inner join common..depcod dp on cc.com_deptid=dp.dep_deptid
where com_empno=@handleno),
pc_service_ext=(select com_telext from common..comper cc
inner join common..orgcod co on cc.com_orgcd=co.org_orgcd
inner join common..depcod dp on cc.com_deptid=dp.dep_deptid
where com_empno=@handleno),
pc_service_dept=(select dep_deptname from common..comper cc
inner join common..orgcod co on cc.com_orgcd=co.org_orgcd
inner join common..depcod dp on cc.com_deptid=dp.dep_deptid
where com_empno=@handleno)
where pc_no=@id
發現有三部份相同的Select語法,
建議可改成:
declare @pc_handle_ext nvarchar(20);
declare @pc_service_ext nvarchar(20);
declare @pc_service_dept nvarchar(20);
select @pc_handle_ext=com_telext ,@pc_service_ext=com_telext ,
@pc_service_dept=dep_deptname
from common..comper cc
inner join common..orgcod co on cc.com_orgcd=co.org_orgcd
inner join common..depcod dp on cc.com_deptid=dp.dep_deptid
where com_empno=@handleno
--先將要修改的值存於SQL變數中,不用跑三次,一次就解決
update dbo.pre_case
set
pc_handle_ext=@pc_handle_ext,
pc_service_ext=@pc_service_ext,
pc_service_dept=@pc_service_dept
where pc_no=@id
之後再執行一次SQL更新的語法,將@變數值存入欄位裡即可
沒有留言:
張貼留言