MySQL Forums
Forum List  »  Stored Procedures

Need Help with Keyword Search Stored Procedure
Posted by: Mark Rouse
Date: January 04, 2012 01:39PM

Hi,

I have the following sp:

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_keyword_search`(IN _vSearchtable varchar(255), _vDate varchar(10))
BEGIN

declare _vI int;
declare _vMax int;
declare _vFieldMin int;
declare _vFieldMax int;
declare _vSearchPattern1 varchar(3000);
declare _vTableName varchar(255);
declare _vColumnName varchar(128);
declare _vSearchPattern2 varchar(110);


-- Creates base Search Results Table
set @s= concat('CREATE TABLE IF NOT EXISTS Search_Results_',_vDate,'
(
CC_ID int(11) NULL,
ColumnName nvarchar(255) NULL,
SearchPattern nvarchar(255) NULL,
MatchedPattern nvarchar(3630) NULL
)');

prepare stmt from @s;
execute stmt;
deallocate prepare stmt;


-- Creates Temporary Keywords Table
CREATE TEMPORARY TABLE TempKeywords
(
Search_Term_ID int(11) AUTO_INCREMENT NOT NULL,
Search_Term varchar(3000) NOT NULL,
KEY tmp_Search_Term_ID (Search_Term_ID)
);

set @s = concat('insert into TempKeywords
(Search_Term)
select replace(replace(Search_Term, ''*'',''''),''?'',''_'')
from ',_vSearchtable);

prepare stmt from @s;
execute stmt;
deallocate prepare stmt;

SET _vI = (select MIN(Search_Term_ID) from TempKeywords);
SET _vMax = (select MAX(Search_Term_ID) from TempKeywords);
SET _VTableName = 'test';
SET _VColumnName = 'textfield1';

WHILE _vI <= _vMax DO

SET _vSearchPattern1 = (select Search_Term from TempKeywords where Search_Term_ID = _vI);
SET _vSearchPattern2 = concat(''%'',_vSearchPattern1,''%'');

SET @s= concat('INSERT INTO Search_Results',_vDate,'
(CC_ID, ColumnName, SearchPattern, MatchedPattern)
SELECT CC_ID,',_vColumnName,',',_vSearchPattern1,', left(',_vColumnName,', 3630)
FROM ',_vTableName,' WHERE ',_vColumnName,' LIKE ',_vSearchPattern2,'');
prepare stmt from @s;
execute stmt;
deallocate prepare stmt;

SET _vI = _vMax + 1;

END WHILE;
END

When I run this stored procedure with call sp_keyword_search('keywords','04012012');

I get the following error:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NULL' at line 1

I just can't see where this error is occuring. Any help would be greatly appreciated.

Thanks

Options: ReplyQuote


Subject
Views
Written By
Posted
Need Help with Keyword Search Stored Procedure
2726
January 04, 2012 01:39PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.