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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Search Expression.....

Author  Topic 

financemanager
Starting Member

9 Posts

Posted - 2008-03-30 : 10:22:24
Hi All,

Firstly thank you for taking time out and looking at this topic.

I have a problem implementing a stored procedure......

What i need to do is using a Stored Procedure pass a user defined search expression and return results from a table.

basically user searches for a company name and based on other variables the procedure returns the relevant results.

the search expression can be a combo of things.....

Common company names....

COMPANY A and ATMS
company name
COMPANY A and COMPANY B
COMPANY A or COMPANY B


Thankyou for all your help.

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-30 : 11:04:03
i would suggest that you don't do these kinds of searches with a stored procedure.
complex searches are one of the few places i reccomend using dynamicaly built parametrized sql in the app.
you can of course create dynamic sql in your sproc but you have to be carefull not to allow sql injection.

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

financemanager
Starting Member

9 Posts

Posted - 2008-03-30 : 11:23:26
Hi,

I would agree however i have been given this task to do and this is how they want it done. i just dont know where to start.....

i declare the variable and am lost i dont kow the select script for it....

Select * from companyTBL where company = @search_expression

But it needs to be dynamic....

I want to search on any given word in the search... with AND OR's...

Please help
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-30 : 11:28:43
well you can't really do this dynamicaly inthe way you wish to.
what exactly would be the user input?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

financemanager
Starting Member

9 Posts

Posted - 2008-03-30 : 11:55:24
Hi thanks for getting back to me....

The user would input the name of the company or a combo of words for example......

INPUT 1: BP
INPUT 2: BP AND SHELL
INPUT 3: SHELL OR BP

I just need the correct way compile the SQL Script and Stored procedure.....

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-03-30 : 12:22:10
may be this:-
Select * from companyTBL
where (PATINDEX('%AND%',company)>0 AND company like '%' + REPLACE(REPLACE(@search_expression,' ',''),'AND','%') + '%' )
OR (PATINDEX('%OR%',company)>0 AND company like '%[' + REPLACE(REPLACE(@search_expression,' ',''),'OR',']*%[') + ']*%' )
OR (PATINDEX('%AND%',company)=0 AND PATINDEX('%OR%',company)=0 AND company = @search_expression)
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-30 : 12:22:34
this would requrie that you parse the input in the sproc and turn it into something like this:
(you might use paramteres)

INPUT 1: BP
set @where = 'where companyName = ''BP'''

INPUT 2: BP AND SHELL
set @where = 'where companyName = ''BP'' and companyName = ''shell''' <- this is also a bit ridicolous since a column can't have 2 values at the same time.

INPUT 3: SHELL OR BP
set @where = 'where companyName = ''BP'' or companyName = ''shell'''

after you parse your string you do
exec('select * from yourTable ' + @where)

you also have to find a way to handle mistypes and malicious tries (sql injection).

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

financemanager
Starting Member

9 Posts

Posted - 2008-03-30 : 13:40:57
thank you for all that has posted to my question, but what I really need is how to do a simple proximity search in sql

Kind Regards

Pete
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-30 : 13:58:35
and in your case that would be what?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

financemanager
Starting Member

9 Posts

Posted - 2008-03-30 : 14:03:40
i have the code for user defined company search and implemented it successfully, now i want to be able to narrow the search on a geographical location..

Kind Regards

Pete
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-30 : 14:12:40
wait wait... this is then a completly different issue than the one you started this thread with?


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

financemanager
Starting Member

9 Posts

Posted - 2008-03-30 : 14:14:58
Sorry am new to this.....When i started the post i had a problem with a dynamic search but has been sorted since but now have issue with proximity search and need help with that.....if i should have started another threat please advise sorry if i should have....

Kind Regards

Pete
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-30 : 14:20:04
yeah start another thread.
how did you resolve the dynamic search?

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

financemanager
Starting Member

9 Posts

Posted - 2008-03-30 : 14:22:58
please see post by visakh16 great input from him thankyou mate.....and thanks to all who viwed the post....

Kind Regards

Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-03-30 : 14:29:24
as much as it's a great suggestion and a pretty cool solution, good luck debugging that when you have problems...

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

Imukai
Starting Member

29 Posts

Posted - 2008-03-31 : 06:47:31
Is it too early in the morning for me to fully comprehend visakh16 or is that solution going to cause issues whenever you search for a company name that happens to contain the letters "AND" or "OR" ?
Go to Top of Page
   

- Advertisement -