SELECT dependant on parameter value

  • Hi,

    Is there a way (through case or if statements i assume) to select rows from a table depending on a parameters value?

    i.e.

    @customerID int

    if customerID = (certainValue)

    select * from Table where customerID = @customerID

    else

    select * from table

    Thanks for your time

    Lee

  • select * from Table

    where customerID = @customerID OR @customerID IS NULL

    _____________
    Code for TallyGenerator

  • SELECT * FROM myTable

    WHERE customerID = CASE

    WHEN @customerID = myValue THEN myValue

    ELSE customerID

    END



    Terri

    To speak algebraically, Mr. M. is execrable, but Mr. C. is
    (x+1)-ecrable.
    Edgar Allan Poe
    [Discussing fellow writers Cornelius Mathews and William Ellery Channing.]

  • select * from Table

    where customerID = ISNULL(@customerID, customerID)

    if the customerID is not NULLable field.

    ---------------------------------------------
    [font="Verdana"]Nothing is impossible.
    It is just a matter of time and money.[/font]

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

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