在 SELECT 语句中设置变量
不幸的是,我遇到了与此处所述相同的问题该解决方案不适用于 MS SQL Server。 MS SQL Server 是否存在类似的语法?
注意:我的查询并不像示例中那么简单。我想在 TooLateTime 的情况下重用 DifferenceMinutes 。
DATEDIFF(MINUTE, DayOfWeekStopTime, GETDATE()) AS DifferenceMinutes,
CASE
WHEN DATEDIFF(MINUTE, DayOfWeekStopTime, GETDATE()) < 0 THEN NULL
ELSE CONVERT(varchar, GETDATE() - DayOfWeekStopTime, 108)
END AS TooLateTime
I'm having the same problem as described here, unfortunately the solution does not work for MS SQL Server.
Does a similar syntax exist for MS SQL Server?
Note: my query is not as simple as in the example. I'd like to reuse DifferenceMinutes in the TooLateTime case.
DATEDIFF(MINUTE, DayOfWeekStopTime, GETDATE()) AS DifferenceMinutes,
CASE
WHEN DATEDIFF(MINUTE, DayOfWeekStopTime, GETDATE()) < 0 THEN NULL
ELSE CONVERT(varchar, GETDATE() - DayOfWeekStopTime, 108)
END AS TooLateTime
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
很难准确说出您想要做什么,但我认为这可能就是您正在寻找的:
您必须将源表替换为 FROM 部分中的“TableName”内部查询。
通过将计算值滚动到像这样的嵌套选择中,您可以通过在外部查询中给它们指定的任何名称来引用它们。
如果要为每个值设置变量,可以按如下方式执行此操作,但需要确保只从查询中返回一行:
It's a little hard to tell exactly what you're trying to do, but I think this might be what you're looking for:
You'll have to substitute your source table(s) for "TableName" in the FROM section of the inner query.
By rolling your calculated values into a nested select like this, you can refer to them by whatever name you give them in the outer query.
If you want to set variables for each of the values, you can do that as follows, but you'll need to make sure you're only returning one row from the query: