IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_prd1_query_tag]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_prd1_query_tag]
GO
create  procedure mps_prd1_query_tag(
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@onlyme int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@pctag int , @prd_updated_min varchar(255), @prd_updated_max varchar(255),@prd_code_min varchar(255), @prd_code_max varchar(255),@prd_title_min varchar(255), @prd_title_max varchar(255),@prd_model_min varchar(255), @prd_model_max varchar(255),@prd_spec_min varchar(255), @prd_spec_max varchar(255)) as begin     set nocount on 
    
    declare @nonedate datetime 
    set @nonedate = CONVERT(datetime,'1910-1-1') 
    declare @s1 varchar(8000) 
    declare @s2 varchar(100) 
    declare @i int 
    declare @amount int 
    select @amount = @pagecount*@pagesize 
    declare @pobjs table(nid int identity(1, 1), ordid bigint) 
    set @s1 = @pageobjids  
    while (@s1>'') 
    begin 
    set @i = charindex(',', @s1) 
    if (@i>0) 
    begin 
    set @s2 = substring(@s1,1,@i-1) 
    set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
    end else  
    begin 
    set @s2 = @s1 
    set @s1 = '' 
    end  
    insert into @pobjs(ordid) values(convert(bigint,@s2)) 
    end 
    if (exists (select top 1 * from @pobjs)) 
    begin 
    select a.* from mps_prd1 a,    @pobjs
    where prd_id=ordid
    order by nid  
    select a.* from mps_prd1_users a , @pobjs where prdp_prdid=ordid and prdp_cuid=@useruid
    return 
    end 
if (@prd_updated_min is null or @prd_updated_min = '_none_') set @prd_updated_min = '' 
if (@prd_updated_max is null or @prd_updated_max = '_none_') set @prd_updated_max = '' 

if (@prd_code_min is null or @prd_code_min = '_none_') set @prd_code_min = '' 
if (@prd_code_max is null or @prd_code_max = '_none_') set @prd_code_max = '' 

if (@prd_title_min is null or @prd_title_min = '_none_') set @prd_title_min = '' 
if (@prd_title_max is null or @prd_title_max = '_none_') set @prd_title_max = '' 

if (@prd_model_min is null or @prd_model_min = '_none_') set @prd_model_min = '' 
if (@prd_model_max is null or @prd_model_max = '_none_') set @prd_model_max = '' 

if (@prd_spec_min is null or @prd_spec_min = '_none_') set @prd_spec_min = '' 
if (@prd_spec_max is null or @prd_spec_max = '_none_') set @prd_spec_max = '' 

if @prd_updated_min>'' 
begin 
if (charindex('%',@prd_updated_min) = 0 and @prd_updated_min!='_empty_') 
 begin
set @prd_updated_min = '%'+@prd_updated_min+'%';
 end else if (@prd_updated_min='_empty_') set @prd_updated_min='' 
end else
begin 
set @prd_updated_min = '%' 
end
if @prd_updated_max>'' 
begin 
if (charindex('%', @prd_updated_max) = 0 and @prd_updated_max!='_empty_') 
begin
set@prd_updated_max = '%'+@prd_updated_max+'%';
 end else if (@prd_updated_max='_empty_') set @prd_updated_max='' 
end else
begin 
set @prd_updated_max = '%' 
end

if @prd_code_min>'' 
begin 
if (charindex('%',@prd_code_min) = 0 and @prd_code_min!='_empty_') 
 begin
set @prd_code_min = '%'+@prd_code_min+'%';
 end else if (@prd_code_min='_empty_') set @prd_code_min='' 
end else
begin 
set @prd_code_min = '%' 
end
if @prd_code_max>'' 
begin 
if (charindex('%', @prd_code_max) = 0 and @prd_code_max!='_empty_') 
begin
set@prd_code_max = '%'+@prd_code_max+'%';
 end else if (@prd_code_max='_empty_') set @prd_code_max='' 
end else
begin 
set @prd_code_max = '%' 
end

if @prd_title_min>'' 
begin 
if (charindex('%',@prd_title_min) = 0 and @prd_title_min!='_empty_') 
 begin
set @prd_title_min = '%'+@prd_title_min+'%';
 end else if (@prd_title_min='_empty_') set @prd_title_min='' 
end else
begin 
set @prd_title_min = '%' 
end
if @prd_title_max>'' 
begin 
if (charindex('%', @prd_title_max) = 0 and @prd_title_max!='_empty_') 
begin
set@prd_title_max = '%'+@prd_title_max+'%';
 end else if (@prd_title_max='_empty_') set @prd_title_max='' 
end else
begin 
set @prd_title_max = '%' 
end

if @prd_model_min>'' 
begin 
if (charindex('%',@prd_model_min) = 0 and @prd_model_min!='_empty_') 
 begin
set @prd_model_min = '%'+@prd_model_min+'%';
 end else if (@prd_model_min='_empty_') set @prd_model_min='' 
end else
begin 
set @prd_model_min = '%' 
end
if @prd_model_max>'' 
begin 
if (charindex('%', @prd_model_max) = 0 and @prd_model_max!='_empty_') 
begin
set@prd_model_max = '%'+@prd_model_max+'%';
 end else if (@prd_model_max='_empty_') set @prd_model_max='' 
end else
begin 
set @prd_model_max = '%' 
end

if @prd_spec_min>'' 
begin 
if (charindex('%',@prd_spec_min) = 0 and @prd_spec_min!='_empty_') 
 begin
set @prd_spec_min = '%'+@prd_spec_min+'%';
 end else if (@prd_spec_min='_empty_') set @prd_spec_min='' 
