Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
maxim
Yak Posting Veteran
51 Posts |
Posted - 2006-05-09 : 13:52:37
|
I made a class in asp.net with a search engine. That class returns me one string consonant the words that I want to search and if it is for method “OR” or “AND”.If i wanted to search de word "blabla" i will send the string:*** WHERE TopiRep.Message LIKE '%blabla%' OR Topi.Subject LIKE '%blabla%' ***If i wanted to search de TWO words ("blabla" & "bleble") i will send the string:*** WHERE TopiRep.Message LIKE '%blabla%' OR Topi.Subject LIKE '%blabla%' OR TopiRep.Message LIKE '%bleble%' OR Topi.Subject LIKE '%bleble%' ***so... the procedure should be prepared for 2... 4... 6... wordsso, there is my procedure:CREATE PROCEDURE sp_Forums_SearchWord@myStr varchar(500),@PageNumber int,@PageSize intASCREATE TABLE #TempSearchWord( ID int IDENTITY PRIMARY KEY, TopicID int, Subject varchar(100), AddedDate datetime, TopicReplies int)INSERT INTO #TempSearchWord( TopicID, Subject, AddedDate, TopicReplies)SELECT DISTINCT(a.TopicID), a.Subject, a.AddedDate, a.TopicRepliesFROMv_Forums_Topics a INNER JOIN (SELECT DISTINCT(TopiRep.TopicID) FROM (SELECT Forums_Topics.TopicID, Forums_Topics.Message FROM Forums_Topics UNION ALL SELECT Forums_Replies.TopicID, Forums_Replies.Message FROM Forums_Replies) TopiRep INNER JOIN Forums_Topics Topi ON Topi.TopicID = TopiRep.TopicID@myStr --WHERE TopiRep.Message LIKE '%blabla%' OR Topi.Subject LIKE '%blabla%' ) as Results on Results.TopicID = a.TOPICID ORDER BY AddedDate DescDECLARE @FromID intDECLARE @ToID intSET @FromID = ((@PageNumber - 1) * @PageSize) + 1SET @ToID = @PageNumber * @PageSizeSELECT TopicID, Subject, AddedDate, TopicReplies FROM #TempSearchWord WHERE ID >= @FromID AND ID <= @ToIDGO Please help me to make that this procedure can receive the string of search for the some words that I to want to search. I already tried of everything without success! I get the error because the use of @myStr I also tried to insert some "exec" but i still get error's !Please, help me, it is very important to me!Thanks,Max, from Portugal |
|
X002548
Not Just a Number
15586 Posts |
|
maxim
Yak Posting Veteran
51 Posts |
Posted - 2006-05-09 : 18:03:51
|
Thanks for your time. I had put this question in some forums but nobody says anything... so... thanks!!I've tried to make the whole thing in dynamic sql, like this:CREATE PROCEDURE sp_Forums_SearchWord@myStr varchar(500),@PageNumber int,@PageSize intASDECLARE @INITSQL varchar(1500)DECLARE @ENDSQL varchar(800)SELECT @INITSQL = 'CREATE TABLE #TempSearchWord( ID int IDENTITY PRIMARY KEY, TopicID int, Subject varchar(100), AddedDate datetime, TopicReplies int)INSERT INTO #TempSearchWord( TopicID, Subject, AddedDate, TopicReplies)SELECT DISTINCT(a.TopicID), a.Subject, a.AddedDate, a.TopicRepliesFROMv_Forums_Topics a INNER JOIN (SELECT DISTINCT(TopiRep.TopicID) FROM (SELECT Forums_Topics.TopicID, Forums_Topics.Message FROM Forums_Topics UNION ALL SELECT Forums_Replies.TopicID, Forums_Replies.Message FROM Forums_Replies) TopiRep INNER JOIN Forums_Topics Topi ON Topi.TopicID = TopiRep.TopicID WHERE'-- TopiRep.Message LIKE '%blabla%' OR Topi.Subject LIKE '%blabla%' ) as Results on Results.TopicID = a.TOPICID SELECT @ENDSQL ='ORDER BY AddedDate DescDECLARE @FromID intDECLARE @ToID intSET @FromID = ((@PageNumber - 1) * @PageSize) + 1SET @ToID = @PageNumber * @PageSizeSELECT TopicID, Subject, AddedDate, TopicReplies FROM #TempSearchWord WHERE ID >= @FromID AND ID <= @ToID 'EXEC ( @INITSQL + @myStr + @ENDSQL )GO But i receive this error :System.Data.SqlClient.SqlException: Must declare the variable '@CREATE'. Must declare the variable '@PageNumber'. Must declare the variable '@PageNumber'.:( what can i do??Thanks!Max |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-05-10 : 03:15:11
|
http://www.sommarskog.se/dynamic_sql.htmlMadhivananFailing to plan is Planning to fail |
 |
|
Kristen
Test
22859 Posts |
Posted - 2006-05-10 : 06:04:54
|
ChangeEXEC ( @INITSQL + @myStr + @ENDSQL )toEXEC sp_executesql @INITSQL + @myStr + @ENDSQL, '@PageNumber int, @PageSize int', @PageNumber, @PageSize and then the query will be parameterised, which will be optimal for the Query Optimiser. (You ought to parameterise the @MyStr bit too, really)It would probably be better to move @FromID and @ToID to be parameters to sp_ExecuteSQL too, and thus move them outside the dynamic SQL.Kristen |
 |
|
maxim
Yak Posting Veteran
51 Posts |
Posted - 2006-05-10 : 07:37:39
|
thanksthe sql doesn't allow to make [CODE]EXEC sp_executesql @INITSQL + ....[/CODE]so, i chanched to:DECLARE @SQL varchar(3000)...EXEC sp_executesql @SQL, '@PageNumber int, @PageSize int', @PageNumber, @PageSize But i got this error:Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar' |
 |
|
Kristen
Test
22859 Posts |
Posted - 2006-05-10 : 07:49:12
|
I wouldn't expect it to be fussy on varchar / nvarcharBoL: "stmt must be either a Unicode constant or a variable that can be implicitly converted to ntext"can you try:DECLARE @SQL nvarchar(4000)...EXEC sp_executesql @SQL, N'@PageNumber int, @PageSize int', @PageNumber, @PageSize Kristen |
 |
|
maxim
Yak Posting Veteran
51 Posts |
Posted - 2006-05-10 : 18:46:38
|
Thank you!!!It works!!You all had been precious! |
 |
|
|
|
|
|
|