IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_gen_bizquery_simple]') AND type = 'P')
DROP PROCEDURE [dbo].[mps_gen_bizquery_simple]
GO
create procedure [dbo].[mps_gen_bizquery_simple](
@cmpid int,
@bizid int
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 

exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
    -- 招聘状态=ord_zpzt,申请人=ord_zpgw,责任部门=ord_zrbm,责任公司=ord_zrgs
    declare @searchfields varchar(max),@sql varchar(max), @s varchar(255), 
            @site int, @fieldtype varchar(50), @sqlpart varchar(max),
            @fldisize int, @fldidecimal int, @sqlpart2 varchar(max),
            @sqlnextpage varchar(max);

    declare @orders varchar(max);
    declare @o2 varchar(100);
    
    set @orders = '';
    -- 初始化sql语句
set @sqlnextpage = N' 
set nocount on  

declare @nonedate datetime 
set @nonedate = CONVERT(datetime,''1910-1-1'') 
declare @s1 varchar(8000) 
declare @s2 varchar(100) 
declare @i int 
declare @amount int 
select @amount = @pagecount*@pagesize 
declare @pobjs table(nid int identity(1, 1), ordid bigint) 
set @s1 = @pageobjids 
while (@s1>'''') 
begin 
set @i = charindex('','', @s1) 
if (@i>0) 
begin 
set @s2 = substring(@s1,1,@i-1) 
set @s1 = substring(@s1,@i+1,len(@s1)-@i) 
end else  
begin 
set @s2 = @s1 
set @s1 = '''' 
end 
insert into @pobjs(ordid) values(convert(bigint,@s2)) 
end 
if (exists (select top 1 * from @pobjs)) 
begin 
select a.* from '+@dbname+@tbname+' a  with(nolock) , @pobjs
where ord_id=ordid
order by nid 
select a.* from '+@dbname+@tbname+'_users a  with(nolock) , @pobjs 
where ordp_type<>''bizview'' and ordp_ordid=ordid and ordp_cuid=@useruid
return 
end 
if (@status is null or @status = ''_none_'') set @status = '''' 

';



    set @sql = ''
    set @sqlpart =  CHAR(10) +CHAR(9)+ '@cmpid int,'+CHAR(13) +CHAR(9)+ 
                    '@branchid int,'+CHAR(13) + CHAR(9)+ 
                    '@userdpid int,'+CHAR(13) + CHAR(9)+ 
                    '@userotid int ,'+CHAR(13) + CHAR(9)+ 
                    '@useruid int ,'+CHAR(13) +CHAR(9)+ 
'@bizid int ,'+CHAR(13) +CHAR(9)+ 
'@finished int ,'+CHAR(13) +CHAR(9)+ 
'@status nvarchar(50),'+CHAR(13) + CHAR(9)+ 
                    '@pagesize int , '+CHAR(13) + CHAR(9)+ 
                    '@pagecount int , '+CHAR(13) + CHAR(9)+ 
                    '@pageobjids varchar(8000),'+CHAR(13) + CHAR(9) +
'@date1 datetime ,'+CHAR(13) + CHAR(9) +
'@date2 datetime ,'+CHAR(13) + CHAR(9) +
'@key1 nvarchar(50),'+CHAR(13) + CHAR(9) +
'@key2 nvarchar(50),'+CHAR(13) + CHAR(9) +
'@key3 nvarchar(50)'+CHAR(13) + CHAR(9)
    set @sqlpart2 = '';
    declare @bizselect varchar(max)
    declare @bizfrom varchar(max)
    declare @bizwhere varchar(max)
    declare @bizorderby varchar(max)
    declare @alltable varchar(max)

    set @alltable = ''
    -- 查询出searchfield
-- 招聘状态=ord_zpzt,申请人=ord_zpgw,责任部门=ord_zrbm,责任公司=ord_zrgs
select top 1 @searchfields=biz_searchfields, @bizselect = biz_select,@bizfrom =biz_from,@bizwhere = biz_where, @bizorderby=biz_orderby 
from mps_biztype with(nolock) 
where biz_id=@bizid 
    set @searchfields = isnull(@searchfields,'') 

if(@bizselect is null or @bizselect = '')
begin
set @bizselect = 'a.*'
end

if(@bizwhere is null or @bizwhere = '')
begin
set @bizwhere = ''
end
else
begin
set @bizwhere = '   ('+ @bizwhere +') ' +char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)
end

if(@bizfrom is null or @bizfrom = '')
begin
set @alltable = ''
end
else
begin
set @bizfrom  = REPLACE(REPLACE(REPLACE(REPLACE(@bizfrom,CHAR(9),''),CHAR(13),''),CHAR(10),''),' ','')
declare @bizf1 varchar(max)
declare @bizsite1 int
while @bizfrom > ''
begin
-- 获取逗号位置
set @bizsite1 = charindex(',',@bizfrom);
if @bizsite1 = 0
begin
set @bizf1 = @bizfrom;
set @bizfrom = ''
end
else
begin
set @bizf1 = LEFT(@bizfrom, @bizsite1-1);
set @bizfrom = SUBSTRING(@bizfrom,@bizsite1+1,LEN(@bizfrom))
end
-- 判断是否是两个主表@dbname+@tbname
if(@bizf1 != @dbname+@tbname and @bizf1 != @dbname+@tbname+'_users')
begin
set @alltable = @alltable + @bizf1 + ','
end
end
end

    if (@searchfields='') set @searchfields='ord_date=ord_date,ord_no=ord_no,ord_title=ord_title,ord_status=ord_status,ord_creator=ord_creator' 

declare @oldsflds nvarchar(1000) 
set @oldsflds = '' 
declare @datecount int 
set @datecount = 0
declare @datestr nvarchar(1000) 
set @datecount = 0

    while @searchfields > ''
    begin
        -- 获取逗号位置
        set @site = charindex(',',@searchfields);
        if @site = 0
        begin
            set @s = @searchfields;
            set @s1 = right(@s,len(@s)-charindex('=',@s));
            set @searchfields = ''
        end
        else
        begin
            set @s = LEFT(@searchfields, @site-1);
            set @s1 = right(@s,len(@s)-charindex('=',@s));
            set @searchfields = SUBSTRING(@searchfields,@site+1,LEN(@searchfields))
        end
set @o2 = '';

if (@s1!='' and charindex(','+@s1+',',@oldsflds)<=0)  
begin
set @oldsflds = ','+@oldsflds+','+@s1+','
if @s1 != 'ord_date'
begin
set @o2 = ',' + @s1 ;
end

        -- 查询字段类型
        select top 1 @fieldtype = fldi_datatype,
        @fldisize = fldi_size, 
        @fldidecimal = fldi_decimal
        from mps_fieldinfo  with(nolock) 
        where fldi_fieldname = @s1 and fldi_cmpid=@cmpid and fldi_tbtype='biz' and fldi_tbid=@bizid and fldi_ord2tag=0 
        if @fieldtype is null
        begin
            set @fieldtype = 'varchar';
            set @fldisize = '255'
        end
if @fieldtype = 'datetime' or @fieldtype = 'date' --日期型
        begin
set @datecount = @datecount + 1
if (@datecount=1) --第一次出现日期字段
begin
set @datestr = +char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ '(' + @s1 + ' >= @date1 and '+@s1+' <= @date2 ) and '
end else
begin
set @sql = @sql +char(10)+ CHAR(9)+ CHAR(9)+ CHAR(9)+ ' (' + @s1 + ' like @key1 and '+@s1+' like @key2 and '+ @s1 + ' like @key3 ) or '
end
if @s1 != 'ord_date'
begin
set @o2 = ',' + @s1 + ' desc';
end
        end else
begin
set @sql = @sql +char(10)+ CHAR(9)+  CHAR(9)+ CHAR(9)+' (' + @s1 + ' like @key1 and '+@s1+' like @key2 and '+ @s1 + ' like @key3 ) or '
end

        set @orders = @orders + @o2;
        end 
    end

if(@datecount=0)
begin
set @datestr = char(10)+ CHAR(9)+  CHAR(9)+ CHAR(9)+' (ord_date >= @date1 and ord_date <= @date2 ) and '
end
if LEN(@sql)>2
begin
set @sql = '('+SUBSTRING(@sql,1,LEN(@sql)-2)+')';
set @sql =  @sql + ' and ' + @datestr
end else
begin
set @sql =  @datestr
end

set @sql = SUBSTRING(@sql,1,LEN(@sql)-3)
    -- 如果排序
if(@bizorderby is null or @bizorderby = '')
begin
set @bizorderby = 'order by ord_date desc'+@orders+char(13)
end else
begin
set @bizorderby = 'order by ' + @bizorderby +char(13)
end
if LEN(@bizwhere) >0
begin
set @bizwhere = ' and ' + @bizwhere
end

set @sql = @sql + @bizwhere+char(10)+ CHAR(9)+  CHAR(9)+ CHAR(9)

    declare @sqlnextend varchar(max);
    set @sqlnextend = char(10)+
'
if (@key1='''') set @key1=''%'' 
if (charindex(''%'',@key1)<=0 and @key1>'''') set @key1=''%''+@key1+''%''

if (@key2='''') set @key2=''%'' 
if (charindex(''%'',@key2)<=0 and @key2>'''') set @key2=''%''+@key2+''%''

if (@key3='''') set @key3=''%'' 
if (charindex(''%'',@key3)<=0 and @key3>'''') set @key3=''%''+@key3+''%''

if (@finished = -11)
begin
if (@pagesize=10000)  -- 下载
begin
select '+@bizselect+' from '+@alltable+@dbname+@tbname+' a  with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_cmpid=@cmpid 
and a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and a.ord_finished > 0 
and '+ @sql+@bizorderby+'
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.ord_id  from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and a.ord_finished > 0
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select count(1) resultcount  from datalinker  with(nolock) where useruid=@useruid 
end else
begin
select count(1) cnt from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and a.ord_finished > 0
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and a.ord_finished > 0
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@bizselect+' from '+@alltable+@dbname+@tbname+' a with(nolock) ,@pobjs 
where a.ord_id=ordid '+ @bizwhere+'
and (a.ord_status=@status or @status='''')
and a.ord_finished > 0
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a with(nolock)  , @pobjs 
where ordp_type<>''bizview'' and ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end else
if (@finished = -10)
begin
if (@pagesize=10000)  -- 下载
begin
select '+@bizselect+' from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_cmpid=@cmpid 
and a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and '+ @sql+@bizorderby+'
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.ord_id  from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select count(1) resultcount  from datalinker with(nolock)  where useruid=@useruid 
end else
begin
select count(1) cnt from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@bizselect+' from '+@alltable+@dbname+@tbname+' a with(nolock) ,@pobjs 
where a.ord_id=ordid '+ @bizwhere+'
and (a.ord_status=@status or @status='''')
and (a.ord_finished>0 and a.ord_createcuid=@useruid) 
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a  with(nolock) , @pobjs 
where ordp_type<>''bizview'' and ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end else
if (@finished = 2)
begin
if (@pagesize=10000)  -- 下载
begin
select '+@bizselect+' from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_cmpid=@cmpid 
and a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and a.ord_finished>=2 
and '+ @sql+@bizorderby+'
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.ord_id  from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and a.ord_finished>=2 
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select count(1) resultcount  from datalinker with(nolock)  where useruid=@useruid 
end else
begin
select count(1) cnt from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and a.ord_finished>=2 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock) 
where a.ord_id=ordp_ordid 
and a.ord_finished>=2 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@bizselect+' from '+@alltable+@dbname+@tbname+' a with(nolock) ,@pobjs 
where a.ord_id=ordid '+ @bizwhere+'
and (a.ord_status=@status or @status='''')
and a.ord_finished>=2 
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a  with(nolock) , @pobjs 
where ordp_type<>''bizview'' and ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end else
if (@finished = 1)  -- 待办事项 
begin
if (@pagesize=10000)  -- 下载
begin
select '+@bizselect+' from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock)  , mps_job with(nolock) 
where a.ord_cmpid=@cmpid 
and a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and (a.ord_finished>0 and a.ord_finished<=1)
and jb_cmpid=@cmpid and jb_crmid='+convert(varchar(10),@bizid)+' and jb_ordid=ord_id  
and jb_cuid=@useruid and jb_wsid=0 and jb_isauto=1 
and '+ @sql+@bizorderby+'
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker    where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.ord_id  from '+@alltable+@dbname+@tbname+' a with(nolock) ,'+@dbname+@tbname+'_users  with(nolock)  , mps_job with(nolock) 
where a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_finished>0 and a.ord_finished<=1)
and (a.ord_status=@status or @status='''') 
and jb_cmpid=@cmpid and jb_crmid='+convert(varchar(10),@bizid)+' and jb_ordid=ord_id  
and jb_cuid=@useruid and jb_wsid=0 and jb_isauto=1 
and '+ @sql+@bizorderby+'
select count(1) resultcount  from datalinker where useruid=@useruid 
end else
begin
select count(1) cnt from '+@alltable+@dbname+@tbname+' a with(nolock),'+@dbname+@tbname+'_users   with(nolock)  , mps_job with(nolock) 
where a.ord_id=ordp_ordid 
and (a.ord_finished>0 and a.ord_finished<=1)
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and jb_cmpid=@cmpid and jb_crmid='+convert(varchar(10),@bizid)+' and jb_ordid=ord_id  
and jb_cuid=@useruid and jb_wsid=0 and jb_isauto=1 
and (a.ord_status=@status or @status='''') 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from '+@alltable+@dbname+@tbname+' a with(nolock),'+@dbname+@tbname+'_users  with(nolock)  , mps_job with(nolock) 
where a.ord_id=ordp_ordid 
and (a.ord_finished>0 and a.ord_finished<=1)
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and jb_cmpid=@cmpid and jb_crmid='+convert(varchar(10),@bizid)+' and jb_ordid=ord_id  
and jb_cuid=@useruid and jb_wsid=0 and jb_isauto=1 
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@bizselect+' from '+@alltable+@dbname+@tbname+' a with(nolock),@pobjs 
where a.ord_id=ordid '+ @bizwhere+'
and (a.ord_status=@status or @status='''')
and (a.ord_finished>0 and a.ord_finished<=1) 
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a  with(nolock), @pobjs 
where ordp_type<>''bizview'' and ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end else
if (@finished = -1)
begin
if (@pagesize=10000)  -- 下载
begin
select '+@bizselect+' from '+@alltable+@dbname+@tbname+' a with(nolock),'+@dbname+@tbname+'_users  with(nolock)
where a.ord_cmpid=@cmpid 
and a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and ord_finished=-1 
and '+ @sql+@bizorderby+'
end else 
if (@pagesize>=100000000)  --  转存到中间表
begin
set @pagesize = @pagesize - 100000000 
if (@pagesize<=0) set @pagesize = 100000000 
delete datalinker where useruid = @useruid 
insert into datalinker(useruid,objid) 
select top(@pagesize) @useruid,a.ord_id  from '+@alltable+@dbname+@tbname+' a with(nolock),'+@dbname+@tbname+'_users  with(nolock)
where a.ord_id=ordp_ordid 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and ord_finished=-1 
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select count(1) resultcount   from datalinker with(nolock) where useruid=@useruid 
end else
begin
select count(1) cnt from '+@alltable+@dbname+@tbname+' a with(nolock),'+@dbname+@tbname+'_users  with(nolock)
where a.ord_id=ordp_ordid 
and ord_finished=-1 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and '+ @sql+'
-- 返回所有ID
insert into @pobjs(ordid) select top(@amount) ord_id from '+@alltable+@dbname+@tbname+' a with(nolock),'+@dbname+@tbname+'_users  with(nolock)
where a.ord_id=ordp_ordid 
and ord_finished=-1 
and ordp_type=''bizview'' 
and ordp_cuid=@useruid 
and (a.ord_status=@status or @status='''') 
and '+ @sql+@bizorderby+'
select ordid from @pobjs 
order by nid 
-- 返回第一页 
select top(@pagesize) '+@bizselect+' from '+@alltable+@dbname+@tbname+' a  with(nolock),@pobjs 
where a.ord_id=ordid '+ @bizwhere+'
and (a.ord_status=@status or @status='''')
and ord_finished=-1 
order by nid 
-- 返回权限 
select a.* from '+@dbname+@tbname+'_users a  with(nolock) , @pobjs 
where ordp_type<>''bizview'' and ordp_ordid=ordid 
and nid<=@pagesize 
and ordp_cuid=@useruid 
end 
end 
set nocount off '+char(13)

    --生成存储过程
    --判断某存储过程是否存在
    declare @psql nvarchar(max) 
    if (dbo.checkprocedure(@tbname+'_query_simple')=0) set @psql='alter ' else set @psql='create '
    set @psql = @psql+' procedure '+@tbname+N'_query_simple('+@sqlpart+') '+char(13)+'as '+char(9) +char(13)+
    'begin '+char(9) +char(13)+@sqlnextpage+@sqlpart2 +' '+char(9)+@sqlnextend +char(13)+
    'end' 
    exec myprint @psql 
    execute(@psql)
    -- 生成mps_sql 
    set @psql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_query_simple'',
        ''' +@sqlpart+''''
    execute(@psql)

set nocount off 
end