IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_g_pms_user_order]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_g_pms_user_order]
GO
CREATE procedure [dbo].[mps_g_pms_user_order](
@cmpid int,
@objtype varchar(20),
@ctgid int,
@objid bigint,
@ptype varchar(20) 
) as
begin
set nocount on 
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 
declare @ptype2 varchar(20) 
set @ptype2 = @ptype 
if (charindex('my',@ptype)=1) set @ptype2 = substring(@ptype , 3,30) 

declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
exec sys_sql_getTableNamePk @cmpid,@objtype,@ctgid,0,@tbname output,@fkey output ,@dbname output 

declare @sql nvarchar(4000) 
set @sql = N'
select a.* , cu_bcid,cu_dpid,cu_otid,cu_code,cu_depart,cu_name,cu_mobile,cu_title,cu_gender,cu_id from '+@dbname+@tbname+'_users  a , mps_employee b 
where ordp_type='''+@ptype2+''' and ordp_ordid='+convert(varchar(10),@objid)+' and ordp_otid=-2 and cu_id=ordp_cuid   ' 

exec sp_executesql @sql
set nocount off 
end