IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_product_by_pcid]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_product_by_pcid]
GO
-- =============================================
-- Author:<Author,,Name>
-- Create date: <Create Date,,>
-- Description:<Description,,>
-- =============================================
CREATE procedure [dbo].[mps_g_product_by_pcid]
@pcid int,
@pagesize int,
@pagecount int,
@pageobjids varchar(max)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @ids table(id int identity(0,1),prd_id bigint)
declare @temp table(id int identity(0,1),prd_id bigint)
declare @pobjs table(id int identity(0,1),prd_id bigint)
declare @count1 int ,@count2 int,@count int,@s1 varchar(max),@s2 varchar(max),@i varchar(max)
--计算允许返回的最大条数
set @count=@pagesize*@pagecount

set @count1=1
set @count2=0

--拆分@pageobjids
set @s1 = @pageobjids 
if (@s1='_none_') set @s1=''
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 
  if (@s2>'') insert into @pobjs(prd_id) values(convert(bigint,@s2)) 
end
--如果objids有值,则只返回objids中的数据
if (exists (select * from @pobjs))
begin
select * from mps_prd_base a , @pobjs b where a.prd_id=b.prd_id order by b.id
return ;
end
--选择该分类的所有子类型
insert into @ids(prd_id) values(@pcid);
while(@count1!=@count2)
begin
set @count2=@count1
insert into @ids(prd_id) select pc_id from mps_prdctg where pc_father in (select prd_id from @ids)
select  @count1 =@@ROWCOUNT
end
--将所有满足条件的id放入临时表
insert into @temp(prd_id) select prd_id from mps_prd_base where prd_pcid in (select prd_id from @ids) and prd_code !='(临)'
--返回条数
select count(*) as [count] from @temp
--返回所有的id
set rowcount @count
select prd_id from @temp
--返回第一页的数据
set rowcount @pagesize
select * from mps_prd_base,@temp B
where mps_prd_base.prd_id=B.prd_id

END