IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizdownloaddetail]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizdownloaddetail]
GO
create procedure [dbo].[mps_gen_bizdownloaddetail](
@cmpid int,
@bizid int
)
as
begin
set nocount on
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @ord2tag2 int 
exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 

declare @sql nvarchar(MAX) 
 
 -- 以下代码是生成子表临时表
 declare c1 cursor LOCAL FORWARD_ONLY READ_ONLY for
select distinct biz2_tag  from mps_biztype2  where biz2_bizid=@bizid 
open c1
FETCH NEXT FROM c1 INTO @ord2tag2 
WHILE (@@FETCH_STATUS = 0)
BEGIN

if (dbo.checkprocedure(@tbname+'_'+convert(varchar(10),@ord2tag2)+'_download')=0) set @sql='alter ' else set @sql='create '
set @sql = @sql+' procedure '+@tbname+N'_'+convert(varchar(10),@ord2tag2)+'_download('+char(13)+char(10)+
'@cmpid int ,'+char(13)+char(10)+
'@ordid bigint) '+char(13)+char(10)+
'as '+char(13)+char(10)+
'begin '+char(13)+char(10)+
'set nocount on'+char(13)+char(10)+
'select * from '+ @dbname+@tbname + '_'+convert(varchar(10),@ord2tag2)+' where ord2_ordid=@ordid order by ord2_order , ord2_id '+char(13)+char(10)+
'set nocount off '+char(13)+char(10)+
'end ' 
exec myprint @sql 
execute(@sql)
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+'_'+convert(varchar(10),@ord2tag2)+
N'_download'',''@cmpid int,@ordid bigint''' 
execute(@sql)
update mps_sql set sql_params='' where sql_name=@tbname+'_'+convert(varchar(10),@ord2tag2)+'_download' 
 FETCH NEXT FROM c1 INTO @ord2tag2 
END
CLOSE c1
DEALLOCATE c1 
 
set nocount off 
end