SQL 存储非零字符的位域
SQL Bit field storing Non-zero character
我知道位字段只能存储 0 或 1。我有一个继承的 InfoPath 表单,当我从表单中解析 XML 并将数据存储在数据库中时,其中之一XML 个节点有一个 1 或 2,它被存储在一个位域中。不管字段中有1还是2,位域只存储1。我的问题是,位域将0存储为0,但它是否也将非零字符存储为1?那么,2 是否也存储为 1?
是的,非 0
值被插入为 1
。
来自MSDN:
The SQL Server Database Engine optimizes storage of bit columns. If there are 8 or less bit columns in a table, the columns are stored as 1 byte. If there are from 9 up to 16 bit columns, the columns are stored as 2 bytes, and so on.
The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.
Converting to bit promotes any nonzero value to 1.
测试SQL:
Create Table Test (A bit);
Insert Test Values (-1), (0), (1), (2)
Select * From Test
结果:
A
----
1
0
1
1
我知道位字段只能存储 0 或 1。我有一个继承的 InfoPath 表单,当我从表单中解析 XML 并将数据存储在数据库中时,其中之一XML 个节点有一个 1 或 2,它被存储在一个位域中。不管字段中有1还是2,位域只存储1。我的问题是,位域将0存储为0,但它是否也将非零字符存储为1?那么,2 是否也存储为 1?
是的,非 0
值被插入为 1
。
来自MSDN:
The SQL Server Database Engine optimizes storage of bit columns. If there are 8 or less bit columns in a table, the columns are stored as 1 byte. If there are from 9 up to 16 bit columns, the columns are stored as 2 bytes, and so on.
The string values TRUE and FALSE can be converted to bit values: TRUE is converted to 1 and FALSE is converted to 0.
Converting to bit promotes any nonzero value to 1.
测试SQL:
Create Table Test (A bit);
Insert Test Values (-1), (0), (1), (2)
Select * From Test
结果:
A
----
1
0
1
1