alter procedure GetTempDataOfCancelProd (@ProdId varchar(500) = null)
as
begin
declare @SqlStr varchar(1000)
set @SqlStr = 'select a.Prodid as N''ProdId'',
a.ACT_ProdCancelId as N''CancelID'',
b.ACT_JobId as N''JobId'',
c.itemId as N''ItemId'',
sum(c.ALLOWSCRAPQTY) as N''AllowScrap''
into #tmpTable
from prodTable a
join ACT_JobTable b on a.ProdId = b.ProdId
join ACT_JobTrans c on b.ACT_JobId = c.ACT_JobId
where a.act_prodCancelId = ''cancel''
and b.ACT_JobRefType = 0
and c.ALLOWSCRAPQTY >0 ' + case
when @ProdId is null then ''
else 'and a.ProdId Like ''' + @ProdId + '''' end + '
group by a.prodId,a.ACT_ProdCancelId,b.act_jobId,c.ItemId
select * from #tmpTable
'
exec(@SqlStr)
--print @SqlStr
--if object_id(tempdb..#tmpTable) is not null
--begin
-- drop table #tmpTable
--end
end