views:

53

answers:

1

In my recent question, I asked for paging methods in SQL Server. Reading an article someone posted there, I came up with the following code. Everything seems correct to me but I receive the following error:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP or FOR XML is also specified.

 PAGEID = Request.QueryString("PAGEID")

 If PAGEID = "" or PAGEID = 0 Then
  PAGEID = 1
 End If

 RecordsPerPage = 1
 RecordsPageSize = PAGEID * RecordsPerPage

 SQL = "SELECT * FROM ( SELECT I.IMAGESID, I.IMAGESNAME, I.IMAGESSMURL, ROW_NUMBER() OVER (ORDER BY I.IMAGESID) As Row"
 SQL = SQL & " FROM IMAGES I"
 SQL = SQL & " WHERE Row > ("& RecordsPageSize - RecordsPerPage &") AND Row <= ("& RecordsPageSize &") ORDER BY I.IMAGESID DESC"
 Set objImages = objConn.Execute(SQL)

%>
                <div class="row">
                    <label for="Images">Images</label>
<% Do While Not objImages.EOF %>
     <img src="<%=objImages("IMAGESSMURL")%>" alt="<%=objImages("IMAGESNAME")%>" border="0" />
<%
 objImages.MoveNext
 Loop
%>
+1  A: 

Try adding another ") A" before the where and change the I.imagesid for A.imagesid:

  SQL = SQL & " FROM IMAGES I ) A"
  SQL = SQL & " WHERE Row > ("& RecordsPageSize - RecordsPerPage &") AND Row <= ("& RecordsPageSize &") ORDER BY A.IMAGESID DESC"

The error message is telling you that you cannot use an order by in a subquery.

Jose Chama
Now, I get "[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near the keyword 'ORDER'."
zurna
Well I was trying to simplify the example. But I see your point, I mistakenly forgot to remove the subquery. However, although the latest codes you provided seem correct to me, I get a different error now. "[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'Row'."
zurna
I corrected the first piece of code. I missed the name of the subquery so I added ) A and A.IMAGESID instead of I.IMAGESID in the order by.
Jose Chama
I get the same error, "[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'Row'."
zurna
Sorry, I corrected my answer. The ") A" should go before the where.
Jose Chama
It works now. What is weird is, in the example, it uses AS between ) and A. So its like; FROM IMAGES I ) AS table_name. But it doesnt work. Anyway, thank you very much!
zurna
Glad I could help! You are very welcome!
Jose Chama
Ah, no error but I get no values now! From; <img src="<%=objImages("IMAGESSMURL")%>" alt="<%=objImages("IMAGESNAME")%>" border="0" /> I get; "<img src="/FLPM/media/news/images/_sm.jpg" alt="" border="0" />".
zurna
Are you sure that the query is not returning any values?
Jose Chama
Only IMAGESID...
zurna
Are you sure that your `PAGEID` has got a valid numeric value ? I would test for `IsNull` also. Also, during development, I tend to write values vital for queries to screen before I run the query (that way you are at least making sure that you are using the correct values).
Edelcom