Link to home
Start Free TrialLog in
Avatar of salman_sulaiman_2008
salman_sulaiman_2008

asked on

I can't figure out how to create an sql statement for searching records in a stored procedure??

Greetings Experts!
I have been facing trouble trying to figure out how to write a stored procedure that would carry out a serach in a table and return the records that match the needed criteria. I'll explain what I am trying to do, I have a web page on which the user selects from dropdownlist controls the search criteria, and then I send the paramters to a stored procedure which I am using in a report to display the resulted records, I am using sql server report deigner and report services.
The problem is in the search criteria, there are so many, but they are mainly in three catargories:
1- searching based on the Accounts types (the table field is: unique_acc_no)
2- searching based on the payment methods (the table field is: payment_method)
3- searching based on the account status (the table field is: status)

the thing is, those dropdownlist controls have beside all the possible values, they also have one item which represent all possible values, so either the user selects one specific value from the list, or simply select the first item which is "ALL" which represents all possible values.
I just don't know how on earth would I implement this in sql server, it seems like a complex sql statement is required.

I wish someone could guide me a little bit on how to tackle this problem. I really appreciate any help.
Thanks in advance.
~salman~
Avatar of adathelad
adathelad
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

You can do something like this:


SELECT * 
FROM YourTable
WHERE (@AccountsType = 'ALL' OR unique_acc_no = @AccountsType)
    AND (@PaymentMethod = 'ALL' OR payment_method = @PaymentMethod)
    AND (@Status = 'ALL' OR status = @Status)

Open in new window

Avatar of GreymanMSC
GreymanMSC

The Select Statement is not really complex at all.  Simple use the DataValueField from the dropdown boxes as follows.
1) using 'ALL'
 
SELECT * FROM Accounts AS A WHERE ((A.unique_acc_no=@unique_acc_no) OR (@unique_acc_no='ALL')) AND ((A.payment_method=@payment_method) OR (@payment_method='ALL')) AND ((A.status=@status) OR (@status='ALL'));
 
2) Using '' and ConvertEmptyStringToNull=True
 
SELECT * FROM Accounts AS A WHERE ((A.unique_acc_no=@unique_acc_no) OR (@unique_acc_no IS NULL)) AND ((A.payment_method=@payment_method) OR (@payment_method IS NULL)) AND ((A.status=@status) OR (@status IS NULL));
 
3) Or something along those lines...

Open in new window

Missed it by that much...
ASKER CERTIFIED SOLUTION
Avatar of Kumawat_Shobha
Kumawat_Shobha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial