'AdvanceSearchPro'

来源:互联网 发布:如何数据采样分析 编辑:程序博客网 时间:2024/05/16 13:48

USE [Hydra_Mig]
-- time convert: Hours-->Minutes
IF EXISTS (SELECT name FROM sysobjects WHERE name='timeCast' AND type='FN')
 DROP FUNCTION dbo.timeCast
GO
CREATE FUNCTION dbo.timeCast(@timeStr varchar(50))
returns varchar(50)
AS
BEGIN
 DECLARE @timeValue varchar(50)
 IF charindex('Day',@timeStr) > 0
  SET @timeValue = convert(varchar(50),
      convert(numeric(31,8),substring(@timeStr,0,len(@timeStr)-6))*86400);
 IF charindex('Hour',@timeStr) > 0
  SET @timeValue = convert(varchar(50),
      convert(numeric(31,8),substring(@timeStr,0,len(@timeStr)-6))*3600);
 IF charindex('Minute(s)',@timeStr) > 0
  SET @timeValue = substring(@timeStr,0,len(@timeStr)-9);

 RETURN @timeValue
END
GO
----string split
IF EXISTS (SELECT name FROM sysobjects WHERE name='strSplitToTable' AND type='TF')
 DROP FUNCTION dbo.strSplitToTable
GO
CREATE FUNCTION dbo.strSplitToTable
(
@str   VARCHAR(300)
)
RETURNS   @retab   table(id   VARCHAR(100))
AS
BEGIN
 DECLARE @spli   VARCHAR(5)
 SET @spli = ','
    DECLARE   @i  INT
    DECLARE   @splen   INT
    SELECT   @splen=len(@spli),@i=charindex(@spli,@str)
    WHILE   @i>0
    BEGIN
        INSERT INTO   @retab   
        VALUES(left(@str,@i-1))
        SELECT   @str=substring(@str,@i+@splen,300)
        SELECT   @i=charindex(@spli,@str)
    END
    IF @str <>' ' INSERT INTO @retab VALUES(@str)
    RETURN
END
GO
--if this procedure is exist,we will drop it
IF OBJECT_ID( 'AdvanceSearchPro','P') IS NOT NULL
DROP PROCEDURE AdvanceSearchPro;
GO

CREATE PROCEDURE [projectcenter].[AdvanceSearchPro]  --create advance search procedure
--input parameters
(
@page_count int OUTPUT
,@xmlParams varchar(max) = null
)
--WITH RECOMPILE 
AS
BEGIN
SET   NOCOUNT   ON

----xml params
DECLARE @idoc int
DECLARE @ParamsTable TABLE
(
 id int NOT NULL,
 pId int,
 pKey varchar(50) NOT NULL,
 pType varchar(50),
 pContent nvarchar(max) COLLATE Chinese_PRC_CI_AS
)
EXEC sp_xml_preparedocument @idoc OUTPUT, @xmlParams
insert into @ParamsTable
SELECT id,pId,pKey,pType,pContent  FROM OPENXML (@idoc, '/root/defect/param', 1)     
WITH (id int,pId int,pKey varchar(50),pType varchar(50),pContent ntext'text()')
exec sp_xml_removedocument @idoc

DECLARE @orderBy varchar(100)
SELECT @orderBy = pContent FROM @ParamsTable WHERE pKey='orderBy'
DECLARE @ascDesc varchar(100)
SELECT @ascDesc = pContent FROM @ParamsTable WHERE pKey='ascDesc'
DECLARE @project_group_id Numeric(19, 0)
SELECT @project_group_id = pContent FROM @ParamsTable WHERE pKey='projectGroupId'
DECLARE @user_id numeric(19,0)
SELECT @user_id = pContent FROM @ParamsTable WHERE pKey='userId'
DECLARE @page_index int
SELECT @page_index = pContent FROM @ParamsTable WHERE pKey='pageIndex'

DECLARE @id varchar(50)
SELECT @id=pContent FROM @ParamsTable WHERE pId=1

DECLARE @summary varchar(50)
SELECT @summary=pContent FROM @ParamsTable WHERE pId=2

DECLARE @ticket_type_id varchar(50)
SELECT @ticket_type_id=pContent FROM @ParamsTable WHERE pId=3

