Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
willfindavid
Starting Member
27 Posts |
Posted - 2008-06-30 : 06:14:39
|
| select 0 DeviationID, PlanID , PlanName , ErrorDeviationCategoryID , ErrorDeviationCategoryName , IsWarningOrInformation , isnull(LineItemID, '') 'LineItemID' , ProjectErrorID, TaskName , Predecessortask , ErrorID , Error_Type , ErrorCode , ErrorDesc , ErrorSeverityID , ErrorSeverityName , case when ltrim(rtrim(Error_Type)) = 'T' Then dbo.PC_fnGetDeviationReportFields(LineItemID, 'U') Else UserName End 'UserName',case when Error_Type = 'T' Then dbo.PC_fnGetDeviationReportFields(LineItemID, 'D') Else DeliveryRole End DeliveryRole ,case when Error_Type = 'T' Then dbo.PC_fnGetDeviationReportFields(LineItemID, 'R') Else RequiredRoleEnd RequiredRole,DeviationRole ,Reason , Status,( select ErrorStatusDesc from ErrorStatus a where a.ErrorStatusID = Status) 'StatusName',isnull(UserID,'') UserID , isnull( dbo.PC_fnGetDeviationProcessPackage ( @ProjecTID, PlanID) , '') 'ProcessPackage' from @ProjectError where ltrim(rtrim(ProjectErrorID)) +'~'+ ltrim(rtrim(UserID)) not in ( select ltrim(rtrim(ProjectErrorID)) +'~'+ ltrim(rtrim(UserID)) from @DeviationReport_Saved ) and planid in ( select Items from dbo.split(@ProcessPlanID,',') )desc order by PlanID, ErrorCode desc |
|
|
willfindavid
Starting Member
27 Posts |
Posted - 2008-06-30 : 06:15:13
|
| The code marked in red causes the query to execute for a long time.It takes around 7 seconds to execute. |
 |
|
|
willfindavid
Starting Member
27 Posts |
Posted - 2008-06-30 : 06:15:46
|
| The PC_fnGetDeviationReportFields function is as follows:ALTER function [dbo].[PC_fnGetDeviationReportFields](@LineItemID bigint,@Data varchar(2))Returns varchar(2000)As Begindeclare @Result as varchar(2000)set @Result = ''if (@Data ='U') -- User NameBeginSelect @Result = case when @Result = ''then ltrim(rtrim(mas_user.s_useR_name))else@Result + ';'+ ltrim(rtrim(mas_user.s_useR_name))End from WOrkMgmtPlanLineItemsInner JoinWorkMgmtPlanTaskAllocationsOn WorkMgmtPlanTaskAllocations.PlanLineItemID = WOrkMgmtPlanLineItems.LineItemIDInner JoinMas_user Onmas_user.s_user_id = WorkMgmtPlanTaskAllocations.ResourceIDwhereWOrkMgmtPlanLineItems.LineItemID = @LineItemIDand WorkMgmtPlanTaskAllocations.IsActive = '1'EndElse if @Data = 'D'BeginSelect @Result = @Result + case when @Result = '' then ltrim(rtrim(RoleName)) else ';'+ ltrim(rtrim(RoleName)) end from (Select distinct ltrim(rtrim(RoleName))'RoleName'from EPFRoles Inner JoinEPFTaskRolesOnEPFRoles.RoleID = EPFTaskRoles.RoleIDInner JoinPCprocessPAckageTasksOnEPFTaskRoles.ProceessPackageTaskID = PCprocessPAckageTasks.EPFProcessPackageTaskIDInner JoinWorkMgmtPlanLineItemsOn PCprocessPAckageTasks.TaskID = WorkMgmtPlanLineItems.PCTaskIDwhereLineItemID = @LineItemID) AEndElse if @Data = 'R'BeginSelect @Result = case when @Result = ''then ltrim(rtrim(RoleName))else@Result + ';'+ ltrim(rtrim(RoleName))End From(Select distinct ltrim(rtrim(EPFRoles.RoleName)) 'RoleName'from WOrkMgmtPlanLineItemsInner JoinWorkMgmtPlanTaskAllocationsOn WorkMgmtPlanTaskAllocations.PlanLineItemID = WOrkMgmtPlanLineItems.LineItemIDInner Jointrn_user_allocationOntrn_user_allocation.s_user_id = WorkMgmtPlanTaskAllocations.ResourceIDInner JoinEPFRoles Ontrn_user_allocation.i_EPF_role_id = EPFRoles.RoleID whereWOrkMgmtPlanLineItems.LineItemID = @LineItemIDand WorkMgmtPlanTaskAllocations.IsActive = '1') AEndreturn @Result End |
 |
|
|
willfindavid
Starting Member
27 Posts |
Posted - 2008-06-30 : 07:12:08
|
| In the function above, I'm selecting values from tablesif (@Data ='U') i'm doing one selectelse if(if @Data = 'D') i'm doing one selectelse if(@Data = 'R') i'm doing one select |
 |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2008-07-02 : 01:52:15
|
| 1)Is it absolutely necessary to have the function ? For example , if you embedded the function into the code , would the query run quicker. My guess is that it would - because the optimizer would be able to see the full code and possibly decide on a better query plan 2)Can the function information be held in a view?Jack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com |
 |
|
|
|
|
|
|
|