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

CREATE procedure [dbo].[mps_gen_biz_ord2search](
@cmpid int,
@bizid int,
@ord2tag int 
)
as
begin
set nocount on
declare @s1 nvarchar(100) 
declare @detail varchar(100)
declare @order2 varchar(100)
declare @tbname varchar(50)
declare @dbname varchar(50)
declare @fkey   varchar(50) 
declare @ord2 varchar(10) 
set @ord2 = convert(varchar(10),@ord2tag) 

exec sys_sql_getTableNamePk @cmpid,'biz',@bizid,0,@tbname output,@fkey output ,@dbname output 
declare @ss nvarchar(MAX) 
declare @sql nvarchar(MAX) 
declare @procparam nvarchar(MAX) 
declare @searchfields varchar(max)
declare @str varchar(max)
declare @site int
declare @site1 int
declare @s nvarchar(MAX)
declare @s2 nvarchar(MAX)

set @ss = ''

SELECT @searchfields=ISNULL(@searchfields+',','')+fldi_fieldname 
FROM (SELECT DISTINCT fldi_fieldname FROM mps_fieldinfo
where fldi_cmpid = @cmpid and fldi_tbtype = 'biz' and fldi_tbid = @bizid and fldi_ord2tag = @ord2tag and fldi_fieldname>'') 
AS T

SELECT @STR=ISNULL(@STR+',','')+fldi_datatype 
FROM (SELECT fldi_datatype FROM mps_fieldinfo
where fldi_cmpid = @cmpid and fldi_tbtype = 'biz' and fldi_tbid = @bizid and fldi_ord2tag = @ord2tag and fldi_datatype>'') 
AS T

while @searchfields > ''
begin
-- 获取逗号位置
set @site = charindex(',',@searchfields);
set @site1 = charindex(',',@STR);
if @site = 0
begin
set @s = @searchfields;
set @s2 = @STR;
set @searchfields = ''
end
else
begin
set @s = LEFT(@searchfields, @site-1);
set @s2 = LEFT(@STR, @site1-1);
set @searchfields = SUBSTRING(@searchfields,@site+1,LEN(@searchfields))
set @STR = SUBSTRING(@STR,@site1+1,LEN(@STR))
end
-- 判断是否是无关字段
if (@s = 'ord2_fatherord2id' or @s = 'ord2_fbizid' or @s = 'ord2_ford2id' or @s = 'ord2_ford2tag' or @s = 'ord2_fordid' or @s = 'ord2_ftype' or @s = 'ord2_id' or @s = 'ord2_key' or @s = 'ord2_ordid' or @s = 'ord2_rbizid' or @s = 'ord2_rord2id' or @s = 'ord2_rord2tag' or @s = 'ord2_rordid' or @s = 'ord2_rtype' or @s = 'ord2_tags' or @s = 'ord2_tag')
begin
set @ss = @ss
end 
else
begin
if (@ss>'') set @ss =  @ss+' or ' 
if (@s2 = 'datetime')
begin
set @ss = @ss   +char(13)+char(9)+ ' (convert(varchar(100),'+ @s +', 21) like @key1 and '  
+'convert(varchar(100),'+ @s +', 21) like @key2 and '  
+'convert(varchar(100),'+ @s +', 21) like @key3)'  
end
else
begin
set @ss = @ss +char(13)+char(9)+ ' (' + @s + ' like @key1 and '  
+@s + ' like @key2 and '  
+ @s + ' like @key3)'  
end
end
end

if (len(@ss)<5) set @ss = ' (1=1) ' 

set @sql = N'
-- 生成时间:'+convert(varchar(20),getdate(),120)+N' by ebs-core  
'
if (dbo.checkprocedure(@tbname+'_'+@ord2+'_search')=0) set @sql=@sql+'alter ' else set @sql=@sql+'create '
set @sql = @sql+' procedure '+@tbname+N'_'+@ord2+'_search'+N'(
@ordid bigint,
@key nvarchar(200),
@pagesize int 
) 
as  
begin  
set nocount on 
-- 将@key 分解为三个值
declare @key1 nvarchar(200)
declare @key2 nvarchar(200)
declare @key3 nvarchar(200)
set @key1=''''
set @key2=''''
set @key3=''''
declare @i int 
set @key1 = @key 
set @key1 = replace(@key1,''  '','' '') 
set @key1 = replace(@key1,''  '','' '') 
set @key1 = replace(@key1,''  '','' '') 
set @key1 = replace(@key1,'',,'','','') 
set @key1 = replace(@key1,'',,'','','') 
set @key1 = replace(@key1,'',,'','','') 
set @key1 = replace(@key1,'','','' '') 
set @key1 = replace(@key1,''  '','' '') 
set @key1 = replace(@key1,''  '','' '') 
set @i = charindex('' '',@key1) 
if (@i>0) 
begin
set @key2 = substring(@key1,@i+1,200) 
set @key1 = substring(@key1,1,@i-1) 
end 
if (@key2>'''') 
begin
set @i = charindex('' '',@key2) 
if (@i>0) 
begin
set @key3 = substring(@key2,@i+1,200) 
set @key2 = substring(@key2,1,@i-1) 
end 
end 
if (@key1='''') set @key1=''%'' 
if (charindex(''%'',@key1)<=0) set @key1=''%''+@key1+''%''

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

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

select top(@pagesize) * from '+@dbname+@tbname+'_'+CONVERT(varchar(10),@ord2tag)+'  
where ord2_ordid=@ordid and ('  
+ @ss  +') '+char(13)+char(9)
+ 'order by ord2_order, ord2_id 
select ord2_id  as ord2id_page from '+@dbname+@tbname+'_'+CONVERT(varchar(10),@ord2tag) +' 
where ord2_ordid=@ordid and ('  
+ @ss  + ') '+char(13)+char(9)
+ 'order by ord2_order, ord2_id 

set nocount off
end '
exec myprint @sql 
execute(@sql)  
-- 生成mps_sql 
set @sql = ' exec mps_sys_gen_mpssql '+CONVERT(varchar(10),@cmpid)+','''+@tbname+N'_'+@ord2+'_search'',
''@ordid bigint,@key nvarchar(200),@pagesize int''' 
execute(@sql)
set nocount off 
end