IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_calc_docpower_all]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_sys_calc_docpower_all]
GO
create procedure [dbo].[mps_sys_calc_docpower_all](
@cmpid int 
) as
begin
declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select dc_id  from mps_docctg  where dc_cmpid=@cmpid and dc_changed = 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,'dc',@bizid,0,@tbname output,@fkey output ,@dbname output 
set @tbname = @dbname + @tbname
exec mps_sys_checktable @tbname , @tableexists1 output 

if (@tableexists1=0)  
 exec mps_sys_calc_docpower @cmpid,@bizid 
 update mps_docctg set dc_changed=1 where dc_id=@bizid 
 FETCH NEXT FROM c1 INTO @bizid 
END
CLOSE c1
DEALLOCATE c1 
end