select '' as supcust_no from sysobjects with(nolock) where 1=2
##
if (#SV = 'kmjxc_pro' or #SV = 'kmjxc_std')
	exec('insert into t_bd_supcust_info(check_out_date,check_out_day,check_out_flag,credit_amt,display_flag,other1,other2,other3,region_no,sup_acct_back,sup_acct_no,
										sup_addr,sup_email,sup_fax,sup_man,sup_name,sup_tax_no,sup_tel,sup_type,supcust_flag,supcust_no ,zip,sale_way) 
		   select check_out_date,check_out_day,check_out_flag,credit_amt,display_flag,other1,other2,other3,region_no,sup_acct_back,sup_acct_no,sup_addr,
				sup_email,sup_fax,sup_man,sup_name,sup_tax_no,sup_tel,sup_type,supcust_flag,supcust_no ,zip,''A'' from #SDB..bi_t_supcust_info')
else if #SV = 'bw9kbzpro'
	begin
		exec('insert into t_bd_supcust_info (supcust_no, supcust_flag, sup_name, region_no, sup_type,sup_man, sup_addr, zip,sup_email, sup_tel, mobile, 
											 sup_fax, sup_acct_back, sup_acct_no, sup_tax_no, display_flag,check_out_flag, check_out_date, check_out_day,
											 credit_amt, other1, other2, other3 ,sale_way)
				SELECT  supcust_no, supcust_flag, sup_name, region_no,''2'', sup_man, sup_addr, zip,	sup_email, sup_tel, sup_mobile, sup_fax, 
					   sup_acct_back, sup_acct_no, sup_tax_no, display_flag,check_out_flag, check_out_date, check_out_day, credit_amt, other1,
					   other2, other3, sale_no
				FROM  #SDB..bi_t_supcust_info where supcust_flag=''S'' ')
		IF #TV = 'hbposev9'
			exec('insert into t_bd_supcust_info (supcust_no, supcust_flag, sup_name, region_no, sup_type,sup_man, sup_addr, zip,sup_email, sup_tel, mobile,
												 sup_fax, sup_acct_back, sup_acct_no, sup_tax_no, display_flag,check_out_flag, check_out_date, check_out_day, 
												 credit_amt, other1, other2, other3 ,sale_way,cust_clsno)
				SELECT  supcust_no, supcust_flag, sup_name, region_no,''2'', sup_man, sup_addr, zip,sup_email, sup_tel, sup_mobile, sup_fax, sup_acct_back, 
						sup_acct_no, sup_tax_no, display_flag,check_out_flag, check_out_date, check_out_day, credit_amt, other1, other2, other3, sale_no,''01''
				FROM  #SDB..bi_t_supcust_info 
				where supcust_flag=''C'' and 
					  supcust_no not in(select supcust_no from t_bd_supcust_info)')
		else
			exec('insert into t_bd_supcust_info (supcust_no, supcust_flag, sup_name, region_no, sup_type,sup_man, sup_addr, zip,sup_email, sup_tel, mobile,
												 sup_fax, sup_acct_back, sup_acct_no, sup_tax_no, display_flag,check_out_flag, check_out_date, check_out_day, 
												 credit_amt, other1, other2, other3 ,sale_way)
				SELECT  supcust_no, supcust_flag, sup_name, region_no,''2'', sup_man, sup_addr, zip,sup_email, sup_tel, sup_mobile, sup_fax, sup_acct_back, 
						sup_acct_no, sup_tax_no, display_flag,check_out_flag, check_out_date, check_out_day, credit_amt, other1, other2, other3, sale_no
				FROM  #SDB..bi_t_supcust_info 
				where supcust_flag=''C'' and 
					  supcust_no not in(select supcust_no from t_bd_supcust_info)')
		
	end 
else if #SV = 'tiger'
	exec('insert into t_bd_supcust_info (supcust_no, supcust_flag, sup_name, region_no, sup_man, sup_addr, zip,sup_email, sup_tel, sup_fax, sup_acct_back, 
									    sup_acct_no, sup_tax_no, display_flag,check_out_flag, check_out_date, check_out_day, credit_amt, other1, other2,other3 ,sale_way)
		SELECT ltrim(rtrim(supcust_no)), supcust_flag, sup_name, region_no, sup_man, sup_addr, zip,sup_email, sup_tel, sup_fax, sup_acct_back, sup_acct_no, sup_tax_no, 
				display_flag,check_out_flag, check_out_date, check_out_day, credit_amt, other1, other2, other3,(case sup_type when ''1'' then ''A'' when ''2'' then ''B'' else ''A'' end)
		FROM #SDB..ba_sup_info where ltrim(rtrim(supcust_no))<>'''' ')
else
	exec('insert into t_bd_supcust_info (supcust_no,supcust_flag,sup_name,region_no,sup_type,sup_man,sup_addr,zip,sup_email,sup_tel,sup_fax,sup_acct_back,sup_acct_no,sup_tax_no,display_flag,
									    check_out_flag,check_out_date,check_out_day,credit_amt,other1,other2,other3,sub_no,pay_flag,order_flag,purchase_day,po_cycle,acc_level,reg_type,oper_id,
									    purchase_week,sale_way,shopcard,modify_date,settle_branch,com_flag,mobile) 
		  select supcust_no,supcust_flag,sup_name,region_no,sup_type,sup_man,sup_addr,zip,sup_email,sup_tel,sup_fax,sup_acct_back,sup_acct_no,sup_tax_no,display_flag,check_out_flag,
				check_out_date,check_out_day,credit_amt,other1,other2,other3,sub_no,pay_flag,order_flag,purchase_day,po_cycle,acc_level,reg_type,oper_id,purchase_week,sale_way,
				shopcard,modify_date,settle_branch,com_flag,mobile 
		   from #SDB..t_bd_supcust_info')
		   
exec('update t_bd_supcust_info set sale_way = (case when CHARINDEX(''´úÏú'',sup_name) > 0 then ''B'' else (case when  CHARINDEX(''ÁªÓª'',sup_name) > 0 then ''C'' else sale_way end) end),
						     region_no = left(region_no,4),
							 check_out_flag = ''0'' ')
exec('update t_bd_item_info set item_sup_flag=sale_way 
	  from  t_bd_item_info,t_bd_supcust_info
	  where t_bd_item_info.main_supcust=t_bd_supcust_info.supcust_no and not sale_way=''A'' ')

if #TV = 'hbposev9'
	exec('update t_bd_supcust_info set status = ''0'' where status = '''' or status is null')

go
if (#SV = 'hbposv6' or #SV = 'hbposv7' or #SV = 'hbposv8' or #SV = 'hbposev8' or #SV = 'hbposev9') and
	(#TV = 'hbposv6' or #TV = 'hbposv7' or #TV = 'hbposv8' or #TV = 'hbposev8' or #TV = 'hbposev9')
	exec('update t_bd_supcust_info set sup_ly_amt = isnull(tab_a.sup_ly_amt,0)
			from #SDB..t_bd_supcust_info as tab_a
			where t_bd_supcust_info.supcust_no = tab_a.supcust_no')
GO
if (#SV = 'hbposv7' or #SV = 'hbposv8' or #SV = 'hbposev8' or #SV = 'hbposev9') and
	(#TV = 'hbposv7' or #TV = 'hbposv8' or #TV = 'hbposev8' or #TV = 'hbposev9')
	exec('update t_bd_supcust_info set cust_clsno = tab_a.cust_clsno
			from #SDB..t_bd_supcust_info as tab_a
			where t_bd_supcust_info.supcust_no = tab_a.supcust_no')
GO
if (#SV = 'hbposev8' or #SV = 'hbposev9') and
	(#TV = 'hbposev8' or #TV = 'hbposev9')
	exec('update t_bd_supcust_info set sup_ly_rate = isnull(tab_a.sup_ly_rate,0),
									   valid_date = tab_a.valid_date
			from #SDB..t_bd_supcust_info as tab_a
			where t_bd_supcust_info.supcust_no = tab_a.supcust_no')
GO

update t_bd_supcust_info set settle_branch = '00'
GO