DECLARE @status_id VARCHAR(50)
SELECT @status_id=pContent FROM @ParamsTable WHERE pId=4

DECLARE @resolution_id varchar(100)
SELECT @resolution_id=pContent FROM @ParamsTable WHERE pId=5

DECLARE @description varchar(max)
SELECT @description=pContent FROM @ParamsTable WHERE pId=6

DECLARE @priority_id varchar(100)  --priority_id
SELECT @priority_id=pContent FROM @ParamsTable WHERE pId=7

DECLARE @severity_id varchar(100)     --severity_id
SELECT @severity_id=pContent FROM @ParamsTable WHERE pId=8

DECLARE @assigned_user_id VARCHAR(100)   -- assigned_user_id
SELECT @assigned_user_id=pContent FROM @ParamsTable WHERE pId=9

DECLARE @reviewer_user_id VARCHAR(100)   --reviewer_user_id
SELECT @reviewer_user_id=pContent FROM @ParamsTable WHERE pId=10

DECLARE @detectd_by_user_id VARCHAR(100)
SELECT @detectd_by_user_id=pContent FROM @ParamsTable WHERE pId=11

DECLARE @due_date_from VARCHAR(100)  --due_date
SELECT @due_date_from=pContent FROM @ParamsTable WHERE pId=12 AND pKey='from'

DECLARE @due_date_to   VARCHAR(100)   --due_date
SELECT @due_date_to=pContent FROM @ParamsTable WHERE pId=12 AND pKey='to'

DECLARE @response_time_from VARCHAR(100)  --response_time
SELECT @response_time_from=pContent FROM @ParamsTable WHERE pId=13 AND pKey='from'

DECLARE @response_time_to VARCHAR(100)  --response_time
SELECT @response_time_to=pContent FROM @ParamsTable WHERE pId=13 AND pKey='to'

DECLARE @resolution_time_from VARCHAR(100)
SELECT @resolution_time_from=pContent FROM @ParamsTable WHERE pId=14 AND pKey='from'

DECLARE @resolution_time_to VARCHAR(100)
SELECT @resolution_time_to=pContent FROM @ParamsTable WHERE pId=14 AND pKey='to'

DECLARE @estimated_size_from VARCHAR(100)  --estimated_size
SELECT @estimated_size_from=pContent FROM @ParamsTable WHERE pId=15 AND pKey='from'

DECLARE @estimated_size_to VARCHAR(100)  --estimated_size
SELECT @estimated_size_to=pContent FROM @ParamsTable WHERE pId=15 AND pKey='to'

DECLARE @actual_size_from VARCHAR(100)   --actual_size
SELECT @actual_size_from=pContent FROM @ParamsTable WHERE pId=16 AND pKey='from'

DECLARE @actual_size_to VARCHAR(100)   --actual_size
SELECT @actual_size_to=pContent FROM @ParamsTable WHERE pId=16 AND pKey='to'

DECLARE @planned_start_from VARCHAR(100)  --planned_start
SELECT @planned_start_from=pContent FROM @ParamsTable WHERE pId=17 AND pKey='from'

DECLARE @planned_start_to VARCHAR(100) --planned_start
SELECT @planned_start_to=pContent FROM @ParamsTable WHERE pId=17 AND pKey='to'

DECLARE @planned_finish_from VARCHAR(100) --planned_finish
SELECT @planned_finish_from=pContent FROM @ParamsTable WHERE pId=18 AND pKey='from'

DECLARE @planned_finish_to VARCHAR(100) --planned_finish
SELECT @planned_finish_to=pContent FROM @ParamsTable WHERE pId=18 AND pKey='to'

DECLARE @duration_from VARCHAR(100)   --duration
SELECT @duration_from=pContent FROM @ParamsTable WHERE pId=19 AND pKey='from'

DECLARE @duration_to VARCHAR(100)    --duration
SELECT @duration_to=pContent FROM @ParamsTable WHERE pId=19 AND pKey='to'

DECLARE @actual_start_from VARCHAR(100)
SELECT @actual_start_from=pContent FROM @ParamsTable WHERE pId=20 AND pKey='from'

DECLARE @actual_start_to VARCHAR(100)
SELECT @actual_start_to=pContent FROM @ParamsTable WHERE pId=20 AND pKey='to'

