开发者

Boolean expression as column value in transact sql

开发者 https://www.devze.com 2023-02-14 19:57 出处:网络
In most RDBMS:es, this work: select (5 > 3) and evaluates to true. It doesn\'t work in MS Transact SQL and the only workaround I\'ve found is to write:

In most RDBMS:es, this work:

select (5 > 3)

and evaluates to true. It doesn't work in MS Transact SQL and the only workaround I've found is to write:

select case when 5 > 3 then 1 else 0 end

Which kind of sucks because it is much more verbose. Is there a better way to write the above 开发者_Python百科kind of checks?


If the problem is arithmetic comparison:

select (5 - 3)

Then at the application level test for < or = or > 0.


You could write it as a scalar-valued function, but it will be very slow on large datasets.


If your program often requires such case constructs you could create your set of functions that will have user functions like Bool_IsGreater(left, right) that will return you your desired 0 or 1.

SQL Server doesn't support boolean value type anyway even for basic column use.

If you will need performance and those 5 and 3 values come naturally from some select query you might want to create a new column and set its value to 1 or 0 by trigger or something, which could help with performance.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号