Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Optimizing stored procedure

$
0
0

Hi,

This is my stored procedure,

Is there any way to avoid temptable to improve performance

CREATE procedure [dbo].[sp_GetMALReportDetails]

(@CategoryID INT = null, @StatusID INT = null, @UserTypeId int = null,

@UserName varchar(100)= null, @assignedTo varchar(200)=null, @priorityID int =null,

@queueId int =null)

AS

BEGIN

select distinct

    a.ID as [ID], p.Name as [Priority], c.Name as [Category],  q.Name as [Queue], a.AssignedTo as [AssignedTo],

    b.Prefix, b.CI,

    a.Name as [AppName],s.Name as [AppStatus], CONVERT(varchar(255), null) as [PrimaryAppOwner],

    ac.Comment as [LastComment], ct.Name as [CommentType], CONVERT(varchar(255), null) as [DivManager]

into #Results_tbl

from Application a(nolock)

inner join Queue q (nolock)

    On a.QueueID = q.ID

inner join Category c (nolock)

    on c.ID = a.CategoryID

left join ApplicationExtended b

    on a.ID = b.ApplicationID

inner join Status s (nolock)

    on s.ID = a.StatusID

left join ApplicationDateType adt (nolock)

    on a.ID = adt.ApplicationID

left join DateType dt (nolock)

    on adt.DateTypeID = dt.ID

left join Priority p (nolock)

    on a.PriorityID = p.ID    

left join ApplicationComment ac(nolock)

    on ac.ApplicationID = a.ID

left join CommentType ct (nolock)

    on ac.CommentTypeID = ct.ID

where a.StatusID = coalesce(@StatusID, a.StatusID)

and a.CategoryID = coalesce(@CategoryID, a.CategoryID)

--and a.AssignedTo = coalesce(@assignedTo, a.AssignedTo)

and a.PriorityID = coalesce(@priorityID, a.PriorityID)

and a.QueueID = coalesce(@queueId, a.QueueID)

if (@assignedTo is not null)

begin

    delete from #Results_tbl where [AssignedTo] != @assignedTo or [AssignedTo] is null

end

-- updating App Owner

update r set r.[PrimaryAppOwner] = aut.UserName

from #Results_tbl r (nolock)

left join ApplicationUserType aut (nolock)

    on r.[ID] = aut.ApplicationID

left join UserType ut (nolock)

    on ut.ID = aut.UserTypeID

where ut.Name = 'Primary App Owner'

-- updating Div Manager

update r set r.[DivManager] = aut.UserName

from #Results_tbl r (nolock)

left join ApplicationUserType aut (nolock)

    on r.[ID] = aut.ApplicationID

left join UserType ut (nolock)

    on ut.ID = aut.UserTypeID

where ut.Name = 'Div Manager'

if (@UserTypeId is not null)

begin

    if(@UserTypeId = 1)

    begin

          select *

          from #Results_tbl

          where [PrimaryAppOwner] = coalesce(@UserName,[PrimaryAppOwner])

          return

    end

    else if(@UserTypeId = 4)

    begin

          select *

          from #Results_tbl

          where [DivManager] = coalesce(@UserName,[DivManager])

          return

    end

end

select * from #Results_tbl

END

G



Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>