sql server 4 字节无符号整数

2021-09-10 00:00:00 sql tsql sql-server-2008 sql-server

有谁知道我可以通过使用 4 个字节而不是使用 8 个字节和 bigint 来保存无符号整数(0 到 4294967295)的任何解决方法吗?

Does anyone know of any work around by which i can save unsigned integers (0 to 4294967295) simply using 4 bytes instead of using 8 bytes and bigint?

我知道我们可以创建用户定义的数据类型并对它们创建一个约束以不允许负值,但这仍然不允许我输入超过 2147483647 的值.我只想使用 4 个字节,但能够保存更大的整数值大于 2147483647 但小于 4294967295.

I know we can create user defined datatypes and create a constraint on them to not allow negative values but that still does not allow me to enter values over 2147483647. I only want to use 4 bytes but be able to save integer values greater than 2147483647 but less than 4294967295.

可能的重复:SQL Server 中的 4 字节无符号整数?

推荐答案

没有可用的无符号类型,因此您可以使用 UDT 创建一个,或者选择更大的数据类型.如果您在 UDT 中执行此操作,您将再次超过 4 个字节.

There is no unsigned type available to you, so you could create one using the UDT, or opt for the larger data type. If you do it in a UDT you are going to exceed the 4 bytes again.

最极端的做法是在您读取存储值后通过添加 -2^31 自动将偏移量应用于它,但这是一种真正的 hacky 方法,并且会使查看代码等的任何人感到困惑,而不是提及错误/遗漏的可能性.我根本不推荐 hack.

The extreme hack would be to apply an offset automatically to your stored value after you read it, by adding -2^31 but this is a real hacky way to go about it and confusing for anyone viewing the code etc, not to mention the potential for mistakes / things being missed. I wouldn't recommend the hack at all.

相关文章