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
180 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
Comment Utility
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
Comment Utility
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
Comment Utility
Missed it by that much...
0
 
LVL 1

Accepted Solution

by:
Kumawat_Shobha earned 500 total points
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now