DECLARE @actual_finish_from VARCHAR(100)
SELECT @actual_finish_from=pContent FROM @ParamsTable WHERE pId=21 AND pKey='from'

DECLARE @actual_finish_to VARCHAR(100)
SELECT @actual_finish_to=pContent FROM @ParamsTable WHERE pId=21 AND pKey='to'

--DECLARE @project_module_id VARCHAR(100)
--SELECT @project_module_id=pContent FROM @ParamsTable WHERE pId='22'
-----
DECLARE @created_time_from VARCHAR(100)
SELECT @created_time_from=pContent FROM @ParamsTable WHERE pId=26 AND pKey='from'

DECLARE @created_time_to VARCHAR(100)
SELECT @created_time_to=pContent FROM @ParamsTable WHERE pId=26 AND pKey='to'

DECLARE @update_time_from VARCHAR(100)
SELECT @update_time_from=pContent FROM @ParamsTable WHERE pId=27 AND pKey='from'

DECLARE @update_time_to VARCHAR(100)
SELECT @update_time_to = pContent FROM @ParamsTable WHERE pId=27 AND pKey='to'

DECLARE @original_estimated_effort_from VARCHAR(100) --original_estimated_effort
SELECT @original_estimated_effort_from=pContent FROM @ParamsTable WHERE pId=28 AND pKey='from'

DECLARE @original_estimated_effort_to VARCHAR(100)
SELECT @original_estimated_effort_to=pContent FROM @ParamsTable WHERE pId=28 AND pKey='to'

DECLARE @latest_estimated_effort_from VARCHAR(100)  --latest_estimated_effort
SELECT @latest_estimated_effort_from=pContent FROM @ParamsTable WHERE pId=29 AND pKey='from'

DECLARE @latest_estimated_effort_to VARCHAR(100)
SELECT @latest_estimated_effort_to=pContent FROM @ParamsTable WHERE pId=29 AND pKey='to'

DECLARE @CustomizedColumn int
select @CustomizedColumn= count(pId) from @ParamsTable where  pId>33 or pId in (22,23,24,25)

DECLARE @project_id numeric(19,0)
select @project_id = p.id from projectcenter.project p where p.group_id = @project_group_id

DECLARE @actual_effort_fix VARCHAR(100)
SELECT @actual_effort_fix=pContent FROM @ParamsTable WHERE pId='30'

DECLARE @review_comments_no VARCHAR(100)
SELECT @review_comments_no=pContent FROM @ParamsTable WHERE pId='33'

DECLARE @newId NVARCHAR(100)
SET @newId = newId()

IF (@CustomizedColumn > 0)
Begin

DECLARE @optionsStr NVARCHAR(1000)
DECLARE @modules NVARCHAR(1000)
DECLARE @version NVARCHAR(1000)
DECLARE @option_id_temp NVARCHAR(1000)
DECLARE @defect_field_name_id NVARCHAR(1000)

SET @optionsStr = ''
SET @modules = ''
SET @version = ''
SET @option_id_temp = ''
SET @defect_field_name_id = ''

DECLARE option_cursor CURSOR FOR
select par.pContent, par.pId  from @ParamsTable par
where par.pType = 'list'
and (par.pId > 33 or par.pId in (22,23,24,25))

OPEN option_cursor;
FETCH NEXT from option_cursor into @option_id_temp, @defect_field_name_id
WHILE(@@FETCH_STATUS = 0)
BEGIN 
 IF(@defect_field_name_id=22)
  IF (@modules = '')
   SET @modules = @option_id_temp;
  ELSE
   SET @modules = @modules+','+@option_id_temp
  
 ELSE IF(@defect_field_name_id in (23,24,25))
  IF (@version = '')
   SET @version = @option_id_temp;
  ELSE
   SET @version = @version+','+@option_id_temp
 ELSE
  IF (@optionsStr = '')
   SET @optionsStr = @option_id_temp;
  ELSE
   SET @optionsStr = @optionsStr+','+@option_id_temp
 FETCH NEXT from option_cursor into @option_id_temp, @defect_field_name_id
END
CLOSE option_cursor
deallocate option_cursor

