Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Simple SQL Query (MSSQL2K)

Options
  • 07-04-2004 10:41am
    #1
    Closed Accounts Posts: 333 ✭✭


    I have a stored procedure that executes a number of SQL statements, but I was wondering if it is possible to do a comparison to "*" for all records.

    To explain.
    I know that this returns all records from a table
    "SELECT * FROM tblCustomers"

    I know that if I wished to retrieve a particular customer it would br
    "SELECT * FROM tblCustomers WHERE cust_code = @Customer"
    @Customer is the input parameter passed up from the application.

    But instead of doing this ...
    If (@Customer = 'All')
    "SELECT * FROM tblCustomers"
    Else
    "SELECT * FROM tblCustomers WHERE cust_code = @Customer"

    I would like to leave just one line
    "SELECT * FROM tblCustomers WHERE cust_code = @Customer"
    and if the customer wanted all records, it would read something like
    "SELECT * FROM tblCustomers WHERE cust_code = *"

    The above example is simplified and the real example is very complicated.

    Cheers


Comments

  • Registered Users Posts: 1,421 ✭✭✭Merrion


        SELECT * FROM tblCustomers 
        WHERE  cust_code = @Customer 
              OR   @Customer = 'ALL'
    


  • Registered Users Posts: 2,781 ✭✭✭amen


    SELECT * FROM tblCustomers
    WHERE cust_code like CASE @Customer
    WHEN 'ALL' THEN 'ALL
    ELSE @Customer
    end

    this does what you want
    not the most efficient query the use of the like is not really a good idea
    why do you need to do this?
    maybe rethink what you are trying to do ?


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    I'm not sure that code will work, amen.

    Its close though.
    SELECT  *
      FROM  tblCustomers
      WHERE cust_code = CASE @Customer 
                          WHEN 'ALL' THEN cust_code
                          ELSE @Customer
                        END
    

    McGinty - what this does :

    when the parameter is 'ALL', then you end up with a WHERE clause which says :

    where cust_code = cust_code

    and when its not all, you end up with :

    where cust_code = @cust_code

    The only significant disadvantage is that on a large table, these should ideally be optimised differently. In the first case (ALL), the correct optimisation would be to ignore the where clause and do a table-scan. In the second case, the correct optimisation would be to use an index lookup on the cust_code field.

    If the performance is a problem, you'd probbly be better going back to seperate queries rather than trying to tweak the optimisation....but thats just an opinion.

    jc


  • Registered Users Posts: 2,781 ✭✭✭amen


    bonkey you are right
    I meant to have cust_code


  • Closed Accounts Posts: 333 ✭✭McGintyMcGoo


    Thanks for the help guys!! :D


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    No problem....although its reminded me that I have a half-written SQL manual that I should get around to finishing some of these days.

    I love writing manuals. Oh joy.

    jc


Advertisement