IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_createproc_g_reportdata]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_createproc_g_reportdata]
GO
-- =============================================
-- Author:  flobit
-- Create date: May 11 2018  2:33PM
-- Description: 生成获取报表数据的存储过程
-- =============================================
CREATE PROCEDURE [dbo].[mps_gen_createproc_g_reportdata] 
@cmpid int,
@rpt_id int 
AS
BEGIN
SET NOCOUNT ON;
declare @source varchar(max)
declare @proc_name varchar(512) ='report_'+CONVERT(varchar,@rpt_id)+'_data'
select @source=rpt_sql from mps_report where rpt_id=@rpt_id
declare @sql_create_proc varchar(max)=' PROCEDURE '+@proc_name+char(13)+char(10)
declare @sql_param_type varchar(max)='int|int|int|int|int|varchar(8000)|varchar(8000)|int'
declare @sql_param_list varchar(max)='{cmpid},{branchid},{userdpid},{userotid},{useruid},{tablename},{pageobjids},{pagesize}'
declare @temp_data varchar(max) = @source
declare @command varchar(max)=''
declare @sql_gen varchar(max)=''
declare @sql nvarchar(max)=''
--1. 处理SQL中包含的参数
--加上平台基本参数
set @sql_create_proc+='@cmpid int,@branchid int,@userdpid int,@userotid int,@useruid int,@tableid varchar(8000),@objids varchar(8000),@pagesize int'
declare @index1 int = 1 
declare @index2 int = 1 
declare @paramName varchar(max)

set @temp_data=@source
WHILE @index1!=0
BEGIN
set @index1 = CHARINDEX('{',@source)
set @index2 = CHARINDEX('}',@source)

if(@index1!=0 and @index2!=0 and @index2 > @index1)
begin
set @paramName=ltrim(rtrim(SUBSTRING(@source,@index1+1,@index2-@index1-1)))
--set @source = SUBSTRING(@source,@index2+1,LEN(@source))
if(@paramName>'' and @paramName not in ('cmpid','branchid','userdpid','userotid','useruid','tableid','objids','pagesize'))
begin
set @sql_create_proc+=',@'+@paramName+' varchar(512)'+char(13)+char(10)
set @source=REPLACE(@source,'{'+@paramName+'}','@'+@paramName)
set @sql_param_type+='|varchar(512)'
set @sql_param_list+=',{'+@paramName+'}'
end
end
END
--参数处理完毕
set @sql_create_proc+=char(13)+char(10)+'AS'+char(13)+char(10)+'BEGIN'+char(13)+char(10)+' set nocount on '+char(13)+char(10)
--申明@dataIds表
set @sql_create_proc+=char(13)+char(10)+'declare @dataIds table(objid int)'
set @sql_create_proc+=char(13)+char(10)+'if @objids!=''_NONE'' and @objids!=''_none_'' and @objids!='''' '
set @sql_create_proc+=char(13)+char(10)+'insert into @dataIds(objid)'
set @sql_create_proc+=char(13)+char(10)+'select value from dbo.fn_split_int(@objids)'+char(13)+char(10)

-- 处理SQL中包含的伪命令

while 1=1
begin
set @index1 = CHARINDEX('makeUpPagingSql(',@source)
if @index1=0 break;
set @command = dbo.fn_parsing_makeupsql(SUBSTRING(@source,@index1,LEN(@source)))
set @index2 = LEN(@command)
--字符串前半段
set @temp_data = SUBSTRING(@source,1,@index1-1)

--生成获取第一个页面的SQL
set @sql='set @sql_gen=dbo.'+@command
set @sql=SUBSTRING(@sql,1,LEN(@sql)-1)+',0)' --删除结尾处的 ),然后补上函数需要的type
--字符串后半段
set @source=SUBSTRING(@source,@index1+@index2,LEN(@source))
exec sp_executesql @sql,N'@sql_gen varchar(max) out',@sql_gen out
--将生成的字符串还原到源SQL
set @source=@temp_data+char(13)+char(10)+@sql_gen+@source

--生成获取后续页面的SQL
set @sql=SUBSTRING(@sql,1,LEN(@sql)-3)+',1)' --删除结尾处的 ),然后补上函数需要的type
exec sp_executesql @sql,N'@sql_gen varchar(max) out',@sql_gen out
--合并到存储过程开头
set @sql_create_proc+=@sql_gen
--print @source
end

--将整理后的SQL合并到创建语句中
set @sql_create_proc+=char(13)+char(0)+@source

--补全存储过程结尾的END
set @sql_create_proc=CASE WHEN OBJECT_ID('report_'+CONVERT(varchar,@rpt_id)+'_data') IS NOT NULL then 'ALTER' else 'CREATE' end +@sql_create_proc
set @sql_create_proc+=char(13)+char(10)+'END';
print '---------------------------------------------'
--创建 or 修改存储过程
execute(@sql_create_proc)
--更新mps_sql
update mps_sql set sql_text=@proc_name+' '+@sql_param_list,sql_params=@sql_param_type where sql_name=@proc_name
if @@ROWCOUNT = 0
insert into mps_sql(sql_cmpid,sql_name,sql_type,sql_text,sql_params)
values(@cmpid,@proc_name,'MSSQL',@proc_name+' '+@sql_param_list,@sql_param_type)
print @sql_create_proc
END