Declare @optionTable Table(
 option_name nvarchar(200) COLLATE Chinese_PRC_CI_AS,
 type varchar(10),
 field_info_id int
)
IF @modules <> ''
BEGIN
 SET @modules = N'select pgm.name,''mo'',null from projectcenter.project_group_module pgm where pgm.id in (' +@modules+')';
 insert into @optionTable EXEC sp_executesql @modules
END
IF @version <> ''
BEGIN
 SET @version = N'select pgv.name, ''ve'', null from projectcenter.project_group_version pgv where pgv.id in (' +@version+')';
 insert into @optionTable EXEC sp_executesql @version
END

IF @optionsStr <> ''
BEGIN
 SET @optionsStr = N'select op.name ,''opt'',op.defect_field_info_id from projectcenter.options op where op.id in (' +@optionsStr+')';
 insert into @optionTable  EXEC sp_executesql @optionsStr
END

insert into projectcenter.defect_id_temp (defect_id,juid)
SELECT defect.id, @newId
FROM projectcenter.defect AS defect
LEFT JOIN (select defect_id,max(changed_time) as changed_time from projectcenter.defect_change_log group by defect_id ) AS defect_change_log on defect.id = defect_change_log.defect_id
WHERE defect.project_id = @project_id and
(isnull(@id,null) IS  NULL OR id like @id) AND
(isnull(@summary,null) IS NULL OR summary like @summary) AND
(isnull(@ticket_type_id,null) IS NULL OR exists
 (select 1 FROM projectcenter.ticket_type tt where tt.id=ticket_type_id and tt.name in
  (select distinct ticket_type.name from projectcenter.ticket_type WHERE ticket_type.id in (select 1 from dbo.strSplitToTable(@ticket_type_id) ) )
    )
) AND
(isnull(@status_id,null) IS NULL OR status_id = @status_id) AND  --List
(isnull(@resolution_id,null) IS NULL OR resolution_id in
 (
  select opt.id
  FROM projectcenter.options AS opt
  WHERE
  exists (
    SELECT 1
    FROM projectcenter.options AS opt1
    WHERE opt1.id in (select id from dbo.strSplitToTable(@resolution_id)) and
    opt1.name=opt.name AND opt1.defect_field_info_id=opt.defect_field_info_id
  )
 )
) AND  ---List
(isnull(@description,null) IS NULL OR [description] like @description) AND
(isnull(@priority_id,null) IS NULL OR priority_id IN
 (
  select opt.id
  FROM projectcenter.options AS opt
  WHERE
  exists (
    SELECT 1
    FROM projectcenter.options AS opt1
    WHERE opt1.id in (select id from dbo.strSplitToTable(@priority_id)) and
    opt1.name=opt.name AND opt1.defect_field_info_id=opt.defect_field_info_id
  )
 )
 ) AND
