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
189 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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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