SELECT COL1+COL2 as CalcColumn,* FROM TABLE WITH (NOLOCK) WHERE 100
发布于 2024-12-10 05:07:57 字数 100 浏览 0 评论 0 原文

在使用基于计算值的条件的 SELECT 语句中,是否可以包含该计算值而无需计算计算值两次 - 一次在选择中,另一次在条件中?

我正在使用 SQL Server 2000。

In a SELECT statement using a condition based on a calculated value, is it possible to include that calculated value without computing the calculated value twice - once in the selection and again in the condition?

I am using SQL Server 2000.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江心雾 2024-12-17 05:07:57

您可以将所有内容放入子查询中并从该

SQL 语句

SELECT *
FROM   (
         SELECT COL1 + COL2 as CalcColumn
         FROM   Table
       ) q
WHERE  100 < CalcColumn 

中进行选择,但就性能而言,我预计这会比原始查询

You could put everything into a subquery and select from that

SQL Statement

SELECT *
FROM   (
         SELECT COL1 + COL2 as CalcColumn
         FROM   Table
       ) q
WHERE  100 < CalcColumn 

but as to performance, I expect this to be slower than your original query.

一笑百媚生 2024-12-17 05:07:57

如果您按编写的方式使用查询,则不应有任何显着的性能损失。我相信 SQL 会为你处理它。我的问题是,你为什么使用上个世纪的软件?

我刚刚

SELECT Debit, Credit, Debit+Credit AS CalcColumn FROM JDT1 WHERE CalcColumn > 100

按照几个人的建议尝试了 SQL 2005,错误是:
消息 207,第 16 级,状态 1,第 1 行
列名“CalcColumn”无效。

There should not be any significant performance loss if you use the query as you wrote it. SQL handles it for you, I believe. My question would be, why are you using software from the previous century?

I just tried

SELECT Debit, Credit, Debit+Credit AS CalcColumn FROM JDT1 WHERE CalcColumn > 100

on SQL 2005 as suggested by a couple of guys and the error is:
Msg 207, Level 16, State 1, Line 1
Invalid column name 'CalcColumn'.

遮了一弯 2024-12-17 05:07:57
SELECT COL1+COL2 as CalcColumn,* 
FROM TABLE WITH (NOLOCK) 
WHERE 100 < CalcColumn

希望有帮助。

SELECT COL1+COL2 as CalcColumn,* 
FROM TABLE WITH (NOLOCK) 
WHERE 100 < CalcColumn

hope that helps.

始终不够 2024-12-17 05:07:57

我通常创建一个视图来也具有可重用的计算列。

CREATE VIEW TableView
AS
SELECT COL1+COL2 as CalcColumn,*  FROM TABLE WITH (NOLOCK)  

GO

SELECT * FROM TableView WHERE 100 < CalcColumn

I usually create a view to also have reusable calculation columns.

CREATE VIEW TableView
AS
SELECT COL1+COL2 as CalcColumn,*  FROM TABLE WITH (NOLOCK)  

GO

SELECT * FROM TableView WHERE 100 < CalcColumn
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文