(isnull(@severity_id,null) IS NULL OR severity_id IN
  (
  select opt.id
  FROM projectcenter.options AS opt
  WHERE
  exists (
    SELECT 1
    FROM projectcenter.options AS opt1
    WHERE opt1.id in (select id from dbo.strSplitToTable(@severity_id)) and
    opt1.name=opt.name AND opt1.defect_field_info_id=opt.defect_field_info_id
  )
 )
) AND
(isnull(@assigned_user_id,null) IS NULL OR assigned_user_id=@assigned_user_id ) AND
(isnull(@reviewer_user_id,null) IS NULL OR reviewer_user_id=@reviewer_user_id ) AND
(isnull(@detectd_by_user_id,null) IS NULL OR detectd_by_user_id=@detectd_by_user_id ) AND
(isnull(@due_date_from,null) IS NULL OR due_date>=CAST(@due_date_from AS datetime) ) AND
(isnull(@due_date_to,null) IS NULL OR due_date<=CAST(@due_date_to AS datetime) ) AND
(isnull(@response_time_from,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(response_time))>=convert(numeric(31,8),dbo.timeCast(@response_time_from)) ) AND
(isnull(@response_time_to,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(response_time))<=convert(numeric(31,8),dbo.timeCast(@response_time_to ))) AND
(isnull(@resolution_time_from,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(resolution_time))>=convert(numeric(31,8),dbo.timeCast(@resolution_time_from ))) AND
(isnull(@resolution_time_to,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(resolution_time))<=convert(numeric(31,8),dbo.timeCast(@resolution_time_to))) AND
(isnull(@estimated_size_from,null) IS NULL OR convert(int,estimated_size)>=convert(int,@estimated_size_from)) AND
(isnull(@estimated_size_to,null) IS NULL OR convert(int,estimated_size)<=convert(int,@estimated_size_to)) AND
(isnull(@actual_size_from,null) IS NULL OR convert(int,estimated_size)>=convert(int,@estimated_size_from)) AND
(isnull(@actual_size_to,null) IS NULL OR convert(int,estimated_size)<=convert(int,@estimated_size_to)) AND
(isnull(@planned_start_from,null) IS NULL OR planned_start>=CAST(@planned_start_from AS datetime)) AND
(isnull(@planned_start_to,null) IS NULL OR planned_start<=CAST(@planned_start_to AS datetime)) AND
(isnull(@planned_finish_from,null) IS NULL OR planned_finish>=CAST(@planned_finish_from AS datetime)) AND
(isnull(@planned_finish_to,null) IS NULL OR planned_finish<=CAST(@planned_finish_to AS datetime)) AND
(isnull(@duration_from,null) IS NULL OR convert(int,duration)>=convert(int,@duration_from)) AND
(isnull(@duration_to,null) IS NULL OR convert(int,duration)<=convert(int,@duration_to)) AND
(isnull(@actual_start_from,null) IS NULL OR actual_start<=CAST(@actual_start_from AS datetime)) AND
(isnull(@actual_start_to,null) IS NULL OR actual_start<=CAST(@actual_start_to AS datetime)) AND
(isnull(@actual_finish_from,null) IS NULL OR actual_finish=CAST(@actual_finish_from AS datetime)) AND
(isnull(@actual_finish_to,null) IS NULL OR actual_finish=CAST(@actual_finish_to AS datetime)) AND
(isnull(@created_time_from,null) IS NULL OR created_time>=CAST(@created_time_from AS datetime)) AND
(isnull(@created_time_to,null) IS NULL OR created_time<=CAST(@created_time_to AS datetime)) AND
(isnull(@update_time_from,null) IS NULL OR changed_time>=CAST(@update_time_from AS datetime)) AND
(isnull(@update_time_to,null) IS NULL OR changed_time<=CAST(@update_time_to AS datetime)) AND
(isnull(@original_estimated_effort_from,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(original_estimated_effort))>=convert(numeric(31,8),dbo.timeCast(@original_estimated_effort_from))) AND
(isnull(@original_estimated_effort_to,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(original_estimated_effort))<=convert(numeric(31,8),dbo.timeCast(@original_estimated_effort_to))) AND
(isnull(@latest_estimated_effort_from,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(latest_estimated_effort))>=convert(numeric(31,8),dbo.timeCast(@latest_estimated_effort_from)))
intersect
select vv.defect, @newId
from @ParamsTable par
inner join projectcenter.defect_field_info dfi on dfi.defect_field_name_id = par.pId
left join projectcenter.defect_extra_field_info defi on defi.id = dfi.id
inner join projectcenter.variable_value vv on vv.extra_field_id =dfi.id
left join projectcenter.options options on options.id = vv.option_id
inner join projectcenter.defect d on d.id = vv.defect and d.project_id = @project_id
where (par.pId >33 or par.pId in (22,23,24,25))--need inprovement
and (isnull(par.pContent,null) IS NULL or
(
(dtype = 'DateValue' and isdate(vv.date_value)>0 and isdate(par.pContent)>0 and ((par.pKey like '%From' and convert(varchar(50),vv.date_value, 120) >= convert(varchar(50),par.pContent, 120))  or (par.pKey like '%To' and convert(varchar(50),vv.date_value, 101) <= convert(varchar(50),par.pContent, 120))))
or
(dtype = 'LongValue' and defi.type <> 'MemberPicker' and par.pId not in(22,23,24,25) and isnumeric(par.pContent)>0 and ((par.pKey like '%From' and vv.long_value >= convert(numeric(19,0), par.pContent)) or (par.pKey like '%To' and vv.long_value <= convert(numeric(19,0), par.pContent))))
or
(dtype = 'StringVale' and vv.string_value like par.pContent)
or
(dtype = 'FloatValue' and isnumeric(par.pContent)>0 and ((par.pKey like '%From' and isnumeric(par.pContent)>0 and vv.float_value > cast(par.pContent as float)) or (par.pKey like '%To' and isnumeric(par.pContent)>0 and vv.float_value < cast(par.pContent as float))))
or
(dtype = 'MemoValue' and vv.memo_value like par.pContent)
or
(dtype = 'LongValue' and par.pId = 22 and exists (select 1 from projectcenter.project_group_module pgm inner join @optionTable ot on ot.option_name =pgm.name and ot.type = 'mo'and pgm.project_group_id = @project_group_id and vv.long_value = pgm.id))
or
(dtype = 'LongValue' and defi.type = 'MemberPicker' and isnumeric(par.pContent)>0 and cast(par.pContent as numeric(19,0))= vv.long_value)
or
(dtype = 'ListValue' and exists(select 1 from @optionTable ot where ot.type='opt' and ot.option_name = options.name))
))
group by vv.defect
having count(distinct vv.extra_field_id) >= (select count(distinct par1.pId) from @ParamsTable par1 where par1.pId > 33 or par1.pId  in (22,23,24,25))
END
ELSE
BEGIN

