IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_wrk_free]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_wrk_free]
GO

create procedure [dbo].[mps_g_wrk_free](
@cmpid int ,
@useruid int ,
@wrkid int ,
@ordid bigint,
@jbid bigint
)
as
begin
set nocount on

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 


declare @otlevel int
declare @managelevel int
declare @professionlevel int 
declare @bcid int 
declare @dpid int 
declare @otid int 
declare @roleid int
declare @branch nvarchar(50)
declare @depart nvarchar(50)
declare @callname nvarchar(50)


declare @freesql nvarchar(4000) 

select top 1 @bcid=cu_bcid,@dpid=cu_dpid,@otid=cu_otid,@roleid=cu_function 
  , @branch=cu_branch , @depart=cu_depart , @callname=cu_callname  , @otlevel=cu_otlevel ,
  @managelevel=cu_managelevel , @professionlevel=cu_professionlevel 
 from mps_login where  cu_id=@useruid and cu_type=0 
 
declare @bizid int 
select top  1  @bizid=wrk_bizid from mps_flowwork where wrk_id=@wrkid 
declare @myi int 
declare @extsql nvarchar(4000) 

select top 1 @freesql = convert(nvarchar(4000),cd_text) from mps_extsqljs where cd_objid=@wrkid and cd_objid2=0 and  cd_name='wrkfree' 

set @freesql = isnull(@freesql,'') 

set @freesql = replace(@freesql,'#user','#permit') 

declare @newsql nvarchar(4000) 
declare @newsql2 nvarchar(4000) 
set @newsql = @freesql 
if (len(@newsql)>20) 
begin

exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
set @tbname =@dbname+@tbname

set @extsql = '' 
if (CHARINDEX('#master',@newsql)>0) 
begin
set @extsql = 'exec drop_temp_table ''#master'' '+char(13)+char(10) 
set @extsql = @extsql +' select * into #master from '+@tbname+' where ord_id='+convert(varchar(10),@ordid) +char(13)+char(10) 
end 
set @myi = 1 
while (@myi<300) 
begin
 if (charindex('#detail'+convert(varchar(10),@myi),@newsql)>0) 
 begin
set @extsql = 'exec drop_temp_table ''#detail'+convert(varchar(10),@myi)+''' '+char(13)+char(10) 
set @extsql = @extsql +' select * into #detail'+convert(varchar(10),@myi)+' from '+@tbname+'_'+convert(varchar(10),@myi)+' where ord2_ordid='+convert(varchar(10),@ordid) +char(13)+char(10) 
 end
 set @myi = @myi + 1
end

exec drop_temp_table '#permittion' 
create table #permittion(ptype varchar(30), ctgid int , pid int , pobjid1 int , pobjid2 int) 
exec drop_temp_table '#permit' 
create table #permit(type varchar(30),cuid int) 

if (len(@freesql)>10) 
begin
set @freesql = @extsql + @freesql
exec sp_executesql @freesql, N'@cmpid int ,@useruid int ,@bizid int ,@ordid bigint , @wrkid int , @jbid bigint', 
@cmpid, @useruid , @bizid , @ordid , @wrkid , @jbid  
end
insert into #permittion(ptype,ctgid,pid,pobjid1,pobjid2)
select case when type is null or type='' then 'wrkfree' else type end,@wrkid,5,cuid,0 from #permit 
delete #permit

exec drop_temp_table '#permit' 
end

create table #myuser(pptype varchar(30),cuid int, grp int) 
exec mps_gen_powerusers @cmpid,@bcid,@dpid,@useruid,@otid,@roleid,@otlevel,@managelevel,@professionlevel,
'biz',@bizid,@ordid,@jbid,@wrkid,'wrkfree' 

declare @users table(pptype varchar(30), ppgroup int , useruid int) 
insert into @users( pptype ,ppgroup,useruid) 
select distinct pptype ,grp , cuid from #myuser 

drop table #myuser

exec drop_temp_table '#permittion' 
exec drop_temp_table '#permit' 

select distinct a.* from mps_employee a , @users where    cu_id=useruid order by cu_name 
 
set nocount off
end