I’m doing some updates to an old Classic ASP website. There is a table which contains several columns of text data and a datetime field. I need to get a list of unique years from all of the values in the table. I’ve tried this:
set objConnection = Server.CreateObject("ADODB.Connection")
objConnection.ConnectionString = "Provider=SQLNCLI10;Server=localhost;Database=mydb;Uid=myuser;Pwd=something;"
objConnection.Open
set objRst = objConnection.execute("SELECT DISTINCT(YEAR(report_date)) AS report_year FROM report;")
if not objRst.eof then
do while not objRst.eof
response.write objRst("report_year")
objRst.movenext
loop
end if
But when I run this script in the page it just does nothing – eventually the script times-out.
Can anyone suggest how to accomplish this? Thanks!
This is going to be an index problem or a locking problem (or both).
First, try to select the data using the NOLOCK hint – this means your select query won’t wait for any uncommitted transactions:
If that still hangs it would suggest an index issue, to sort that out you’ll need to run the query in SSMS (if you can) and see how long it takes there. If it takes ages in SSMS it would suggest you need to create an index on the report_date column – if it’s quick in SSMS then it’s something to do with your ASP, i.e. is the connection open? have you tried doing a simple query to make sure that works? i.e.
EDIT: just noticed your comment regarding the fact that there are only 5 rows in the table – so it’s probably not an indexing issue! However the
NOLOCKhint andGROUP BY(as opposed toDISTINCT) might help.Would be interesting to see an execution plan of this too (i.e. make sure there are no triggers slowing things down)