简体   繁体   English

整数SQL Server T-SQL中的最大值常量?

[英]integer Max value constants in SQL Server T-SQL?

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). 我有一个代码表,其中每一行都有一个上下范围列,我需要一个表示一个范围的条目,其中上限是int可以容纳的最大值(有点像一个hackish无穷大)。 I would prefer not to hard code it and instead use something like SET UpperRange = int.Max 我宁愿不对它进行硬编码,而是使用类似SET UpperRange = int.Max

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 ...

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. 设置一个包含三列的表:TypeName,Max和Min。 That way you only have to populate them once. 这样你只需要填充一次。

Scalar Valued Function 标量值函数

Alternatively you could use scalar valued functions GetMaxInt() for example (see this StackOverflow answer for 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

Avoid Scalar-Functions like the plague: 避免标量 - 像瘟疫一样的功能:
Scalar UDF Performance Problem 标量UDF性能问题

That being said, I wouldn't use the 3-Column table another person suggested. 话虽这么说,我不会使用另一个人建议的3柱表。
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. 相反,每个数据类型的每个Min和Max都有一列(使用它自己的数据类型定义)并直接调用它们进行比较。

Example: 例:

SELECT *
  FROM SomeTable as ST
  CROSS JOIN TypeRange as TR
  WHERE ST.MyNumber BETWEEN TR.IntMin  AND TR.IntMax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM