 select card_id, cardid_source, card_id as card_flowno , card_type, vip_name, vip_sex, vip_add, vip_zip, vip_tel, vip_email, vip_tax, card_status, oper_id, oper_date, vip_start_date, vip_end_date, use_num, birthday, social_id, residual_amt, consum_amt, consum_count, company, duty, degree, mobile, homeplace, reg_place, live_place, marriage, salary_level, photo_file, nation, height, weight, blood_type, favor, acc_num, dec_num, memo, num1, num2, num3, other1, other2, other3, modify_date, com_flag,
tab_a.branch_no
 from #sdb..t_rm_vip_info,(select top 1 branch_no from #tdb..t_bd_branch_info where property = '1') as tab_a
##
truncate table t_rm_vip_good_flow
go

insert into t_rm_vip_good_flow(vgf_branch_no,vgf_flag,vgf_vip_no,vgf_item_num,vgf_vip_num,vgf_oper,vgf_date)
	select '00','3',card_id,'0',(acc_num-dec_num),'1001',getdate() 
                  from t_rm_vip_info 
                  where (acc_num-dec_num)<>0 and
                              exists(select top 1 1 from t_bd_branch_info where property = '1' and branch_no = '00')
go

truncate table t_rm_saving_plus_record
go

create table #tab_vip(id int identity(1,1),card_id varchar(50),residual_amt decimal(18,4))
declare @ls_branch_no varchar(10),
		@ls_pos_sav_amttype varchar(1),
		@ls_card_id varchar(50),
		@ldec_residual_amt decimal(18,4),
		@ll_count int

set @ls_branch_no = ''
select top 1 @ls_branch_no from t_bd_branch_info where property = '1'
select @ls_branch_no = isnull(@ls_branch_no,'')

set @ls_pos_sav_amttype = '0'
select top 1 @ls_pos_sav_amttype = sys_var_value from t_sys_system where sys_var_id = 'pos_sav_amttype'
select @ls_pos_sav_amttype = isnull(@ls_pos_sav_amttype,'0')

if @ls_branch_no = '00' or @ls_pos_sav_amttype = '0'
	begin
		insert into #tab_vip 
		select card_id,dbo.f_str_decrypt(residual_amt) from t_rm_vip_info 
		where card_type in(select  type_id from t_rm_vip_type where saving_flag='1')
		
		set @ll_count = 0
		select @ll_count = count(*) from #tab_vip
		
		while @ll_count > 0
			begin
				select @ls_card_id = card_id,@ldec_residual_amt = residual_amt from #tab_vip where id = @ll_count
				
				insert into t_rm_saving_plus_record (branch_no,card_id,plus_count,oper_id,oper_date,plus_real_count)	
				values(@ls_branch_no,@ls_card_id,@ldec_residual_amt,'1001',getdate(),@ldec_residual_amt)
				
				set @ll_count = @ll_count - 1
			end
	end
	
drop table #tab_vip
go
