IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_order_allabel]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_order_allabel]
GO
create proc [dbo].[mps_gen_order_allabel]
(
@cmpid int
)
AS
declare @bqid varchar(max)
declare @sql varchar(max)
declare @bizid int
declare @count int
BEGIN
    --删除旧的存储过程
    
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
set @sql=''
select name from sys.procedures where name like 'order_%_%_query' or name like 'order_%_%_query_simple';
select @sql+='drop procedure '+name+';' from sys.procedures where  name like 'order_%_%_query' or name like 'order_%_%_query_simple';
print @sql
exec(@sql)
--查询所有业务单id
    create table #temp(bizid int)
insert into #temp select distinct bql_bizid from mps_biztype_query
select @count=count(bizid) from #temp
--循环所有业务但id
while @count>0
begin
select top 1 @bizid=bizid from #temp
select @bqid=stuff((select ','+cast(bql_id as varchar) from mps_biztype_query bq where bq.bql_bizid=@bizid  for xml path('')),1,1,'')+',' 
declare @m int, @n int, @id varchar(20)
set @m = charindex(',',@bqid)
set @n = 1
while(@m > 0)
begin
  set @id=substring(@bqid,@n,@m-@n)
  set @n = @m + 1
  set @m = charindex(',',@bqid,@n)
  -- 得到 id
  if cast(@id as int) <> 0
  begin
  --生成查询存储过程
  exec mps_gen_order_bizquery @cmpid,@bizid,@id
  exec mps_gen_order_bizquery_simple @cmpid,@bizid,@id
  end
end
delete from #temp where bizid=@bizid
set @count=@count-1
end
drop table #temp
END