MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old January 23rd, 2004, 10:08 AM
layz d layz d is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 39 layz d User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 6 m 28 sec
Reputation Power: 5
Question about the WHERE statement

Hi all,
I have a page with 5 drop down menus. These drop downs act like filters for whatever data the user wants to retrieve from my database. Once submitted, it then goes to my asp page which displays the records based on what the user filtered from the previous page.

Here is the question.

How do i form my WHERE statement so that it returns the records based on the filters? Like If the user only decides to filter using 1 of the drop downs instead of all 5. If I use the AND statement, it trys to return all records that have a blank in them because one of the drop downs were left blank. Therefore returning no records. If I use the OR statement then it returns all records that meet drop down 1 and all records that meet drop down 2. What i want is someway to tell my SQL statement to only return records that meet drop down 1 and drop down 2 but when drop down 2 is blank, dont search for blank records in the database. Kind of like an And/Or statement. I hope that makes sense.

Here is my current SQL statement:

"SELECT YKPC_EXT_Orders.ACCOUNTANT, YKPC_EXT_Orders.FULL_NAME, YKPC_EXT_Orders.CL, YKPC_EXT_Orders.RF, YKPC_EXT_Orders.RFGL, YKPC_EXT_Orders.SITE, YKPC_EXT_Orders.PL, YKPC_EXT_Orders.[ACTUAL AMOUNT], YKPC_EXT_Orders.[TARGET AMOUNT], YKPC_EXT_Orders.[PRICE VAR], YKPC_EXT_Orders.[MATL USAGE], YKPC_EXT_Orders.[OTHER USAGE], YKPC_EXT_Orders.[PROCESS CHG VAR], YKPC_EXT_Orders.[VOL VAR] FROM YKPC_EXT_Orders WHERE (((YKPC_EXT_Orders.ACCOUNTANT)='" & Request.Form("txtAcct") & "')) OR (((YKPC_EXT_Orders.CL)='" & Request.Form("txtClient") & "'));"

thanks

Reply With Quote
  #2  
Old January 23rd, 2004, 11:02 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 47 sec
Reputation Power: 870
the answer is to start your WHERE clause like this --

... WHERE 1=1

then in your script, you can test each variable for a value, and if a value has been selected, then you generate an AND like this --

... AND foo = 'bar'

that way, you do not have to wory about which value is the first one selected, or even if any were selected, because if none were selected, then WHERE 1=1 will return all the rows in the table

see also The "any" option in dynamic search SQL
__________________
r937.com | rudy.ca

Reply With Quote
  #3  
Old January 28th, 2004, 10:38 AM
layz d layz d is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 39 layz d User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 6 m 28 sec
Reputation Power: 5
thanks alot.
however thay article i found to be just a little confusing.

Does this modified SQL Line look alright??

strSQL = "SELECT YKPC_EXT_Orders.ACCOUNTANT, YKPC_EXT_Orders.FULL_NAME, YKPC_EXT_Orders.CL, YKPC_EXT_Orders.RF, YKPC_EXT_Orders.RFGL, YKPC_EXT_Orders.SITE, YKPC_EXT_Orders.PL, YKPC_EXT_Orders.[ACTUAL AMOUNT], YKPC_EXT_Orders.[TARGET AMOUNT], YKPC_EXT_Orders.[PRICE VAR], YKPC_EXT_Orders.[MATL USAGE], YKPC_EXT_Orders.[OTHER USAGE], YKPC_EXT_Orders.[PROCESS CHG VAR], YKPC_EXT_Orders.[VOL VAR] FROM YKPC_EXT_Orders" & _
" WHERE 1=1" & _
" If strCboAcct <> 'Show All' Then" & _
" AND (((YKPC_EXT_Orders.ACCOUNTANT)='" & strCboAcct & "'))" & _
" End If" & _
" If strCboCl <> 'Show All' Then" & _
" AND (((YKPC_EXT_Orders.CL)='" & strCboCl & "'))" & _
" End If"

I am getting an Unspecified Error whenever i try to load the page.

thanks

Reply With Quote
  #4  
Old January 28th, 2004, 10:57 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 47 sec
Reputation Power: 870
you have

" WHERE 1=1" & _
" If strCboAcct <> 'Show All' Then" & _

this makes your If test part of the sql string

what you have to do is terminate the string after WHERE 1=1, then do your if test, and for the true condition,

strSQL = strSQL & " AND (((YKPC_EXT_Orders.ACCOUNTANT)='" & strCboAcct & "'))"

Reply With Quote
  #5  
Old January 28th, 2004, 11:53 AM
layz d layz d is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 39 layz d User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 6 m 28 sec
Reputation Power: 5
r937, thank you for your help.

This is my new statement:

strAcct = Request.Form("txtAcct")
strClient = Request.Form("txtClient")

strSQL = "SELECT YKPC_EXT_Orders.ACCOUNTANT, YKPC_EXT_Orders.FULL_NAME, YKPC_EXT_Orders.CL, YKPC_EXT_Orders.RF, YKPC_EXT_Orders.RFGL, YKPC_EXT_Orders.SITE, YKPC_EXT_Orders.PL, YKPC_EXT_Orders.[ACTUAL AMOUNT], YKPC_EXT_Orders.[TARGET AMOUNT], YKPC_EXT_Orders.[PRICE VAR], YKPC_EXT_Orders.[MATL USAGE], YKPC_EXT_Orders.[OTHER USAGE], YKPC_EXT_Orders.[PROCESS CHG VAR], YKPC_EXT_Orders.[VOL VAR] FROM YKPC_EXT_Orders WHERE 1=1"

'Accountant Filter
If strAcct <> "Show All" Then
strSQL = strSQL & " AND (((YKPC_EXT_Orders.ACCOUNTANT)='" & strAcct & "'))"
End If

'CL Filter
If strClient <> "Show All" Then
strSQL = strSQL & " AND (((YKPC_EXT_Orders.CL)='" & strClient & "'))"
End If



Do you see anything wrong with this statement? For some reason it will not return any results anymore when searching for an accountant and leaving the client field's value = Show All.

I double checked using response.write to make sure i was bringing in the right field names and i am.

thanks

Reply With Quote
  #6  
Old January 28th, 2004, 11:58 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 24th Plane (16500 - 16999 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 16,743 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 2 Days 21 h 15 m 47 sec
Reputation Power: 870
leave both fields as "show all" -- it should return all rows

if not, you have a problem in your ASP, and i don't do ASP, sorry

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Question about the WHERE statement


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway