?
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
?
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

801 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