IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_salecart]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_salecart]
GO
CREATE proc [dbo].[mps_g_salecart](
@cmpid int ,
@useruid int , 
@pagesize int , 
@pagecount int , 
@pageobjids varchar(8000),
@maxid int,
@malltype nvarchar(30) 
) as
begin
set nocount on
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
 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_saleprds a,@pobjs
    where a.sp_id=ordid
    order by nid  
    return 
    end  


select count(1) cnt from  mps_customer,mps_salecart a with(nolock) , mps_saleprds  b with(nolock)  
  where ct_cuid=@useruid and ct_spid=sp_id and sp_type=@malltype and cc_useruid=sp_cuid
  




-- 返回所有ID 
insert into @pobjs(ordid) 
select  distinct top(@amount)  cc_id from  mps_customer,mps_salecart a with(nolock) , mps_saleprds  b with(nolock)  
  where ct_cuid=@useruid and ct_spid=sp_id and sp_type=@malltype  and cc_useruid=sp_cuid
order by cc_id desc 

    set @maxid = ISNULL(@maxid,0) 
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) * from mps_customer a,@pobjs 
where a.cc_id=ordid and nid>@maxid 
order by nid 
set nocount off 
end