insert into projectcenter.defect_id_temp (defect_id,juid)
SELECT defect.id, @newId
FROM projectcenter.defect AS defect
LEFT JOIN (select defect_id,max(changed_time) as changed_time from projectcenter.defect_change_log group by defect_id ) AS defect_change_log on defect.id = defect_change_log.defect_id
WHERE defect.project_id = @project_id and
(isnull(@id,null) IS  NULL OR id like @id) AND
(isnull(@summary,null) IS NULL OR summary like @summary) AND
(isnull(@ticket_type_id,null) IS NULL OR exists
 (select 1 FROM projectcenter.ticket_type tt where tt.id=ticket_type_id and tt.name in
  (select distinct ticket_type.name from projectcenter.ticket_type WHERE ticket_type.id in (select 1 from dbo.strSplitToTable(@ticket_type_id) ) )
    )
) AND
(isnull(@status_id,null) IS NULL OR status_id = @status_id) AND  --List
(isnull(@resolution_id,null) IS NULL OR resolution_id in
 (
  select opt.id
  FROM projectcenter.options AS opt
  WHERE
  exists (
    SELECT 1
    FROM projectcenter.options AS opt1
    WHERE opt1.id in (select id from dbo.strSplitToTable(@resolution_id)) and
    opt1.name=opt.name AND opt1.defect_field_info_id=opt.defect_field_info_id
  )
 )
) AND  ---List
(isnull(@description,null) IS NULL OR [description] like @description) AND
(isnull(@priority_id,null) IS NULL OR priority_id IN
 (
  select opt.id
  FROM projectcenter.options AS opt
  WHERE
  exists (
    SELECT 1
    FROM projectcenter.options AS opt1
    WHERE opt1.id in (select id from dbo.strSplitToTable(@priority_id)) and
    opt1.name=opt.name AND opt1.defect_field_info_id=opt.defect_field_info_id
  )
 )
 ) AND
