IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[Qsy_cc_desk_vipINTEGRAL]') AND type = 'P')
DROP PROCEDURE [dbo].[Qsy_cc_desk_vipINTEGRAL]
GO

-- =============================================
-- Author:<00384>
-- Create date: <2018-06-28>
-- Description:<呼叫中心-VIP积分流水查询>
-- =============================================
CREATE PROCEDURE [dbo].[Qsy_cc_desk_vipINTEGRAL] 
-- Add the parameters for the stored procedure here
@pagesize int,
@pagecount int ,
@pageobjids varchar(max),
@VIPID nvarchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;


declare @s1 varchar(8000),
@s2 varchar(100),
@IDs varchar(max)='',
@i int,
@amount int,
@sql nvarchar(max)

declare @pobjs table(nid int identity(1, 1), ordid bigint) 
select @amount = @pagecount*@pagesize

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
if(@IDs>'') 
set @IDs = @IDs +','+@s2
else 
set @IDs = @s2
insert into @pobjs(ordid) values(convert(bigint,@s2)) 
end

if (exists (select top 1 * from @pobjs))
begin
set @sql='
select * from openquery(posformal,''select ID,CHANGDATE,DOCNO,AMT_ACTUAL ,INTEGRAL ,DESCRIPTION   
from FA_VIPINTEGRAL_FTP 
where c_vip_id='''''+@VIPID+''''' and isactive=''''Y''''  
and ID in('+@IDs+')'')
order by CHANGDATE desc,docno desc
'

exec (@sql)
return
end

---以下是第一次加载数据
set @sql='
select * from openquery(posformal,''select count(ID) cnt
from FA_VIPINTEGRAL_FTP 
where c_vip_id='''''+@VIPID+''''' and isactive=''''Y''''  '')

select ID from openquery(posformal,''select ID,CHANGDATE CHANGDATE1,docno
from FA_VIPINTEGRAL_FTP 
where c_vip_id='''''+@VIPID+''''' and isactive=''''Y''''  
and rownum<='+convert(varchar(20),@amount)+'
'')
order by CHANGDATE1 desc,docno desc

select * from openquery(posformal,''select ID,CHANGDATE ,DOCNO,AMT_ACTUAL ,INTEGRAL ,DESCRIPTION   
from FA_VIPINTEGRAL_FTP where c_vip_id='''''+@VIPID+''''' and isactive=''''Y''''  
and rownum<='+convert(varchar(20),@pagesize)+'
'')
order by CHANGDATE desc,docno desc
'

exec (@sql)


END