Print Page | Close Window

SQL - If Statements

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Database Discussion
Forum Description: Discussion and chat on database related topics.
URL: https://forums.webwiz.net/forum_posts.asp?TID=17042
Printed Date: 29 March 2026 at 4:41am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: SQL - If Statements
Posted By: pjb007
Subject: SQL - If Statements
Date Posted: 26 October 2005 at 3:16pm
Can anyone help here, is there a better way to do this code
 


if request.querystring("state") = "uk" then
sSQL="SELECT * FROM links_news WHERE country = 'UK' ORDER BY siteTitle "
end if
if request.querystring("state") = "us" then
sSQL="SELECT * FROM links_news WHERE country = 'America' ORDER BY siteTitle "
end if
if request.querystring("state") = "ie" then
sSQL="SELECT * FROM links_news WHERE country = 'Ireland' ORDER BY siteTitle "
end if

if request.querystring("state") = "world" then
sSQL="SELECT * FROM links_news WHERE country = 'World' ORDER BY siteTitle "
end if 
if request.querystring("state") = "other" then
sSQL="SELECT * FROM links_news WHERE country = 'other' ORDER BY siteTitle "
end if
 
Also could anyone help with another statement that will show everything if the stats is not part of the URL?



Replies:
Posted By: dfrancis
Date Posted: 26 October 2005 at 4:34pm

if request.querystring("state") = "uk" then
 sSQL="SELECT * FROM links_news WHERE country = 'UK' ORDER BY siteTitle "
elseif request.querystring("state") = "us" then
 sSQL="SELECT * FROM links_news WHERE country = 'America' ORDER BY siteTitle "
elseif request.querystring("state") = "ie" then
 sSQL="SELECT * FROM links_news WHERE country = 'Ireland' ORDER BY siteTitle "
elseif request.querystring("state") = "world" then
 sSQL="SELECT * FROM links_news WHERE country = 'World' ORDER BY siteTitle "
elseif request.querystring("state") = "other" then
 sSQL="SELECT * FROM links_news WHERE country = 'other' ORDER BY siteTitle "
Else
 sSQL="SELECT * FROM links_news ORDER BY siteTitle "
End If

 
 
OR
 

Select case request.querystring("state")

 Case "uk"
   sSQL="SELECT * FROM links_news WHERE country = 'UK' ORDER BY siteTitle "
 Case "us"
   sSQL="SELECT * FROM links_news WHERE country = 'America' ORDER BY siteTitle "
 Case "ie"
   sSQL="SELECT * FROM links_news WHERE country = 'Ireland' ORDER BY siteTitle "
 Case "world"
   sSQL="SELECT * FROM links_news WHERE country = 'World' ORDER BY siteTitle "
 Case "other"
   sSQL="SELECT * FROM links_news WHERE country = 'other' ORDER BY siteTitle "
 ELSE
   sSQL="SELECT * FROM links_news ORDER BY siteTitle "

End Select



Posted By: pjb007
Date Posted: 26 October 2005 at 5:13pm
Thanks I went for the first one, excellent. Smile


Posted By: Gullanian
Date Posted: 26 October 2005 at 8:04pm
Second way is probably the better, select cases are more efficient.


Posted By: theSCIENTIST
Date Posted: 15 November 2005 at 6:52pm
Why all that code?

Why not this:


Dim qState
qState = Trim(Request.QueryString("state"))

'// DB connection
'// Open a RecordSet

sSQL="SELECT * FROM links_news WHERE country = '" & qState & "' ORDER BY siteTitle "

Set RS = Conn.Execute(sSQL)

If RS.EOF Then Response.Write("Sorry mate, state not in our records or not recognised!")


By the way avoid using the (*) to select all columns, instead name the columns even if you want to retrieve all of them, the reason for this is that by specifying the (*) you force the database server to first enumerate all columns names in that table, then rebuild the query with it. This doesn't happen if you name the columns in the first place.

-------------
:: http://www.mylittlehost.com/ - www.mylittlehost.com


Posted By: MadDog
Date Posted: 16 November 2005 at 12:03am
qState = Trim(Request.QueryString("state"))


should be (to prevent sql injection):

qState = Replace(Request.QueryString("state"), "'", "''")


-------------
http://www.iportalx.net" rel="nofollow">



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net