简体   繁体   English

关键字'with'附近的语法不正确(SQL)

[英]Incorrect syntax near the keyword 'with' (SQL)

I have a little bit of an odd issue. 我有一个奇怪的问题。

When I run this SQL: 当我运行这个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). 在SQL Express中,它运行并返回第一行(类似于MySQL中的Limit 0,1)。 However, when I run this in delphi via the TADOQuery object, I receive the error in the title. 但是,当我通过TADOQuery对象在delphi中运行它时,我收到标题中的错误。 I am guessing the ADO object is doing something along the way but I don't understand what. 我猜这个ADO对象正在做一些事情,但我不明白是什么。

Note: This is the exact SQL I am passing into the ADO object. 注意:这是我传递给ADO对象的确切SQL。

Does anyone have any ideas? 有没有人有任何想法?

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). 当批处理中有其他语句时,WITH关键字需要以分号开头(技术上讲,前面的语句必须以“;”结尾,但只是将它放在WITH之前更容易维护) 。

My guess is that ADO sets a connection variable or something similar, so the WITH is no longer first in the batch. 我的猜测是ADO设置了一个连接变量或类似的东西,因此WITH不再是批处理中的第一个。 Change it to ";WITH" and see if that works. 将其更改为“; WITH”,看看是否有效。

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;

Which OLE DB provider are you specifying in your connection string? 您在连接字符串中指定了哪个OLE DB提供程序? To be able to use the WITH (CTE) syntax you need to use the SQL Native Client provider eg 为了能够使用WITH (CTE)语法,您需要使用SQL Native Client提供程序,例如

Provider=SQLNCLI10.1

rather than say the SQL Server OLE DB Provider eg 而不是说SQL Server OLE DB提供程序,例如

Provider=SQLOLEDB.1

Try this: 尝试这个:

declare @a int
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;

//------------------------------
//with can not be the first row

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM