Advertisement

05.02.2007 at 02:05AM PDT, ID: 22546998
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.6

Stored Procedure and the Like statement

Asked by PaulEll in SQL Server 2005, MS SQL Server

Tags: , , ,

Hi there,

I'm using and SQL2005 server and asp 2.

I am trying to filter / search for records using a stored procedure, based on one or more search criteria
supplied by a user.

The asp code below creates a whereclause that is used to call the stored procedure.

Where a value has not been searched on or selected the value of that field is NULL.

If a customers types "a" then I want to return all the customers starting with the letter "a".
A further selection might then request all customers from a town beginning with "t" etc etc.
You get the picture.

session("Cust_WC") = "@CustomerName='a', "
session("Cust_WC") = session("Cust_WC") & "@CustomerAdd1=NULL, "
session("Cust_WC") = session("Cust_WC") & "@CustomerTown=NULL, "
session("Cust_WC") = session("Cust_WC") & "@CustomerCounty=NULL, "
session("Cust_WC") = session("Cust_WC") & "@CustomerPostCode=NULL, "
session("Cust_WC") = session("Cust_WC") & "@CustomerCountry=NULL, "
session("Cust_WC") = session("Cust_WC") & "@CustomerType=NULL, "
session("Cust_WC") = session("Cust_WC") & "@CustomerAccount=NULL "

' Debug Code
response.write session("Cust_WC") + "<br>"

' Open a connection to the database
Set db = Server.CreateObject("ADODB.Connection")
db.Open "driver={SQL Server};server=xx.xx.xx.xx;database=xx","xx","xx"

' Count all the records based on the user listbox selection
set rs = db.execute("EXEC Customer_Count " & session("Cust_WC"))
MyTotalRecs = rs("CountofCustomerID")
rs.close
set rs = nothing
set db = nothing

' Debug Code
response.write "Total Records Returned = " &  MyTotalRecs


The stored procedure I am using will only work if I supply the whole customer name to the stored procedure call.

ALTER PROCEDURE [dbo].[Customer_Count]

@CustomerName nchar(100),
@CustomerAdd1 nchar(100),
@CustomerTown nchar(30),
@CustomerCounty nchar(30),
@CustomerPostCode nchar(10),
@CustomerCountry nchar(15),
@CustomerType nchar(30),
@CustomerAccount nchar(20)

AS

BEGIN
    SELECT Count(CustomerID) AS CountOfCustomerID

    FROM Customers

    WHERE (@CustomerName IS NULL OR CustomerName like @CustomerName + '%')
    AND ( @CustomerAdd1 IS NULL OR CustomerAdd1 = @CustomerAdd1)
    AND ( @CustomerTown IS NULL OR CustomerTown = @CustomerTown)
    AND ( @CustomerCounty IS NULL OR CustomerCounty = @CustomerCounty)
    AND ( @CustomerPostCode IS NULL OR CustomerPostCode = @CustomerPostCode)
    AND ( @CustomerCountry IS NULL OR CustomerCountry = @CustomerCountry)
    AND ( @CustomerType IS NULL OR CustomerType = @CustomerType)
    AND ( @CustomerAccount IS NULL OR CustomerAccount = @CustomerAccount)

END

Please can you advise where I am going wrong or if there is another way of working this idea.

Regards
PaulStart Free Trial
[+][-]05.02.2007 at 02:08AM PDT, ID: 19014327

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: SQL Server 2005, MS SQL Server
Tags: procedure, statement, stored, like
Sign Up Now!
Solution Provided By: angelIII
Participating Experts: 1
Solution Grade: A
 
 
[+][-]05.02.2007 at 02:30AM PDT, ID: 19014397

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20081112-EE-VQP-44