|
thiruna
Starting Member
41 Posts |
Posted - 2002-07-03 : 06:28:21
|
| Hello friends,I am using a stored procedure to search multiple fields in a table which is as followsCREATE PROCEDURE SP_search_user_all(@page int,@recsperpage int,@querystr varchar(100))AS-- We don't want to return the # of rows inserted-- into our temporary table, so turn NOCOUNT ONSET NOCOUNT ON--Create a temporary tableCREATE TABLE #TempItems( ID int IDENTITY, user_no int, user_name varchar(20), user_firstname varchar(50), user_email varchar(50), user_department varchar(50), user_active bit)-- Insert the rows from tblItems into the temp. tableIF @querystr IS Null BEGIN INSERT INTO #TempItems (user_no, user_name, user_firstname, user_email, user_department, user_active) SELECT user_no, user_name, user_firstname, user_email, user_department, user_active FROM users ENDELSE BEGIN Select @querystr = '%' + ltrim(rtrim(@querystr)) + '%' INSERT INTO #TempItems (user_no, user_name, user_firstname, user_email, user_department, user_active) SELECT user_no, user_name, user_firstname, user_email, user_department, user_active FROM users WHERE user_name LIKE @querystr or user_firstname LIKE @querystr or user_email LIKE @querystr or user_department LIKE @querystr END-- Find out the first and last record we wantDECLARE @FirstRec int, @LastRec intSELECT @FirstRec = (@Page - 1) * @RecsPerPageSELECT @LastRec = (@Page * @RecsPerPage + 1)-- Now, return the set of paged records, plus, an indiciation of we-- have more records or not!SELECT *, MoreRecords = ( SELECT COUNT(*) FROM #TempItems TI WHERE TI.ID >= @LastRec ) , Totalrecords = ( SELECT COUNT(*) FROM #TempItems TI )FROM #TempItemsWHERE ID > @FirstRec AND ID < @LastRec-- Turn NOCOUNT back OFFSET NOCOUNT OFFGOBut this stored procedure searches only for exact keywords we supply.. I mean if we type a keyword something like "Thiru from India", it searches only for %Thiru from India% but i want it search it as follows.....It should parse the keywords and search %thiru from India%, %thiru%, %from%, %india%Please guide me to modify the above stored procedure to search by parsing the keyword....Many thanksthiruna@vsnl.com |
|