IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[mps_sys_checkkeywords]') AND type = 'FN')
DROP FUNCTION [dbo].[mps_sys_checkkeywords]
GO
CREATE function [dbo].[mps_sys_checkkeywords](
@name varchar(100) ,
@objid int,
@objid2 int,
@keywords1 varchar(100)  ,
@keywords2 varchar(100)  
)
RETURNS int 
AS
BEGIN
declare @name1 varchar(50)  
declare @name2 varchar(50)

declare @key1 varchar(100)
declare @key2 varchar(100)

declare @key11 varchar(100)
declare @key22 varchar(100)


declare @i int 
declare @res1 int 
set @res1 = 1 

set @i = charindex(',',@name) 
if (@i>0) 
begin
set @name1 = substring(@name , 1 , @i - 1 ) 
set @name2 = substring(@name , @i+1 , len(@name)-@i) 
end else 
begin
set @name1 = @name 
set @name2 = @name 
end

set @i = charindex(',',@keywords1) 
if (@i>0) 
begin
set @key1 = substring(@keywords1,1,@i-1) 
set @keywords1 = substring(@keywords1 , @i+1 , len(@keywords1)-@i) 
set @i = charindex(',',@keywords1) 
if (@i>0) 
begin
set @key2 = substring(@keywords1,1,@i-1) 
set @keywords1 = substring(@keywords1 , @i+1 , len(@keywords1)-@i) 
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
((cd_text like '%'+@keywords1+'%') or (cd_text like '%'+@key1+'%') or (cd_text like '%'+@key2+'%')))) set @res1 = 0 
set @i = charindex(',',@keywords2) 
if (@i>0) 
begin
set @key11 = substring(@keywords2,1,@i-1) 
set @keywords2 = substring(@keywords2 , @i+1 , len(@keywords2)-@i) 
set @i = charindex(',',@keywords2) 
if (@i>0) 
begin
set @key22 = substring(@keywords2,1,@i-1) 
set @keywords2 = substring(@keywords2 , @i+1 , len(@keywords2)-@i) 
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
((cd_text like '%'+@keywords1+'%') or (cd_text like '%'+@key1+'%') or (cd_text like '%'+@key2+'%')) and ((cd_text like '%'+@keywords2+'%') or (cd_text like '%'+@key11+'%') or  (cd_text like '%'+@key22+'%')))) set @res1 = 0 
end else
begin
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
((cd_text like '%'+@keywords1+'%') or (cd_text like '%'+@key1+'%') or (cd_text like '%'+@key2+'%')) and (cd_text like '%'+@keywords2+'%' or cd_text like '%'+@key11+'%'))) set @res1 = 0 
end
end else 
begin
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
((cd_text like '%'+@keywords1+'%') or (cd_text like '%'+@key1+'%') or (cd_text like '%'+@key2+'%')) and cd_text like '%'+@keywords2+'%')) set @res1 = 0 
end
end else 
begin

set @i = charindex(',',@keywords2) 
if (@i>0) 
begin
set @key11 = substring(@keywords2,1,@i-1) 
set @keywords2 = substring(@keywords2 , @i+1 , len(@keywords2)-@i) 
set @i = charindex(',',@keywords2) 
if (@i>0) 
begin
set @key22 = substring(@keywords2,1,@i-1) 
set @keywords2 = substring(@keywords2 , @i+1 , len(@keywords2)-@i) 
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
((cd_text like '%'+@keywords1+'%') or (cd_text like '%'+@key1+'%')) and (cd_text like '%'+@keywords2+'%' or cd_text like '%'+@key11+'%' or  cd_text like '%'+@key22+'%'))) set @res1 = 0 
end else
begin
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
((cd_text like '%'+@keywords1+'%') or (cd_text like '%'+@key1+'%')) and (cd_text like '%'+@keywords2+'%' or cd_text like '%'+@key11+'%'))) set @res1 = 0 
end
end else 
begin
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
((cd_text like '%'+@keywords1+'%') or (cd_text like '%'+@key1+'%')) and cd_text like '%'+@keywords2+'%')) set @res1 = 0 
end 

end
end else
begin
set @i = charindex(',',@keywords2) 
if (@i>0) 
begin
set @key11 = substring(@keywords2,1,@i-1) 
set @keywords2 = substring(@keywords2 , @i+1 , len(@keywords2)-@i) 
set @i = charindex(',',@keywords2) 
if (@i>0) 
begin
set @key22 = substring(@keywords2,1,@i-1) 
set @keywords2 = substring(@keywords2 , @i+1 , len(@keywords2)-@i) 
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
cd_text like '%'+@keywords1+'%' and (cd_text like '%'+@keywords2+'%' or cd_text like '%'+@key11+'%' or cd_text like '%'+@key22+'%'))) set @res1 = 0 
end else
begin
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
cd_text like '%'+@keywords1+'%' and (cd_text like '%'+@keywords2+'%' or cd_text like '%'+@key11+'%'))) set @res1 = 0 
end
end else 
begin
if (exists (select 1 from mps_extsqljs where cd_objid=@objid and cd_objid2=@objid2 and 
(cd_name=@name1 or cd_name=@name2) and len(convert(varchar(100),cd_text))>10 and 
cd_text like '%'+@keywords1+'%' and cd_text like '%'+@keywords2+'%')) set @res1 = 0 
end 
end


RETURN(@res1) 
END