473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Query with different WHERE clauses

Hi guys.

I have a database of R/C Helicopters.

The user wants to filte down on a specific helicopter configuration.
So the GUI has a screen with 5 dropdownlists.

ESC (Electric Speed Controller), Motor, Gyro, Engine and Servo.

Lets say the user wants to find all helicopters using a Z80 motor. So,
he selects that from a drop down, and the rest remain as 'Select
one..'.

So I pass paramteres to my stored procedure....

sp_find_aircraft esc=0, motor=67, gyro=0, engine=0, servo=0
(67 being the ID of the component he wants to look up.. in this case,
the Z80 engine).

How do I create the where clause for this? My select looks like this:

SELECT ua.id, c.[Model]
FROM dbo.[UserAircraft] ua
LEFT JOIN dbo.[UserAircraftComponent] uac1 ON uac1.useraircraftid =
ua.id
INNER JOIN dbo.[Component] c ON c.id = uac1.componentid

I have no where clause yet, as I'm not sure how to handle dffering
selections.

In the above example, my where clause would simply be:

WHERE c.id = 67

And that would return me a correct recordset of all UserAircraft
having a Z80 engine.

But now the user wants a result with the Z80, as well as maybe a GY401
gyro... so now I pass:
sp_find_aircraft esc=0, motor=67, gyro=145, engine=0, servo=0

I kinda need a dynamic where clause....
Is this possible?

A kind of:
IF @gyro 0
add 'OR Componentid=145

type of thing.
Aug 20 '08 #1
2 1588
Cralis (ad***@myschoolmates.com) writes:
Lets say the user wants to find all helicopters using a Z80 motor. So,
he selects that from a drop down, and the rest remain as 'Select
one..'.

So I pass paramteres to my stored procedure....

sp_find_aircraft esc=0, motor=67, gyro=0, engine=0, servo=0
(67 being the ID of the component he wants to look up.. in this case,
the Z80 engine).

How do I create the where clause for this? My select looks like this:
There are a couple of options, and I have an article (in two versions) on
my web site that discusses this in detail:
http://www.sommarskog.se/dyn-search.html.

By the way, do not use the sp_ prefix for your stored procedures. This
prefix is reserved for system procedures.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 20 '08 #2
Thanks Erland!
Agh! Forgot about the poor use of 'sp_'.
Foolish - will resolve, thanks.
Aug 20 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Wescotte | last post by:
Here is a small sample program I wrote in PHP (running off Apache 1.3.31 w/ PHP 5.0.1) to help illustrates problem I'm having. The data base is using DB2 V5R3M0. The client is WinXP machine using...
4
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and...
22
by: Robert Brown | last post by:
suppose I have the following table: CREATE TABLE (int level, color varchar, length int, width int, height int) It has the following rows 1, "RED", 8, 10, 12 2, NULL, NULL, NULL, 20...
10
by: Susan M. | last post by:
I'm trying to do a query that joins two tables. The trick is that I only want it to return rows based on a certain criteria. Table 1: Inventory Fields: Inventory #, Description Table 2:...
2
by: Lyn | last post by:
Hi, How do you bind the output columns from a UNION query when the fields from the two tables have different names? Consider this query (WHERE clauses omitted)... SELECT SurnameBirth,...
14
by: Darin | last post by:
I have a table that I want to delete specific records from based on data in other tables. I'm more familiar with Access '97, but am now using 2003, but the database is in 2000 format. In '97, I...
8
by: GeorgeSmiley | last post by:
Does anyone know of a way, via VBA, to set the screen position of query results to a particular top, left position? I've glanced at API techniques but cannot find exactly what will do the trick....
6
by: blue875 | last post by:
A tale of two queries: One query looks like this, and runs fine. We'll call this the "Customer1 query": SELECT Customer1 overall.*, IIf(IsNull(.),0,1) AS IsField, IIf(IsNull(.),0,1) AS...
1
by: annroberti | last post by:
I have web page that users use to specify sort and selection critieria for a query. So there is a "most common" type set of where clauses, but they can change. Every time the query is run, there...
0
theGeek
by: theGeek | last post by:
Hi All!! Please look at the query below: select * from tab1 inner join tab2 on tab1.c1 = tab2.c1 inner join
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.