Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - SQL  - If Statements
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

SQL - If Statements

 Post Reply Post Reply
Author
pjb007 View Drop Down
Groupie
Groupie


Joined: 03 September 2004
Location: United Kingdom
Status: Offline
Points: 185
Post Options Post Options   Thanks (0) Thanks(0)   Quote pjb007 Quote  Post ReplyReply Direct Link To This Post Topic: SQL - If Statements
    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?
Back to Top
dfrancis View Drop Down
Senior Member
Senior Member


Joined: 16 March 2005
Location: United States
Status: Offline
Points: 442
Post Options Post Options   Thanks (0) Thanks(0)   Quote dfrancis Quote  Post ReplyReply Direct Link To This Post 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



Edited by dfrancis - 26 October 2005 at 4:36pm
Back to Top
pjb007 View Drop Down
Groupie
Groupie


Joined: 03 September 2004
Location: United Kingdom
Status: Offline
Points: 185
Post Options Post Options   Thanks (0) Thanks(0)   Quote pjb007 Quote  Post ReplyReply Direct Link To This Post Posted: 26 October 2005 at 5:13pm
Thanks I went for the first one, excellent. Smile
Back to Top
Gullanian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gullanian Quote  Post ReplyReply Direct Link To This Post Posted: 26 October 2005 at 8:04pm
Second way is probably the better, select cases are more efficient.
Back to Top
theSCIENTIST View Drop Down
Senior Member
Senior Member


Joined: 31 July 2003
Location: United Kingdom
Status: Offline
Points: 440
Post Options Post Options   Thanks (0) Thanks(0)   Quote theSCIENTIST Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
MadDog View Drop Down
Mod Builder Group
Mod Builder Group
Avatar

Joined: 01 January 2002
Status: Offline
Points: 3008
Post Options Post Options   Thanks (0) Thanks(0)   Quote MadDog Quote  Post ReplyReply Direct Link To This Post Posted: 16 November 2005 at 12:03am
qState = Trim(Request.QueryString("state"))


should be (to prevent sql injection):

qState = Replace(Request.QueryString("state"), "'", "''")
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.