将INT转换为BIT [英] Convert INT to BIT
问题描述
我在下面尝试查询
DECLARE @Input INT = 300
DECLARE @Ouput TINYINT
SET @Ouput = @Input
SELECT @Ouput
执行上述声明时,我收到以下错误。
While execute the above statement, I received the following error.
数据类型tinyint的算术溢出错误,值= 300。
输入值超出限制,因此显示错误。
The input value exceeds the limit, so the error is displayed.
我尝试了另一个查询
DECLARE @Input INT = 300
DECLARE @Ouput BIT
SET @Ouput = @Input
SELECT @Ouput
当我执行声明,我真的很想知道,它没有显示任何错误。如果输入值<> 0
(负数或正数),输出值始终 1
。
When I execute the statement, I really wondered, it doesn't show any error. If the input value <> 0
(negative or positive), the output value always 1
.
推荐答案
Sqlserver有权或者说尝试将值隐式转换为输入和输出不使用强制转换或转换函数的类型,如果你没有指定任何。
Sqlserver have power or say try to convert value implicitly as your input and output type without using cast or convert function, if you not specify any.
当转换错误出现在默认逻辑或scenerio中时,它会给出特定的类型转换错误。
When the conversion error comes in default logic or scenerio, it give the specific type cast error.
所以你的小int和int长度不同,300不适合微小的int。 Tiny int允许最大值为255值
这篇关于将INT转换为BIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!