(isnull(@severity_id,null) IS NULL OR severity_id IN
  (
  select opt.id
  FROM projectcenter.options AS opt
  WHERE
  exists (
    SELECT 1
    FROM projectcenter.options AS opt1
    WHERE opt1.id in (select id from dbo.strSplitToTable(@severity_id)) and
    opt1.name=opt.name AND opt1.defect_field_info_id=opt.defect_field_info_id
  )
 )
) AND
(isnull(@assigned_user_id,null) IS NULL OR assigned_user_id=@assigned_user_id ) AND
(isnull(@reviewer_user_id,null) IS NULL OR reviewer_user_id=@reviewer_user_id ) AND
(isnull(@detectd_by_user_id,null) IS NULL OR detectd_by_user_id=@detectd_by_user_id ) AND
(isnull(@due_date_from,null) IS NULL OR due_date>=CAST(@due_date_from AS datetime) ) AND
(isnull(@due_date_to,null) IS NULL OR due_date<=CAST(@due_date_to AS datetime) ) AND
(isnull(@response_time_from,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(response_time))>=convert(numeric(31,8),dbo.timeCast(@response_time_from)) ) AND
(isnull(@response_time_to,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(response_time))<=convert(numeric(31,8),dbo.timeCast(@response_time_to ))) AND
(isnull(@resolution_time_from,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(resolution_time))>=convert(numeric(31,8),dbo.timeCast(@resolution_time_from ))) AND
(isnull(@resolution_time_to,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(resolution_time))<=convert(numeric(31,8),dbo.timeCast(@resolution_time_to))) AND
(isnull(@estimated_size_from,null) IS NULL OR convert(int,estimated_size)>=convert(int,@estimated_size_from)) AND
(isnull(@estimated_size_to,null) IS NULL OR convert(int,estimated_size)<=convert(int,@estimated_size_to)) AND
(isnull(@actual_size_from,null) IS NULL OR convert(int,estimated_size)>=convert(int,@estimated_size_from)) AND
(isnull(@actual_size_to,null) IS NULL OR convert(int,estimated_size)<=convert(int,@estimated_size_to)) AND
(isnull(@planned_start_from,null) IS NULL OR planned_start>=CAST(@planned_start_from AS datetime)) AND
(isnull(@planned_start_to,null) IS NULL OR planned_start<=CAST(@planned_start_to AS datetime)) AND
(isnull(@planned_finish_from,null) IS NULL OR planned_finish>=CAST(@planned_finish_from AS datetime)) AND
(isnull(@planned_finish_to,null) IS NULL OR planned_finish<=CAST(@planned_finish_to AS datetime)) AND
(isnull(@duration_from,null) IS NULL OR convert(int,duration)>=convert(int,@duration_from)) AND
(isnull(@duration_to,null) IS NULL OR convert(int,duration)<=convert(int,@duration_to)) AND
(isnull(@actual_start_from,null) IS NULL OR actual_start<=CAST(@actual_start_from AS datetime)) AND
(isnull(@actual_start_to,null) IS NULL OR actual_start<=CAST(@actual_start_to AS datetime)) AND
(isnull(@actual_finish_from,null) IS NULL OR actual_finish=CAST(@actual_finish_from AS datetime)) AND
(isnull(@actual_finish_to,null) IS NULL OR actual_finish=CAST(@actual_finish_to AS datetime)) AND
(isnull(@created_time_from,null) IS NULL OR created_time>=CAST(@created_time_from AS datetime)) AND
(isnull(@created_time_to,null) IS NULL OR created_time<=CAST(@created_time_to AS datetime)) AND
(isnull(@update_time_from,null) IS NULL OR changed_time>=CAST(@update_time_from AS datetime)) AND
(isnull(@update_time_to,null) IS NULL OR changed_time<=CAST(@update_time_to AS datetime)) AND
(isnull(@original_estimated_effort_from,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(original_estimated_effort))>=convert(numeric(31,8),dbo.timeCast(@original_estimated_effort_from))) AND
(isnull(@original_estimated_effort_to,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(original_estimated_effort))<=convert(numeric(31,8),dbo.timeCast(@original_estimated_effort_to))) AND
(isnull(@latest_estimated_effort_from,null) IS NULL OR convert(numeric(31,8),dbo.timeCast(latest_estimated_effort))>=convert(numeric(31,8),dbo.timeCast(@latest_estimated_effort_from)))
END
select @page_count=count(1) from defect_id_temp where juid=@newId

EXEC [projectcenter].[pageOrderTicketList] @newId, @orderBy, @ascDesc, @page_index ,20, @newId OUTPUT
EXEC [projectcenter].[show_ticket_list] @project_group_id, @user_id, @newId

END

GO
DECLARE @page_count int
EXEC AdvanceSearchPro @page_count=@page_count OUTPUT,
  @xmlParams ='
     <root><defect><param id="1" pType="Int" pKey="pageIndex" >1</param>
     <param id="3" pType="String" pKey="userId" >3203</param>
     <param id="4" pType="String" pKey="projectGroupId" >322</param>
     <param id="5" pType="String" pKey="orderBy">10</param>
     <param id="6" pType="String" pKey="ascDesc">asc</param></defect></root>
       '
       select @page_count