Select b.branch_no,b.item_no,b.supcust_no,b.sale_way,b.price,b.base_price,b.sale_price,b.vip_price,b.sup_ly_rate,
b.branch_price,'1' as branch_sale,'0' as com_flag,b.item_stock,a.base_price1,a.base_price2,a.base_price3  From #SDB..t_pc_branch_price b,#SDB..t_bd_item_info a 
Where a.item_no = b.item_no and (select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no') = '00'
##
if exists(select sys_var_value from t_sys_system where sys_var_id = 'g_branch_no' and sys_var_value <> '00')
begin
	declare @sheet_no int,@s_date varchar(8),@d_date datetime,@branch_no varchar(20),@sheet_id varchar(2),@v_no varchar(20)
	
	select @sheet_id = 'PX'
	select @branch_no = sys_var_value from t_sys_system where sys_var_id = 'g_branch_no'
	select @sheet_no=(isnull(sheet_value,0) + 1) 	
		from t_sys_sheet_no	where Lower(sheet_id) = Lower(@sheet_id)
	if @sheet_no>9999  
	   set   @sheet_no =0	 
	update t_sys_sheet_no set sheet_value= @sheet_no where Lower(sheet_id) = Lower(@sheet_id)
	select @v_no = '0000'+convert(char,@sheet_no)
	select @v_no = right(rtrim(@v_no),4)
	select @s_date=convert(char(8),getdate(),112)
	select @v_no = upper(@sheet_id) + @branch_no + right(@s_date,6) + @v_no

	Insert Into t_pc_price_flow_master(sheet_no,branch_no,trans_no,oper_date,oper_id,confirm_man,work_date,valid_date,other1,week)
		Values(@v_no,@branch_no,'PX',getdate(),'1001','1001',getdate(),getdate(),'数据转入','1111111');

	INSERT INTO t_pc_price_flow_detail(sheet_no,item_no,price_type,start_date,end_date,week,new_price,new_price1,new_price2,new_price3,new_price4)
	Select @v_no,b.item_no,'1',getdate(),DATEADD(day,30,getdate()),'1111111',b.price,b.sale_price,b.vip_price,a.sup_ly_rate,b.price From 
	#SDB..t_pc_branch_price b,#SDB..t_bd_item_info a Where a.item_no = b.item_no and b.branch_no = @branch_no

	if exists(select sheet_no from t_pc_price_flow_master where sheet_no = @v_no )
	update t_pc_price_flow_master set approve_flag = '1' where sheet_no = @v_no
	
	select @sheet_id = 'LK'
	select @branch_no = sys_var_value from t_sys_system where sys_var_id = 'g_branch_no'
	select @sheet_no=(isnull(sheet_value,0) + 1) 	
		from t_sys_sheet_no	where Lower(sheet_id) = Lower(@sheet_id)
	if @sheet_no>9999  
	   set   @sheet_no =0	 
	update t_sys_sheet_no set sheet_value= @sheet_no where Lower(sheet_id) = Lower(@sheet_id)
	select @v_no = '0000'+convert(char,@sheet_no)
	select @v_no = right(rtrim(@v_no),4)
	select @s_date=convert(char(8),getdate(),112)
	select @v_no = upper(@sheet_id) + @branch_no + right(@s_date,6) + @v_no

	Insert Into t_pc_price_flow_master(sheet_no,branch_no,trans_no,oper_date,oper_id,confirm_man,work_date,valid_date,other1,week,chgtype)
	Values(@v_no,@branch_no,'LK',getdate(),'1001','1001',getdate(),getdate(),'数据转入','1111111','100000')

	INSERT INTO t_pc_price_flow_detail(sheet_no,item_no,price_type,start_date,end_date,week,new_price)
	Select @v_no,b.item_no,'1',getdate(),DATEADD(day,30,getdate()),'1111111',b.base_price From #SDB..t_pc_branch_price b,#SDB..t_bd_item_info a 
	Where a.item_no = b.item_no and b.branch_no = @branch_no

	if exists(select sheet_no from t_pc_price_flow_master where sheet_no = @v_no )
	update t_pc_price_flow_master set approve_flag = '1' where sheet_no = @v_no
end 
go
