简体   繁体   English

来自@Parameter的价值

[英]Value from @Parameter

Im trying to do this 我试着这样做

SELECT     CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
FROM         batch
WHERE     (NewDT <= @BatchStartDate)

But i get the error "Invalid colum name NewDT" The problem is that date and time is in two colums in the database. 但我得到错误“无效的列名称NewDT”问题是日期和时间在数据库中的两个列中。

Guessing you are using MS SQL Server. 猜猜你正在使用MS SQL Server。 You can't reference an alias on the WHERE clause, you should use the full CAST(date AS datetime) + CAST(time AS datetime) so it would be: 您不能在WHERE子句上引用别名,您应该使用完整的CAST(date AS datetime) + CAST(time AS datetime)因此它将是:

SELECT CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
  FROM batch
 WHERE (CAST(date AS datetime) + CAST(time AS datetime) <= @BatchStartDate)

(Also guessing that you use SQL Server) (还猜测你使用的是SQL Server)

You cannot use an alias in the where clause, so you can use the whole expression (as in Yaroslav's answer) or you can wrap the original select as a subquery, like this: 你不能在where子句中使用别名,所以你可以使用整个表达式(如在Yaroslav的答案中),或者你可以将原始select包装为子查询,如下所示:

select * from 
(
   SELECT CAST(date AS datetime) + CAST(time AS datetime) AS NewDT, variable, value
   FROM batch
)
WHERE NewDT <= @BatchStartDate

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

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