简体   繁体   English

如何解决此错误(关键字“SELECT”附近的语法不正确。)

[英]how solve this error( Incorrect syntax near the keyword 'SELECT'.)

My query is :我的查询是:

DECLARE @Date DATETIME2
DECLARE @Number INT
DECLARE @Number2 INT

SET @Number = 90
SET @Number2 = 89

SET @Num = @Number - @Number2

SET @Date = SELECT TOP (@Num) YEAR(Tarikh) FROM Final 

You need to either put your SELECT subquery into brackets like this:你需要要么把你的SELECT子查询到这样的括号:

DECLARE @Date DATETIME2   -- this should *really* be an `INT` ....   
SET @Date = (SELECT TOP (@Num) YEAR(Tarikh) FROM Final)  -- YEAR(..) returns *INT* !

or you need to rewrite this as a SELECT only statement - like this:或者您需要将其重写为仅SELECT语句 - 如下所示:

SELECT TOP (@Num) @Date = YEAR(Tarikh) 
FROM Final 
-- ORDER BY ......   -- you should *REALLY* include an ORDER BY for TOP to make any sense....

Of course, this only works if your subquery always returns only ONE possible value!当然,这只适用于您的子查询总是返回一个可能的值!

And also : using the TOP clause without an explicit ORDER BY clause is kinda dangerous - since you're not explicitly telling SQL Server which TOP row you want, you might get any arbitrary row(s) ...而且:使用没有显式ORDER BY子句的TOP子句有点危险 - 因为您没有明确告诉 SQL Server 您想要哪个TOP行,您可能会得到任何任意行...

And lastly : using YEAR(...) returns an INT value - which you're assigning to a DATETIME2 variable ...... that doesn't really make any sense at all.......最后:使用YEAR(...)返回一个INT值 - 您将其分配给DATETIME2变量......这根本没有任何意义......

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

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