Enquiry on retrieving data from database

  • Hi,

    I have a drop down menu for transmission. Inside the drop down menu, there are 1.Any 2.Auto 3.Manual options

    Now I want to retrieve from my database, if the client select '1.Any' option. Meaning the output I want it to display all the products which have both auto and manual for transmission.

    I am not sure what is the coding should be like. Can any one help me out with this.

    I have no problem retrieving all the products which either only have auto option or manual option.

    Your help is greatly appreciated. Thank you very much.

  • Hello and welcome to the forums.

    If they select Any, you don't want to apply the WHERE clause. If another one is selected, you want to apply the WHERE clause. Since I don't know what your table looks like, here's a general approach. It's probably much simpler than what you're after, but it's the approach that counts:

    --create a table to hold the parts where TransmissionID denotes the type

    create table #parts (

    ID Integer not null identity (1, 1),

    Description varchar(32),

    TransmissionID Integer);

    --depending on @TransmissionID passed to the procedure, query the parts list

    IF @TransmissionID IS NULL

    BEGIN

    SELECT ID, Description

    FROM #parts

    ORDER BY Description;

    END;

    ELSE

    BEGIN

    SELECT ID, Description

    FROM #parts

    WHERE TransmissionID = @TransmissionID

    ORDER BY Description;

    END;

    HTH

  • You haven't told us what you are using for querying the database? Are you using SSRS, .NET, java? What are you using for database access, ad hoc SQL, an ORM tool (Entity Framework, nHibernate, Hibernate), or stored procedures?

    So even without the answer to those questions here's a generic answer to your original post. At some point you need to interrogate the choice that was made and if it is "Any" don't add a where clause to your query. So in pseudo code it would be something like this:

    If Parameter Value = "Any" THEN

    query = Select columns from table

    Else

    query = Select columns from table where transmission = parameter.value

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply