Published on InterSystems Developer Community (https://community.intersystems.com)

主页 > 第110章 SQL函数 POWER

文章
姚 鑫 · 四月 12, 2022 阅读大约需 2 分钟

第110章 SQL函数 POWER

第110章 SQL函数 POWER

一个数值函数,它返回给定表达式的指定幂的值。

大纲

POWER(numeric-expression,power)

{fn POWER(numeric-expression,power)}

参数

  • numeric-expression - 基数。可以是正整数或负整数或小数。
  • power - 指数,它是数值表达式的幂。可以是正整数或负整数或小数。

POWER 返回 NUMERIC 或 DOUBLE 数据类型。如果 numeric-expression 是数据类型 DOUBLE,则 POWER 返回 DOUBLE;否则,它返回 NUMERIC。

描述

POWER 计算一个数字的另一个幂。它返回一个精度为 36、比例为 18 的值。

请注意,POWER 可以作为 ODBC 标量函数(使用大括号语法)或 SQL 通用标量函数来调用。

numeric-expression 和 power 的所有组合都是有效的,除了:
- POWER(0,-m):0 数字表达式和负幂会导致 SQLCODE -400 错误。
- POWER(-n,.m):负数值表达式和小数幂会导致 SQLCODE -400 错误。

示例

以下示例将 5 提高到 3 次方:

SELECT POWER(5,3) AS Cubed

125

以下嵌入式 SQL 示例返回 2 的前 16 次幂:

/// d ##class(PHA.TEST.SQLFunction).Power()
ClassMethod Power()
{
    s a = 1
    while a < 17 {
        &sql(
            SELECT {fn POWER(2, :a)}
                INTO :b
        )
    if SQLCODE '= 0 {
        w !,"Error code ",SQLCODE 
    } else {
        w !,"2 to the ",a," = ",b
        s a = a +1  }
    }
}

DHC-APP>d ##class(PHA.TEST.SQLFunction).Power()

2 to the 1 = 2
2 to the 2 = 4
2 to the 3 = 8
2 to the 4 = 16
2 to the 5 = 32
2 to the 6 = 64
2 to the 7 = 128
2 to the 8 = 256
2 to the 9 = 512
2 to the 10 = 1024
2 to the 11 = 2048
2 to the 12 = 4096
2 to the 13 = 8192
2 to the 14 = 16384
2 to the 15 = 32768
2 to the 16 = 65536
#SQL #Caché

源 URL:https://cn.community.intersystems.com/post/%E7%AC%AC110%E7%AB%A0-sql%E5%87%BD%E6%95%B0-power