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

CREATE procedure [dbo].[sys_sql_getTableNamePk](
@cmpid int,
@biztype varchar(10),
@bizid int,
@tagid int,
@tablename varchar(30) output,
@pkfld varchar(30) = default output ,
@databasename varchar(30)=default output 
)
as
begin
declare @biz_table int,@tbid int,@s varchar(50)
select @tbid=@bizid
set @databasename = '' 
declare @btype varchar(10) 
set @btype=LOWER(@biztype) 
set @tablename = ''
set @pkfld = ''

if (@bizid>100000000 and @bizid<500000000)
begin
set @bizid = @bizid - (@bizid/100000000)*100000000
end else 
if (@bizid>500000000)
begin
set @bizid = @bizid - (@bizid/100000000)*100000000
if (@btype='cst') select top 1 @bizid=ct_tag from mps_csttype where ct_id=@bizid 
if (@btype='prd') select top 1 @bizid=pc_tag from mps_prdctg where pc_id=@bizid 
if (@btype='equ') select top 1 @bizid=ec_tag from mps_equctg where ec_id=@bizid 
if (@btype='etc') select top 1 @bizid=etc_tag from mps_ext_resource where etc_id=@bizid 
end 
select @tbid=@bizid
if (@btype='cst' and @bizid<0)   -- 如果@bizid<0 表示是通过ct_id 
begin
select @tbid=ct_tag  from mps_csttype where  ct_cmpid= @cmpid and ct_id= @bizid * (-1) 
end 
if (@btype='equ' and @bizid<0)   -- 如果@bizid<0 表示是通过ct_id 
begin
select @tbid=ec_tag  from mps_equctg where  ec_cmpid= @cmpid and ec_id= @bizid * (-1) 
end 
if (@btype='prd' and @bizid<0)   -- 如果@bizid<0 表示是通过ct_id 
begin
select @tbid=pc_tag  from mps_prdctg where  pc_cmpid= @cmpid and pc_id= @bizid * (-1) 
end 
if (@btype='etc' and @bizid<0)   -- 如果@bizid<0 表示是通过ct_id 
begin
select @tbid=etc_tag  from mps_ext_resource where  etc_cmpid= @cmpid and etc_id= @bizid * (-1) 
end 

if (@bizid<0)
set @bizid=9999999
if (@tagid<0)
set @tagid=9999999



if (@btype = 'biz')
begin
select @biz_table=biz_table,@tbid=biz_id , @databasename=biz_database 
from mps_biztype where biz_cmpid= @cmpid and  biz_id= @bizid 

if (@biz_table>0) 
begin
select @biz_table=biz_table,@tbid=biz_id , @databasename=biz_database 
from mps_biztype where  biz_cmpid= @cmpid and biz_id= @biz_table
end 

set @databasename=isnull(@databasename,'') 
if (@databasename>'') set @databasename = @databasename +'.dbo.' 

select @tablename='order_'+convert(varchar(10),@tbid),@pkfld='ord_id'
--明细表表名
if (@tagid>0)
select @tablename=@tablename+ '_' + convert(varchar(10),@tagid),@pkfld='ord2_id'
end


if (@btype = 'emp')
begin
select @tablename='mps_employee',@pkfld='cu_id'
end

if (@btype = 'doc')
begin
select @tablename='mps_document',@pkfld='doc_id'
end

if ((@btype = 'dpt'))
begin
select @tablename='mps_depart',@pkfld='dp_id'
end

if ((@btype = 'shp'))
begin
select @tablename='mps_depart',@pkfld='dp_id'
end

if ((@btype = 'brch'))
begin
select @tablename='mps_branch',@pkfld='bc_id'
end

if ((@btype = 'private'))
begin
select @tablename='privateview_'+CONVERT(varchar(10),@bizid),@pkfld='objid'
end

if ((@btype = 'web'))
begin
select @tablename='zsvc_temp_'+CONVERT(varchar(10),@bizid),@pkfld='svc_id'
end

if ((@btype = 'rfc'))
begin
select @tablename='zsvc_temp_'+CONVERT(varchar(10),@bizid),@pkfld='svc_id'
end

if ((@btype = 'prd'))
begin
if (@tbid=0)  select @tablename='mps_prd',@pkfld='prd_id' 
else select @tablename='mps_prd'+convert(varchar(10),@tbid),@pkfld='prd_id' 
if (@tagid>0)select @tablename=@tablename+ '_' + convert(varchar(10),@tagid),@pkfld='prd2_id'
end

if ((@btype = 'equ'))
begin
if (@tbid=0)  select @tablename='mps_equ',@pkfld='equ_id' 
else select @tablename='mps_equ'+convert(varchar(10),@tbid),@pkfld='equ_id' 
if (@tagid>0)select @tablename=@tablename+ '_' + convert(varchar(10),@tagid),@pkfld='equ2_id'
end

if ((@btype = 'etc'))
begin
if (@tbid=0)  select @tablename='mps_etc',@pkfld='tec_id' 
else select @tablename='mps_etc'+convert(varchar(10),@tbid),@pkfld='etc_id' 
if (@tagid>0)select @tablename=@tablename+ '_' + convert(varchar(10),@tagid),@pkfld='etc2_id'
end

 

if ((@btype = 'cst'))
begin
set @databasename='' 
select top 1 @databasename=ct_database from mps_csttype where  ct_cmpid= @cmpid and ct_tag= @tbid 
set @databasename=isnull(@databasename,'') 
if (@databasename>'') set @databasename = @databasename +'.dbo.' 
if (@tbid=0)  select @tablename='mps_customer',@pkfld='cc_id' 
else select @tablename='mps_customer'+convert(varchar(10),@tbid),@pkfld='cc_id' 
if (@tagid>0)select @tablename=@tablename+ '_' + convert(varchar(10),@tagid),@pkfld='cc2_id'
end
if ((@btype = 'user'))
begin
set @databasename='' 
select top 1 @databasename=pt_database from mps_programtype where  pt_cmpid= @cmpid and pt_id= @tbid 
set @databasename=isnull(@databasename,'') 
if (@databasename>'') set @databasename = @databasename +'.dbo.' 
select @tablename='user_'+convert(varchar(10),@tbid),@pkfld='usr_id' 
if (@tagid>0)select @tablename=@tablename+ '_' + convert(varchar(10),@tagid),@pkfld='usr2_id'
end


if ((@btype = 'wrk'))
begin
select @tablename='mps_flowwork',@pkfld='wrk_id'
end

if ((@btype = 'fee'))
begin
select @tablename='mps_account_item',@pkfld='ai_id'
end

if ((@btype = 'barn'))
begin
select @tablename='mps_warehouse',@pkfld='wh_id'
end

if ((@btype = 'svc'))
begin
select @tablename='zsvc_temp_'+CONVERT(varchar(10),@bizid),@pkfld='svc_id'
end

end