?
Solved

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

Posted on 2008-10-14
4
Medium Priority
?
201 Views
Last Modified: 2012-05-05
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~
0
Comment
Question by:salman_sulaiman_2008
  • 2
4 Comments
 
LVL 23

Expert Comment

by:adathelad
ID: 22709268
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

0
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 22709278
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

0
 
LVL 16

Expert Comment

by:GreymanMSC
ID: 22709287
Missed it by that much...
0
 
LVL 1

Accepted Solution

by:
Kumawat_Shobha earned 2000 total points
ID: 22709311
Make your procedure like this:
IF OBJECT_ID('GetRecords') IS NOT NULL
    DROP PROCEDURE GetRecords
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE GetRecords
(
      @accountNO      INTEGER,
      @paymentMethod      VARCHAR(100),
      @status            VARCHAR(100)
)
AS
BEGIN
      BEGIN TRY
            --$Header: $
            SET NOCOUNT ON
            
            SELECT      *
            FROM      <table_name>
            WHERE      AccountNO      =      ISNULL(@accountNO, AccountNO)
            AND      PaymentMethod      =      ISNULL(@paymentMethod, PaymentMethod)
            AND      Status            =      ISNULL(@status, Status)

            RETURN 0
      END TRY
      BEGIN CATCH
            RETURN -1
      END CATCH
END

Note: in case when "ALL" is selected pass "null" as a value for that paramenter.
it will surely work for you
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question