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
182 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 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

914 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

18 Experts available now in Live!

Get 1:1 Help Now