IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_calc_bizpower_all]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sys_calc_bizpower_all]
GO
CREATE procedure [dbo].[mps_sys_calc_bizpower_all](
@cmpid int 
) as
begin
declare @userchanged int 
select @userchanged=COUNT(1) from mps_tableupdated where tu_cmpid=@cmpid and tu_table in ('mps_employee','mps_depart') and tu_updated=0 
declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select biz_id  from mps_biztype  where biz_cmpid=@cmpid and (biz_changed = 0 or @userchanged>0)
open c1
declare @bizid int 
declare @tableexists1 int
declare @tableexists2 int
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
FETCH NEXT FROM c1 INTO @bizid 
WHILE (@@FETCH_STATUS = 0)
BEGIN
set @tableexists1 = 1 
set @tableexists2 = 1 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
set @tbname = @dbname + @tbname
exec mps_sys_checktable @tbname , @tableexists1 output 
set @tbname = @tbname+'_users' 
exec mps_sys_checktable @tbname , @tableexists2 output 
if (@tableexists1=0 and @tableexists2=0) 
 exec mps_sys_calc_bizpower @cmpid,@bizid 
 update mps_biztype set biz_changed=1 where biz_id=@bizid 
 FETCH NEXT FROM c1 INTO @bizid 
END
CLOSE c1
DEALLOCATE c1 
end