AS附近的语法,但我无法识别问题

问题描述 投票:0回答:2

我一直收到以下错误:

Msg 156,第15级,状态1,第4行关键字“ as”附近的语法不正确。

Msg 156,第15级,状态1,第10行关键字'between'附近的语法不正确。

我看不到问题。

use leads;

select *
    (select max(HighTrw) 
     from (values (trw1), (trw2), (trw21)) as Value (HighTrw)) as [high_trw]
from 
    dbo.spi s
Where 
    (select max(HighTrw)
     from (values (trw1), (trw2), (trw21)) As updatedate (HighTrw)) between '600' and '625'
sql sql-server syntax
2个回答
0
投票

您的代码看起来像SQL Server代码,所以我将使用该语法。

大概,您打算做这样的事情:

select s.*, ss.high_trw
from dbo.spi s outer apply
     (select max(Value.HighTrw) as high_trw
      from (values (s.trw1), (s.trw2), (s.trw21)
           ) as Value(HighTrw)
     ) ss
where ss.high_trw between 600 and 625;

注意:

  • 您可以使用from将子查询放入apply子句。
  • 一个明显的问题是在相关子查询之前缺少逗号。
  • 您应该学习限定all列名。这对于相关的子查询尤为重要。
  • [600625]看起来像数字,而不是字符串。如果是这样,则常量不应使用引号。

0
投票

除了不提供某些数据外,还有一些我关心的代码问题:

use leads;

-- There's no comma "," after the "*"
select*
-- This look like it belongs in the "from" section as a subquery, eg "(select ...) as sub"
-- You can then reference all of its contents in the main select using "sub.*"
(select max(HighTrw) from (values (trw1), (trw2), (trw21)) as Value(HighTrw)) as [high_trw]


from dbo.spi s

Where (select max(HighTrw)
-- Haven't seen this way of specifying a table before, is it valid? 
-- Why not explicitly define a temporary table above with the data you want, _before_ starting the query? - makes the code cleaner/clearer, IMO
            from (values (trw1), (trw2), (trw21)) As updatedate (HighTrw)) between '600' and '625'
-- There's no final ";" to terminate the query, might not be absolutely necessary, but good habit, 
-- because at some point, this code's going to dovetail into some other code, and the compiler will probably throw a wobbly

© www.soinside.com 2019 - 2024. All rights reserved.