![]() |
SQL Query Problem
I am trying to write a SQL stored procedure that will search a certain field in a table. This is what I have so far:
:
CREATE PROCEDURE pSearchRecords |
I don't think you can do that. But lets say you have Category1, Category2, Category3.
Now, you don't know what category the user will select , or if he will select all of them. So you do like this : :
That's one way of doing it ... maybe there are other |
I was going to do something like that, but there are 30 fields in this table and don't want to type it all out hahah. I might have to though, thanks.
|
Hmm, to me this sounds like a database design problem. You should never end up with 30 fields in a table each of which represents a category that you might want to search by in the same sort of way. If you designed the table structure, I recommend reading some introductory stuff about database design - the way SQL works makes so much more sense if you do the right thing as far as relational databases are concerned.
At a quick glance, this article looks like it wouldn't be a bad starting point: http://www.edm2.com/0612/msql7.html It might be a bit simple for you in places since it's starting from the very basics, and it looks like a bit of a dry read, too, but it looks really useful to me. Hope this helps! Oh, and just to confirm: you can't specify which field to use in a WHERE clause via a variable. This code: :
CREATE PROCEDURE pSearchRecordswill return nothing (empty recordset) if the values of the two variables differ, and if they are the same, it will return every row in the table. It has no way of knowing that the value in @Category is a table name; it just treats it as a value, the same way it treats @Search, so in the end your WHERE clause ends up just comparing two scalar values from the arguments, not looking at the data coming back from the table at all. P.S.: Why use T-SQL if you're addicted to Linux? :) Switch to MySQL or Postgres! I wish I could, but they'll only pay me to code for MS SQL 2000. Bah! |
Database administration
Quote:
Hey thr well i dont know much about your problem but i have a hunch that your having some sort of database issues... am a CS student and was facing same sort of issues few days back.. my frend use to visit www.oracleplace.com for soving his programming and database issues on his advice i try it on too and guess waht it helps me out... hope this will help you too. Good Luck BIA |
| All times are GMT -5. The time now is 4:08 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC