T-SQL选择平等结果

Omt*_*ara 1 t-sql sql-server

在下面的代码片段中,我在第4行('='附近的语法错误)中收到错误.我需要在select语句中将相等结果显示为列.

declare @five int
set @five = 5

declare @bool bit
set @bool = (@five = 6)

select @five, @bool
Run Code Online (Sandbox Code Playgroud)

结果集应该有两列:5个false

usr*_*usr 5

T-SQL没有真正的布尔类型.这是一个奇怪的情况.这是解决方案:

set @bool = case when @five = 6 then 1 else 0 end
Run Code Online (Sandbox Code Playgroud)

真理表达式在其他语言中是布尔类型的,在T-SQL中没有类型.你只能在特殊的句法位置使用真值表达式,比如where,ifcase.