end else
begin 
set @prd_spec_min = '%' 
end
if @prd_spec_max>'' 
begin 
if (charindex('%', @prd_spec_max) = 0 and @prd_spec_max!='_empty_') 
begin
set@prd_spec_max = '%'+@prd_spec_max+'%';
 end else if (@prd_spec_max='_empty_') set @prd_spec_max='' 
end else
begin 
set @prd_spec_max = '%' 
end
 
    
if (@onlyme = 0) 
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from mps_prd1 a,mps_prd1_users , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and (a.prd_cuid=@useruid or a.prd_createcuid=@useruid ) 
and prd_updated like @prd_updated_min 
and prd_updated like @prd_updated_max 
and prd_code like @prd_code_min 
and prd_code like @prd_code_max 
and prd_title like @prd_title_min 
and prd_title like @prd_title_max 
and prd_model like @prd_model_min 
and prd_model like @prd_model_max 
and prd_spec like @prd_spec_min 
and prd_spec like @prd_spec_max 
order by prd_updated desc,prd_code,prd_title,prd_model,prd_spec
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.prd_id  from mps_prd1 a,mps_prd1_users , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and (a.prd_cuid=@useruid or a.prd_createcuid=@useruid ) 
and prd_updated like @prd_updated_min 
and prd_updated like @prd_updated_max 
and prd_code like @prd_code_min 
and prd_code like @prd_code_max 
and prd_title like @prd_title_min 
and prd_title like @prd_title_max 
and prd_model like @prd_model_min 
and prd_model like @prd_model_max 
and prd_spec like @prd_spec_min 
and prd_spec like @prd_spec_max 
order by prd_updated desc,prd_code,prd_title,prd_model,prd_spec
select count(1) resultcount  from datalinker where useruid=@useruid 
end else 
begin
select count(1) cnt from mps_prd1 a,mps_prd1_users , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and (a.prd_cuid= @useruid or a.prd_createcuid= @useruid) 
and prd_updated like @prd_updated_min 
and prd_updated like @prd_updated_max 
and prd_code like @prd_code_min 
and prd_code like @prd_code_max 
and prd_title like @prd_title_min 
and prd_title like @prd_title_max 
and prd_model like @prd_model_min 
and prd_model like @prd_model_max 
and prd_spec like @prd_spec_min 
and prd_spec like @prd_spec_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) prd_id from mps_prd1 a ,mps_prd1_users , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid
and (a.prd_cuid= @useruid or a.prd_createcuid= @useruid) 
and prd_updated like @prd_updated_min 
and prd_updated like @prd_updated_max 
and prd_code like @prd_code_min 
and prd_code like @prd_code_max 
and prd_title like @prd_title_min 
and prd_title like @prd_title_max 
and prd_model like @prd_model_min 
and prd_model like @prd_model_max 
and prd_spec like @prd_spec_min 
and prd_spec like @prd_spec_max 
order by prd_updated desc,prd_code,prd_title,prd_model,prd_spec
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from mps_prd1 a,@pobjs 
where a.prd_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_prd1_users a , @pobjs 
where prdp_prdid=ordid 
and nid<=@pagesize 
and prdp_cuid=@useruid 
order by prdp_type 
end 
end 
else
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from mps_prd1 a,mps_prd1_users  , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and prd_updated like @prd_updated_min 
and prd_updated like @prd_updated_max 
and prd_code like @prd_code_min 
and prd_code like @prd_code_max 
and prd_title like @prd_title_min 
and prd_title like @prd_title_max 
and prd_model like @prd_model_min 
and prd_model like @prd_model_max 
and prd_spec like @prd_spec_min 
and prd_spec like @prd_spec_max 
order by prd_updated desc,prd_code,prd_title,prd_model,prd_spec
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.prd_id  from mps_prd1 a,mps_prd1_users  , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and prd_updated like @prd_updated_min 
and prd_updated like @prd_updated_max 
and prd_code like @prd_code_min 
and prd_code like @prd_code_max 
and prd_title like @prd_title_min 
and prd_title like @prd_title_max 
and prd_model like @prd_model_min 
and prd_model like @prd_model_max 
and prd_spec like @prd_spec_min 
and prd_spec like @prd_spec_max 
order by prd_updated desc,prd_code,prd_title,prd_model,prd_spec
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from mps_prd1 a,mps_prd1_users  , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid 
and prd_updated like @prd_updated_min 
and prd_updated like @prd_updated_max 
and prd_code like @prd_code_min 
and prd_code like @prd_code_max 
and prd_title like @prd_title_min 
and prd_title like @prd_title_max 
and prd_model like @prd_model_min 
and prd_model like @prd_model_max 
and prd_spec like @prd_spec_min 
and prd_spec like @prd_spec_max 

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) prd_id from mps_prd1 a,mps_prd1_users  , mps_prdctg 
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and a.prd_pcid=pc_id and pc_cmpid=@cmpid and pc_tag=@pctag  
and prdp_cuid=@useruid
and prd_updated like @prd_updated_min 
and prd_updated like @prd_updated_max 
and prd_code like @prd_code_min 
and prd_code like @prd_code_max 
and prd_title like @prd_title_min 
and prd_title like @prd_title_max 
and prd_model like @prd_model_min 
and prd_model like @prd_model_max 
and prd_spec like @prd_spec_min 
and prd_spec like @prd_spec_max 
order by prd_updated desc,prd_code,prd_title,prd_model,prd_spec
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) a.* from mps_prd1 a,@pobjs 
where a.prd_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_prd1_users a , @pobjs 
where prdp_prdid=ordid 
and nid<=@pagesize 
and prdp_cuid=@useruid 
order by prdp_type 
end 
end
set nocount off end