--begin tran
declare @item_id int,@branch_id smallint,@item_subno varchar(30),@sale_price numeric(9, 2),@vip_price numeric(9, 2),@vip_price1 numeric(9, 2),@vip_price2 numeric(9, 2),@pf_price numeric(9, 2),@ls_sql varchar(4000)
declare cur_item_price_cmdata cursor for
select a.branch_id,a.item_id,a.item_subno,a.sale_price,a.vip_price,a.vip_price1,a.vip_price2,a.pf_price
from bi_t_item_price a
inner join sys_t_system b on b.sys_var_id='g_branch_no'
inner join bi_t_branch_info c on b.sys_var_value=c.branch_no and a.branch_id=c.branch_id
order by a.branch_id,a.item_id,a.item_subno
open cur_item_price_cmdata
fetch cur_item_price_cmdata into @branch_id,@item_id,@item_subno,@sale_price,@vip_price,@vip_price1,@vip_price2,@pf_price
while @@FETCH_STATUS = 0
begin
	if @@FETCH_STATUS  <> 0 break
	set @ls_sql = '	if exists( select 1 from bi_t_item_price where branch_id = '+CAST(@branch_id as varchar(30))+' and item_id = '+CAST(@item_id as varchar(30))+' and item_subno = '+CHAR(39)+@item_subno+CHAR(39)+' ) 
					begin 
						update bi_t_item_price set sale_price=0'+rtrim(convert(char,isnull(@sale_price,0)))
						+',vip_price=0'+rtrim(convert(char,isnull(@vip_price,0)))
						+',vip_price1=0'+rtrim(convert(char,isnull(@vip_price1,0)))
						+',vip_price2=0'+rtrim(convert(char,isnull(@vip_price2,0)))
						+',pf_price=0'+rtrim(convert(char,isnull(@pf_price,0)))+
						' where item_id= 0' +rtrim(convert(char,isnull(@item_id,0)))+' and branch_id= 0' +rtrim(convert(char,isnull(@branch_id,0)))+' and item_subno=' + char(39) + @item_subno + char(39)+'
					end 
					else 
					begin 
						insert into bi_t_item_price(item_id,branch_id,item_subno,sale_price,vip_price,vip_price1,vip_price2,pf_price) 
						values ('+'0'+rtrim(convert(char,isnull(@item_id,0)))+','+ '0'+rtrim(convert(char,isnull(@branch_id,0)))+','+ char(39)+rtrim(isnull(@item_subno,''))+char(39)+','+ 
						'0'+rtrim(convert(char,isnull(@sale_price,0)))+','+ '0'+rtrim(convert(char,isnull(@vip_price,0)))+','+ '0'+rtrim(convert(char,isnull(@vip_price1,0)))+','+ 
						'0'+rtrim(convert(char,isnull(@vip_price2,0)))+','+ '0'+rtrim(convert(char,isnull(@pf_price,0)))+') 
					end '
						insert into cm_t_cmdata (com_data, update_date, up_flag, branch_no, down_flag, d_branch_no) 
					values (@ls_sql, getdate(), '1', null, '0', null)
	fetch cur_item_price_cmdata into @branch_id,@item_id,@item_subno,@sale_price,@vip_price,@vip_price1,@vip_price2,@pf_price
end
close cur_item_price_cmdata
deallocate cur_item_price_cmdata
--select * from cm_t_cmdata where update_date > '2022-06-27 20:00:00'
--rollback tran