组装SQL 临时表 case null

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

转载于:https://www.cnblogs.com/perock/archive/2011/12/09/2282018.html

你可能感兴趣的:(组装SQL 临时表 case null)