SQL Server T-SQL 中的整数最大值常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2740662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
integer Max value constants in SQL Server T-SQL?
提问by AaronLS
Are there any constants in T-SQL like there are in some other languages that provide the max and min values ranges of data types such as int?
T-SQL 中是否有任何常量,就像其他一些语言中提供的数据类型(如 int)的最大值和最小值范围一样?
I have a code table where each row has an upper and lower range column, and I need an entry that represents a range where the upper range is the maximum value an int can hold(sort of like a hackish infinity). I would prefer not to hard code it and instead use something like SET UpperRange = int.Max
我有一个代码表,其中每一行都有一个上下范围列,我需要一个条目来表示一个范围,其中上限是 int 可以容纳的最大值(有点像黑客无穷大)。我宁愿不对其进行硬编码,而是使用类似的东西SET UpperRange = int.Max
采纳答案by OMG Ponies
There are two options:
有两种选择:
- user-defined scalar function
- properties table
- 用户定义的标量函数
- 属性表
In Oracle, you can do it within Packages - the closest SQL Server has is Assemblies...
在 Oracle 中,您可以在 Packages 中执行此操作 - 最接近的 SQL Server 是 Assemblies...
回答by vfilby
I don't think there are any defined constants but you could define them yourself by storing the values in a table or by using a scalar valued function.
我认为没有任何定义的常量,但您可以通过将值存储在表中或使用标量值函数来自己定义它们。
Table
桌子
Setup a table that has three columns: TypeName, Max and Min. That way you only have to populate them once.
设置一个包含三列的表:TypeName、Max 和 Min。这样你只需要填充它们一次。
Scalar Valued Function
标量值函数
Alternatively you could use scalar valued functions GetMaxInt() for example (see this StackOverflow answerfor a real example.
或者,您可以使用标量值函数 GetMaxInt() 例如(有关真实示例,请参阅此StackOverflow 答案。
You can find all the max/min values here: http://msdn.microsoft.com/en-us/library/ms187752.aspx
您可以在此处找到所有最大值/最小值:http: //msdn.microsoft.com/en-us/library/ms187752.aspx
回答by MikeTeeVee
Avoid Scalar-Functions like the plague:
Scalar UDF Performance Problem
That being said, I wouldn't use the 3-Column table another person suggested.
This would cause implicit conversions just about everywhere you'd use it.
You'd also have to join to the table multiple times if you needed to use it for more than one type.
Instead have a column for each Min and Max of each Data Type (defined using it's own data type) and call those directly to compare to.
Example:
像瘟疫一样避免标量函数:
标量 UDF 性能问题 话
虽如此,我不会使用另一个人建议的 3 列表。
这几乎会在您使用它的任何地方导致隐式转换。
如果您需要将它用于多种类型,您还必须多次加入该表。
而是为每个数据类型的每个最小值和最大值(使用它自己的数据类型定义)有一列,并直接调用它们进行比较。
例子:
SELECT *
FROM SomeTable as ST
CROSS JOIN TypeRange as TR
WHERE ST.MyNumber BETWEEN TR.IntMin AND TR.IntMax