A store procedure sample using caes ...when ...then...
create proc Pr_ToSolver_ds_in_item_loc
as
if exists(select * from OFG_SLV_DEV3.dbo.ds_in_item_loc)
delete from OFG_SLV_DEV3.dbo.ds_in_item_loc
insert into OFG_SLV_DEV3.dbo.ds_in_item_loc
(
item_name
,loc_name
,inventory_point
,buffer_time
,buffer_time_trans
,capable
,qualed
,prq_start_date
,prq_end_date
)
select
i.ItemName
,l.LocationName
,0
,il.BufferTime
,il.BufferTimeTrans
,il.isCapable
,il.IsQualed
,ltrim(str(b1.YearCode)) + case
when b1.WorkWeekCode <=9 then '0'+ltrim(str(b1.WorkWeekCode))
when b1.WorkWeekCode > 9 then ltrim(str(b1.WorkWeekCode))
end
as prg_start_date
,ltrim(str(b2.YearCode)) + case
when b2.WorkWeekCode <=9 then '0'+ltrim(str(b2.WorkWeekCode))
when b2.WorkWeekCode > 9 then ltrim(str(b2.WorkWeekCode))
end
as prg_end_date
from Item_Locations il
join Item i on il.ItemId=i.ItemId
join Location l on il.LocationId=l.LocationId
left join Bucket b1 on il.BucketId_PRQFrom=b1.BucketId
left join Bucket b2 on il.BucketId_PRQTo=b2.BucketId