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

主页 > 第130章 SQL函数 SQRT

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

第130章 SQL函数 SQRT

第130章 SQL函数 SQRT

返回给定数值表达式的平方根的数值函数。

大纲

SQRT(numeric-expression)

{fn SQRT(numeric-expression)}

参数

  • numeric-expression - 解析为计算平方根的正数的表达式。

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

描述

SQRT 返回 numeric-expression 的平方根。 numeric-expression 必须是正数。负数值表达式(-0 除外)会生成 SQLCODE -400 错误。如果传递 NULL 值,SQRT 返回 NULL。

SQRT 返回一个精度为 36、小数位数为 18 的值。

SQRT 可以指定为常规标量函数或 ODBC 标量函数(使用大括号语法)。

示例

以下示例显示了两种 SQRT 语法形式。两者都返回 49 的平方根:

SELECT SQRT(49) AS SRoot,{fn SQRT(49)} AS ODBCSRoot

7   7

以下嵌入式 SQL 示例返回整数 0 到 10 的平方根:

/// d ##class(PHA.TEST.SQLFunction).Sort()
ClassMethod Sort()
{
    s a = 0
    while a < 11 {
        &sql(
            SELECT SQRT(:a) INTO :b
        )
        if SQLCODE '= 0 {
            w !,"Error code ",SQLCODE 
        } else {
            w !,"The square root of ",a," = ",b
            s a = a + 1 
        }
    }
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).Sort()

The square root of 0 = 0
The square root of 1 = 1
The square root of 2 = 1.414213562373095049
The square root of 3 = 1.732050807568877294
The square root of 4 = 2
The square root of 5 = 2.236067977499789697
The square root of 6 = 2.449489742783178098
The square root of 7 = 2.645751311064590591
The square root of 8 = 2.828427124746190098
The square root of 9 = 3
The square root of 10 = 3.162277660168379332
#SQL #Caché

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