SQL中的布尔表达式选择列表 [英] Boolean Expressions in SQL Select list
问题描述
选择'测试名',foo = 'result'
从bar
其中baz =(某些条件)
这个想法是,如果foo列的值为结果,那么我会得到一个值true / 1;如果不是,我会得到false / 0。
不幸的是,T-SQL不喜欢表达式;它等于等号。
有没有办法评估SQL选择列表中的表达式并获得可返回的结果? (或其他方式实现我想要的单元测试?)
编辑:3好,答案,全部内置围绕CASE。我会接受feihtthief的,因为他得到最少的代表,因此需要它最多。 : - )感谢大家。
使用案例结构:
选择'测试名称',
case当foo ='结果'然后1 else 0结束
从bar其中baz =(某些标准)
另请参阅 MSDN Transact-SQL CASE文档。
I want to create a SQL Select to do a unit test in MS SQL Server 2005. The basic idea is this:
select 'Test Name', foo = 'Result' from bar where baz = (some criteria)
The idea being that, if the value of the "foo" column is "Result", then I'd get a value of true/1; if it isn't, I'd get false/0.
Unfortunately, T-SQL doesn't like the expression; it chokes on the equals sign.
Is there some way of evaluating an expression in the SQL select list and getting a returnable result? (Or some other way of achieving the unit testing that I want?)
EDIT: 3 great, answers, all built around CASE. I'll accept feihtthief's as he's got the least rep and thus needs it the most. :-) Thanks to everyone.
Use the case construct:
select 'Test Name',
case when foo = 'Result' then 1 else 0 end
from bar where baz = (some criteria)
Also see the MSDN Transact-SQL CASE documentation.
这篇关于SQL中的布尔表达式选择列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!