IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_prd_query_simple]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_prd_query_simple]
GO
CREATE  procedure [dbo].[mps_prd_query_simple](
@cmpid int,@branchid int,@userdpid int,@userotid int ,@useruid int ,@onlyme int ,@pagesize int , @pagecount int , @pageobjids varchar(8000),@pcid int , @includesub int , @date1 datetime ,@date2 datetime ,@key1 nvarchar(50),@key2 nvarchar(50),@key3 nvarchar(50)) 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_prd a,    @pobjs
    where prd_id=ordid
    order by nid  
    select a.* from mps_prd_users a , @pobjs where prdp_prdid=ordid and prdp_cuid=@useruid
  order by prdp_type    return 
    end  
    
if (@key1='') set @key1='%' 
if (charindex('%',@key1)<=0 and @key1>'') set @key1='%'+@key1+'%'

if (@key2='') set @key2='%' 
if (charindex('%',@key2)<=0 and @key2>'') set @key2='%'+@key2+'%'

if (@key3='') set @key3='%' 
if (charindex('%',@key3)<=0 and @key3>'') set @key3='%'+@key3+'%'

declare @prdctg1 table(pcid int) 
declare @prdctg table(pcid int) 
insert into @prdctg1(pcid) values(@pcid) 
if (@includesub=0) 
begin
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg1(pcid) 
select pc_id from mps_prdctg,@prdctg1 b where pc_father=b.pcid and (not exists (select 1 from @prdctg1 c where c.pcid=pc_id)) 
insert into @prdctg(pcid) select distinct pcid from @prdctg1 
end    else 
begin
insert into @prdctg(pcid) select distinct pcid from @prdctg1 
end

declare @prduse int = 0
if @onlyme = 2 set @prduse = 1
else set @prduse = 0

if (@onlyme = 0) 
begin
if (@pagesize=10000)  -- 下载
begin
select a.* from mps_prd a,mps_prd_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and (a.prd_cuid=@useruid or a.prd_createcuid=@useruid ) 
and 
(prd_updated >= @date1 and prd_updated <= @date2 ) and 
(
 (prd_updated like @key1 and prd_updated like @key2 and prd_updated like @key3 ) or 
 (prd_code like @key1 and prd_code like @key2 and prd_code like @key3 ) or 
 (prd_title like @key1 and prd_title like @key2 and prd_title like @key3 ) or 
 (prd_model like @key1 and prd_model like @key2 and prd_model like @key3 ) or 
 (prd_spec like @key1 and prd_spec like @key2 and prd_spec like @key3 ) )
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_prd a,mps_prd_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'') 
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and (a.prd_cuid=@useruid or a.prd_createcuid=@useruid ) 
and 
(prd_updated >= @date1 and prd_updated <= @date2 ) and 
(
 (prd_updated like @key1 and prd_updated like @key2 and prd_updated like @key3 ) or 
 (prd_code like @key1 and prd_code like @key2 and prd_code like @key3 ) or 
 (prd_title like @key1 and prd_title like @key2 and prd_title like @key3 ) or 
 (prd_model like @key1 and prd_model like @key2 and prd_model like @key3 ) or 
 (prd_spec like @key1 and prd_spec like @key2 and prd_spec like @key3 ) )
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_prd a,mps_prd_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and (a.prd_cuid= @useruid or a.prd_createcuid= @useruid) 
and 
(prd_updated >= @date1 and prd_updated <= @date2 ) and 
(
 (prd_updated like @key1 and prd_updated like @key2 and prd_updated like @key3 ) or 
 (prd_code like @key1 and prd_code like @key2 and prd_code like @key3 ) or 
 (prd_title like @key1 and prd_title like @key2 and prd_title like @key3 ) or 
 (prd_model like @key1 and prd_model like @key2 and prd_model like @key3 ) or 
 (prd_spec like @key1 and prd_spec like @key2 and prd_spec like @key3 ) )

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) prd_id from mps_prd a ,mps_prd_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid
and (a.prd_cuid= @useruid or a.prd_createcuid= @useruid) 
and 
(prd_updated >= @date1 and prd_updated <= @date2 ) and 
(
 (prd_updated like @key1 and prd_updated like @key2 and prd_updated like @key3 ) or 
 (prd_code like @key1 and prd_code like @key2 and prd_code like @key3 ) or 
 (prd_title like @key1 and prd_title like @key2 and prd_title like @key3 ) or 
 (prd_model like @key1 and prd_model like @key2 and prd_model like @key3 ) or 
 (prd_spec like @key1 and prd_spec like @key2 and prd_spec like @key3 ) )
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_prd a,@pobjs 
where a.prd_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_prd_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_prd a,mps_prd_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'') 
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and 
(prd_updated >= @date1 and prd_updated <= @date2 ) and 
(
 (prd_updated like @key1 and prd_updated like @key2 and prd_updated like @key3 ) or 
 (prd_code like @key1 and prd_code like @key2 and prd_code like @key3 ) or 
 (prd_title like @key1 and prd_title like @key2 and prd_title like @key3 ) or 
 (prd_model like @key1 and prd_model like @key2 and prd_model like @key3 ) or 
 (prd_spec like @key1 and prd_spec like @key2 and prd_spec like @key3 ) )
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_prd a,mps_prd_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'') 
and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and 
(prd_updated >= @date1 and prd_updated <= @date2 ) and 
(
 (prd_updated like @key1 and prd_updated like @key2 and prd_updated like @key3 ) or 
 (prd_code like @key1 and prd_code like @key2 and prd_code like @key3 ) or 
 (prd_title like @key1 and prd_title like @key2 and prd_title like @key3 ) or 
 (prd_model like @key1 and prd_model like @key2 and prd_model like @key3 ) or 
 (prd_spec like @key1 and prd_spec like @key2 and prd_spec like @key3 ) )
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_prd a,mps_prd_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'')
            and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid 
and 
(prd_updated >= @date1 and prd_updated <= @date2 ) and 
(
 (prd_updated like @key1 and prd_updated like @key2 and prd_updated like @key3 ) or 
 (prd_code like @key1 and prd_code like @key2 and prd_code like @key3 ) or 
 (prd_title like @key1 and prd_title like @key2 and prd_title like @key3 ) or 
 (prd_model like @key1 and prd_model like @key2 and prd_model like @key3 ) or 
 (prd_spec like @key1 and prd_spec like @key2 and prd_spec like @key3 ) )

-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) prd_id from mps_prd a,mps_prd_users , @prdctg
where a.prd_id=prdp_prdid 
and prdp_type='prdview' 
and (a.prd_code not like '%临%' and a.prd_code<>'') 
            and a.prd_use = @prduse
and a.prd_pcid=pcid 
and prdp_cuid=@useruid
and 
(prd_updated >= @date1 and prd_updated <= @date2 ) and 
(
 (prd_updated like @key1 and prd_updated like @key2 and prd_updated like @key3 ) or 
 (prd_code like @key1 and prd_code like @key2 and prd_code like @key3 ) or 
 (prd_title like @key1 and prd_title like @key2 and prd_title like @key3 ) or 
 (prd_model like @key1 and prd_model like @key2 and prd_model like @key3 ) or 
 (prd_spec like @key1 and prd_spec like @key2 and prd_spec like @key3 ) )
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_prd a,@pobjs 
where a.prd_id=ordid 
order by nid 
-- 返回权限 
select a.* from mps_prd_users a , @pobjs 
where prdp_prdid=ordid 
and nid<=@pagesize 
and prdp_cuid=@useruid 
order by prdp_type 
end 
end
set nocount off end