views:

128

answers:

3

Hi Guys,

I have a little bit of an odd issue.

When I run this SQL:

with ID_Table as ( 
    select row_number() over (order By SS_ID) As row_id, 
             ss_id 
      from slide_show ) 
     select t0.* 
       from ID_Table as t1 
inner Join slide_show as t0 on t1.ss_id = t0.ss_id 
    where t1.row_id between 0 and 1 
 order by t1.row_id asc;

in SQL Express, it runs and returns the first row as it should (similar to the Limit 0,1 in MySQL). However, when I run this in delphi via the TADOQuery object, I receive the error in the title. I am guessing the ADO object is doing something along the way but I don't understand what.

Note: This is the exact SQL I am passing into the ADO object.

Does anyone have any ideas?

+2  A: 

Try:

SELECT t0.*
FROM (SELECT row_number() over(ORDER BY SS_ID) AS row_id, ss_id FROM slide_show) AS t1
INNER JOIN slide_show AS t0
ON t1.ss_id = t0.ss_id
WHERE t1.row_id BETWEEN 0 AND 1
ORDER BY t1.row_id ASC;
Michael Pakhantsov
This is my thought too - the Delphi/ADO doesn't support WITH, which is just syntactic sugar for a derived table/inline view anways...
OMG Ponies
It occured to me after posting that this is in fact 2 statements trying to run. I wasn't aware you can subselect for a table though so this is just what I needed. Thanks very much!
webnoob
A: 

The WITH keyword needs to be preceded by a semicolon when there are other statements before it in a batch (technically speaking, the preceding statement has to be terminated with ";", but just putting it before the WITH is a bit easier to maintain).

My guess is that ADO sets a connection variable or something similar, so the WITH is no longer first in the batch. Change it to ";WITH" and see if that works.

Tom H.
I've never had to prefix `WITH` with a semicolon - SQL Server or Oracle.
OMG Ponies
I just tested it to be sure I wasn't remembering incorrectly and received as error without the semicolon. For example, "SELECT 1 <newline> WITH My_CTE AS (SELECT 1 AS one)" fails, but I added the semicolon and it worked. This was in SQL 2008.
Tom H.
Implement best practise by ending all SQL statements with a semicolon and there's no issue.
onedaywhen
It still becomes an issue if your code is being attached to other code over which you have limited or no control.
Tom H.
+1  A: 

Which OLE DB provider are you specifying in your connection string? To be able to use the WITH (CTE) syntax you need to use the SQL Native Client provider e.g.

Provider=SQLNCLI10.1

rather than say the SQL Server OLE DB Provider e.g.

Provider=SQLOLEDB.1

onedaywhen
Thats useful info, for situations where subselects won't work, this would be good.
webnoob