Hi there,
Well, this is pretty much as optimised as it can get. Few things though:
1. You don't need to actually select the data to see if it's there. So this:
Code:
"SELECT 1 FROM asumm_spelling WHERE " & _
"spelling='" & question2 & "' " & _
"or cspelling = '" & question2 & "'"
would e slightly faster because it doesn't retrieve the actual data. If the database is on a different server, you may save yourself from some network overhead. By selecting just the number 1 (or something else) you transfer only that, and not the value of spelling and cspelling.
2. Alternatively, you can use COUNT:
Code:
"SELECT COUNT(*) FROM asumm_spelling WHERE " & _
"spelling='" & question2 & "' " & _
"or cspelling = '" & question2 & "'"
Count returns the number of records so you could do this:
Code:
Set rs = conn.Execute(rs)
Code:
id41 = CInt(rs(0))
rs.close
Depending on how many rows your query returns, id41 could be 0, 1 or more.
3. IMO, it is better to have a separate variable for the SQL statement. ASP is quite forgiving when it comes to type swapping, but it looks a bit weird to store the SQL statement (a string) in rs which becomes a recordset after the Execute method. This would be a little cleaner:
Code:
Dim sql
sql = "SELECT spelling,cspelling FROM asumm_spelling WHERE " & _
"spelling='" & question2 & "' " & _
"or cspelling = '" & question2 & "'"
Set rs = conn.Execute(sql)
If Not rs.EOF Then id41 = "1" else id41 = "0"
rs.close
Same effect, but easier to understand for others.
4. Depending on the source of question2, your code may be open to
SQL injection.
Hope this helps,
Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of
ASP.NET 2.0 Instant Results and
Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.