如何在 T-SQL 中的位字段上创建三元条件
我有一个 SQLExpress 表,其中包含一个用于存储 TRUE/FALSE 状态的位字段。
类似于:
+----+---------+
| ID | IsAlive |
+----+---------+
| 1 | 1 |
| 2 | 0 |
| 3 | NULL |
| 4 | 1 |
+----+---------+
使用该表作为示例,我想创建一个存储过程来执行以下任一操作:
- 检索所有记录。
- 仅检索带有
IsAlive=1
的记录。 - 仅检索
IsAlive=0 或 NULL
的记录。
我正在尝试思考如何创建查询而不必编写 IF/ELSE 条件 - 在我看来,有一种比这样做更好/更干净的方法:
-- The ternary logic...
-- 0 or NULL retrieves records where IsAlive = 0 or NULL
-- 1 retrieves records where IsAlive = 1
-- Otherwise return all records
-- sproc .....
@IsAlive tinyint = 2 -- Return all records by default
AS
BEGIN
IF(@SentToNTService = 0 OR @SentToNTService = 1)
BEGIN
SELECT *
FROM MyTable
WHERE IsAlive = @IsAlive;
END
ELSE -- Lame redundancy
BEGIN
SELECT *
FROM MyTable
END
END
是否有另一种方法可以创建相同的结果而无需编写像我上面那样创建两个不同的查询?
I have a SQLExpress table that includes a bit field for storing TRUE/FALSE state.
Something like:
+----+---------+
| ID | IsAlive |
+----+---------+
| 1 | 1 |
| 2 | 0 |
| 3 | NULL |
| 4 | 1 |
+----+---------+
Using that table as our example, I want to create one Stored Procedure that will do any one of the following:
- Retrieve all records.
- Retrieve only the records with
IsAlive=1
. - Retrieve only the records with
IsAlive=0 or NULL
.
I am trying to think of how I can create my query without having to write IF/ELSE conditions - It seems to me there is a better/cleaner way than to do something like this:
-- The ternary logic...
-- 0 or NULL retrieves records where IsAlive = 0 or NULL
-- 1 retrieves records where IsAlive = 1
-- Otherwise return all records
-- sproc .....
@IsAlive tinyint = 2 -- Return all records by default
AS
BEGIN
IF(@SentToNTService = 0 OR @SentToNTService = 1)
BEGIN
SELECT *
FROM MyTable
WHERE IsAlive = @IsAlive;
END
ELSE -- Lame redundancy
BEGIN
SELECT *
FROM MyTable
END
END
Is there another way of creating the same results without having to create two different queries as I did above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
关于如何执行此操作的 2 个建议:
假设您的变量 @isalive 也被声明为“位”(应该是)
如果您想使用不需要 @isalive 为“位”的“位比较”解决方案(它适用于 bit 和tinyint)
第二个解决方案适用于像我这样的书呆子。一些硬核人士可能会觉得它很有趣(或者至少很有趣),因为我认为它提供了最佳的性能(如果我错了,请有人纠正我)。这是一个强大的解决方案,但难以阅读。
2 suggestions of how to do this:
Assuming your variable @isalive is declared as 'bit' as well (which is should be)
If you want to use a 'bit compare' solution that doesn't require @isalive to be 'bit' (it will work for bit as well as tinyint)
Second solution is for nerds like me. Some hardcore people may find it interesting (or at least amusing) as I think it offer the best possible performance (please, someone correct me if i am wrong). It is a powerful solution but hard to read.
这将执行您想要的操作:
基于 @IsAlive 的值:
COALESCE 是一个返回其第一个参数的函数,除非它为 NULL,在这种情况下它返回其第二个参数。
因此,如果 IsAlive 为 NULL,则 LHS 返回 0;如果 IsAlive 为 1,则 LHS 返回 0;如果 IsAlive 为 1,则 LHS 返回 1。
当存储过程参数 @IsAlive 为 NULL 时,RHS 返回相同的结果,否则仅返回 @IsAlive 参数。
编辑:
这假设 @IsAlive 是 BIT。对于tinyint,你可以添加一个case语句:
This will do what you want:
Based on the value of @IsAlive:
COALESCE is a function that returns it's first argument, unless it's NULL, in which case it returns its second argument.
So the LHS returns 0 if IsAlive is NULL or 0 and 1 if IsAlive is 1.
The RHS returns the same when the stored procedure argument @IsAlive is NULL and just returns the @IsAlive argument otherwise.
EDIT:
This assumed that @IsAlive is BIT. In the case of tinyint you can add a case statement:
试试这个:
try this:
这并不准确,但非常接近您可以做的事情:
This isnt exact, but pretty close to what you can do:
像这样的东西也应该有效。
Something like this should also work.