INSERT INTO t_sys_oper_grant(oper_id, func_id, other)
select o.oper_id, m.menu_id, '' 
from t_sys_operator o,
	(
	 select menu_id 
	 from t_sys_menu
	 where (menu_name <> '-')     		
	) m
where not exists(select top 1 1 from t_sys_oper_grant tab_a with(nolock) 
								where tab_a.oper_id=o.oper_id and
									  tab_a.func_id=m.menu_id)
GO

declare @ls_old_branch_no varchar(1000),
        @ls_new_branch_no varchar(1000)
	
set @ls_old_branch_no = ''
set @ls_new_branch_no = ''

if (#TV = 'isszmv6' or #TV = 'isszmv7' or #TV = 'isszmv8' or #TV = 'isszmv9') and (not (#SV = 'isszmv6' or #SV = 'isszmv7' or #SV = 'isszmv8' or #SV = 'isszmv9'))
	begin
		declare @ll_id int
				
		set @ll_id = 0
		if #TV = 'isszmv9'
			update t_bd_branch_info set branch_no = (case when branch_no = '00' or branch_no = '000' then right('0' + ltrim(rtrim(branch_no)),3) else right('000000' + cast(@ll_id as varchar(6)),6) end),
										property = (case when branch_no = '00' or branch_no = '000' then property else '2' end),
										@ll_id = @ll_id + 1,
										@ls_old_branch_no = @ls_old_branch_no + '|' + branch_no,
										@ls_new_branch_no = @ls_new_branch_no + '|' + (case when branch_no = '00' or branch_no = '000' then right('0' + ltrim(rtrim(branch_no)),3) else right('000000' + cast(@ll_id as varchar(6)),6) end),
									    trade_type = (case trade_type when '0' then '6' when '1' then '3' when '2' then '8' when '3' then '2' when '4' then '6' else '2' end)
		else
			update t_bd_branch_info set branch_no = (case len(ltrim(rtrim(branch_no))) when 2 then '0' + ltrim(rtrim(branch_no)) when 4 then '0' + left(ltrim(rtrim(branch_no)),2) + '0' + right(ltrim(rtrim(branch_no)),2) else branch_no end),
										@ls_old_branch_no = @ls_old_branch_no + '|' + branch_no,
										@ls_new_branch_no = @ls_new_branch_no + '|' + (case len(ltrim(rtrim(branch_no))) when 2 then '0' + ltrim(rtrim(branch_no)) when 4 then '0' + left(ltrim(rtrim(branch_no)),2) + '0' + right(ltrim(rtrim(branch_no)),2) else branch_no end),
									    trade_type = (case trade_type when '0' then '6' when '1' then '3' when '2' then '8' when '3' then '2' when '4' then '6' else '2' end)			

	
	end
else if (not (#TV = 'isszmv6' or #TV = 'isszmv7' or #TV = 'isszmv8' or #TV = 'isszmv9')) and (#SV = 'isszmv6' or #SV = 'isszmv7' or #SV = 'isszmv8' or #SV = 'isszmv9')
	begin
		
		
		update t_bd_branch_info set branch_no = (case len(ltrim(rtrim(branch_no))) when 3 then right(ltrim(rtrim(branch_no)),2) when 6 then right(left(ltrim(rtrim(branch_no)),3),2)+ right(ltrim(rtrim(branch_no)),2) else branch_no end),
								    @ls_old_branch_no = @ls_old_branch_no + '|' + branch_no,
								    @ls_new_branch_no = @ls_new_branch_no + '|' + (case len(ltrim(rtrim(branch_no))) when 3 then right(ltrim(rtrim(branch_no)),2) when 6 then right(left(ltrim(rtrim(branch_no)),3),2)+ right(ltrim(rtrim(branch_no)),2) else branch_no end),
									trade_type = (case trade_type when '0' then '3' when '1' then '1' when '2' then '3' when '5' then '1' when '6' then '0' when '7' then '1' when '8' then '2' else '1' end)
	end

if not (@ls_old_branch_no = '' or @ls_new_branch_no = '')
	begin
		
	

		declare @ll_index int,
				@ll_index_bh int,
				@ls_up_old_branch_no varchar(10),
				@ls_up_new_branch_no varchar(10)
		set @ls_up_old_branch_no = ''
		set @ls_up_new_branch_no = ''
		
		if left(@ls_new_branch_no,1) = '|' 
			set @ls_new_branch_no = right(@ls_new_branch_no,len(@ls_new_branch_no) - 1)
		if left(@ls_old_branch_no,1) = '|' 
			set @ls_old_branch_no = right(@ls_old_branch_no,len(@ls_old_branch_no) - 1)

		set @ll_index    = charindex('|',@ls_new_branch_no)
		set @ll_index_bh = charindex('|',@ls_old_branch_no)
		

		while (@ll_index > 0 and @ll_index_bh > 0) or (@ls_new_branch_no <> '' and @ls_old_branch_no <> '') 
			begin
				if not (@ll_index > 0 and @ll_index_bh > 0)
					begin
						set @ls_up_old_branch_no = @ls_old_branch_no
						set @ls_up_new_branch_no = @ls_new_branch_no
						set @ls_old_branch_no = ''
						set @ls_up_new_branch_no = ''
					end
				else
					begin
						set @ls_up_old_branch_no = substring(@ls_old_branch_no,1,@ll_index_bh - 1)
						set @ls_up_new_branch_no = substring(@ls_new_branch_no,1,@ll_index - 1)
					end
				
				
				
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_bd_branch_group_info' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')
					update t_bd_branch_group_info set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no and 
														not exists(select top 1 1 from t_bd_branch_group_info as tab_a where tab_a.branch_no = @ls_up_new_branch_no and 
																															  tab_a.group_id = t_bd_branch_group_info.group_id)
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_bd_place_info' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')
					update t_bd_place_info set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no 

				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_bd_place_item_info' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')			
					update t_bd_place_item_info set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no  
				
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_im_flow' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')								
					update t_im_flow set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no
				
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_rm_salecust_info' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')						
					update t_rm_salecust_info set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no  
							
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_rm_vip_info' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')										
					update t_rm_vip_info set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no  
								
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_rm_vip_consume' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')								
					update t_rm_vip_consume set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no  
						
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_sys_operator' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')							
					update t_sys_operator set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no  
													
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_rm_cashier' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')			
					update t_rm_cashier set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no  
														
				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_sys_system' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'sys_var_value')		
					update t_sys_system set sys_var_value = @ls_up_new_branch_no 
												  where sys_var_value = @ls_up_old_branch_no and 
														sys_var_id = 'g_stock_no'

				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_pc_branch_price' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')							
					update t_pc_branch_price set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no 

				if exists(select top 1 1 from sysobjects tab_a with(nolock),syscolumns tab_b with(nolock)
									   where tab_a.xtype = 'U' and
											 tab_a.name = 't_pc_price_flow_master' and
											 tab_a.id = tab_b.id and
											 tab_b.name = 'branch_no')							
					update t_pc_price_flow_master set branch_no = @ls_up_new_branch_no 
												  where branch_no = @ls_up_old_branch_no 
				

				if @ls_old_branch_no <> '' and @ls_new_branch_no <> ''
					begin
						set @ls_old_branch_no = right(@ls_old_branch_no,len(@ls_old_branch_no) - @ll_index_bh)
						set @ls_new_branch_no = right(@ls_new_branch_no,len(@ls_new_branch_no) - @ll_index_bh)
						set @ll_index    = charindex('|',@ls_new_branch_no)
						set @ll_index_bh = charindex('|',@ls_old_branch_no)
					end
				else
					begin
						set @ll_index = 0
						set @ll_index_bh = 0
					end
				
			end
	end
GO