SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Cannot retrieve data from the database


Cannot retrieve data from the database

Author
Message
rich_a_wms
rich_a_wms
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 5
Here is the code I am using to retrieve data that I have seen on the database:
<!-- #INCLUDE FILE="sqlconnect.asp" -->
<%
Dim sqlString, cityState, storeTime, RStimes
cityState = "Mountain View, CA"
storeTime = 9
sqlString = "SELECT city_time FROM citytime WHERE city_state='" & cityState & "'"
SET RStimes = Con.Execute( sqlString )
IF NOT RStimes.EOF THEN
storeTime = RStimes("city_time")
END IF

Response.Write sqlString & vbCrLf
Response.Write "storeTime=" & storeTime & vbCrLf
Response.End
%>

Here are the results of the write statements:
SELECT city_time FROM citytime WHERE city_state='Mountain View, CA'
storeTime=9
I know that city_state contains Mountain View, CA and that city_time contains 0. Why am I getting city_time 9 ? I have reviewed the SQL query many times and I'm not seeing any errors.
matt.bowler
matt.bowler
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1633 Visits: 615
Is there more than one result that satisfies your where clause?
matt.bowler
matt.bowler
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1633 Visits: 615
Can you explain what the intention of this piece of code is?

IF NOT RStimes.EOF THEN
storeTime = RStimes("city_time")
END IF

It would seem that the predicate is failing here and your store_time is never changing from the 9 you have set it to at the beginning.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)SSC Guru (210K reputation)

Group: General Forum Members
Points: 210795 Visits: 41973
What do you get for the following???

 SELECT *
FROM citytime
WHERE city_state='Mountain View, CA'
;



If the answer is "more than 1 row", then you've probably identified the problem. Look at the data and see if your assumptios about what the citytime should be are actually correct.

As a sidebar, the code you posted is VERY suseptible to SQL INJECTION and should be changed to parameterized embedded SQL instead.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
rich_a_wms
rich_a_wms
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 5
There is only one result in the table: Mountain View, CA
rich_a_wms
rich_a_wms
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 5
This code sets the storeTime from the table only if the city_state = Mountain View, CA
Otherwise, the storeTime is set to 9 if the cityState value is not found in the table. This is just an example of the code I am using that shows the problem.
rich_a_wms
rich_a_wms
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 5
I have used the literal 'Mountain View, CA' and still receive a storeTime = 9.
There is only one instance of it. Can you give me an example of the second line of your response?
rich_a_wms
rich_a_wms
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
Points: 15 Visits: 5
I found an extra space after the , and before the state in the city_state column. Problem solved and all is well. Thanks Jeff for taking the time and for your comments.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)SSC Guru (61K reputation)

Group: General Forum Members
Points: 61271 Visits: 17954
rich_a_wms (11/27/2012)
I found an extra space after the , and before the state in the city_state column. Problem solved and all is well. Thanks Jeff for taking the time and for your comments.


Two things.

1) The solution to your problem is a good example of why you should not store city and state in the same field.

2) Your classic asp appears to be the beginning of a page that will use user input for city_state? The way you have this coded is wide open to sql injection attack. You should NEVER NEVER NEVER execute inputs from the user, especially on a website!!! You need to parameterize your queries, or even better, use stored procs to retrieve your data.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search