Azure Cosmos DB SQL 查询中的标量表达式Scalar expressions in Azure Cosmos DB SQL queries

适用于: SQL API

SELECT 子句支持标量表达式。The SELECT clause supports scalar expressions. 标量表达式是符号和运算符的组合,经计算后可获得单个值。A scalar expression is a combination of symbols and operators that can be evaluated to obtain a single value. 标量表达式的示例包括:常量、属性引用、数组元素引用、别名引用或函数调用。Examples of scalar expressions include: constants, property references, array element references, alias references, or function calls. 可以使用运算符将标量表达式组合成复杂表达式。Scalar expressions can be combined into complex expressions using operators.

语法Syntax

<scalar_expression> ::=  
       <constant>
     | input_alias
     | parameter_name  
     | <scalar_expression>.property_name  
     | <scalar_expression>'['"property_name"|array_index']'  
     | unary_operator <scalar_expression>  
     | <scalar_expression> binary_operator <scalar_expression>    
     | <scalar_expression> ? <scalar_expression> : <scalar_expression>  
     | <scalar_function_expression>  
     | <create_object_expression>
     | <create_array_expression>  
     | (<scalar_expression>)

<scalar_function_expression> ::=  
        'udf.' Udf_scalar_function([<scalar_expression>][,…n])  
        | builtin_scalar_function([<scalar_expression>][,…n])  

<create_object_expression> ::=  
   '{' [{property_name | "property_name"} : <scalar_expression>][,…n] '}'  

<create_array_expression> ::=  
   '[' [<scalar_expression>][,…n] ']'  

参数Arguments

  • <constant>

    表示一个常数值。Represents a constant value. 有关详细信息,请参阅常数部分。See Constants section for details.

  • input_alias

    表示由 FROM 子句引入,由 input_alias 定义的值。Represents a value defined by the input_alias introduced in the FROM clause.
    此值可以保证不是 未定义的 -- 将跳过输入中的 未定义 值。This value is guaranteed to not be undefined -undefined values in the input are skipped.

  • <scalar_expression>.property_name

    表示对象的属性值。Represents a value of the property of an object. 如果该属性不存在或在非对象的值上引用了该属性,则表达式的求值结果为“未定义”值 。If the property does not exist or property is referenced on a value, which is not an object, then the expression evaluates to undefined value.

  • <scalar_expression>'['"property_name"|array_index']'

    表示名为 property_name 的属性的值或数组中索引为 array_index 的数组元素的值。Represents a value of the property with name property_name or array element with index array_index of an array. 如果不存在属性/数组索引,或对非对象/数组的值引用了属性/数组索引,则表达式的求值结果为未定义值。If the property/array index does not exist or the property/array index is referenced on a value that is not an object/array, then the expression evaluates to undefined value.

  • unary_operator <scalar_expression>

    表示应用于单个值的运算符。Represents an operator that is applied to a single value. 有关详细信息,请参阅运算符部分。See Operators section for details.

  • <scalar_expression> binary_operator <scalar_expression>

    表示应用于两个值的运算符。Represents an operator that is applied to two values. 有关详细信息,请参阅运算符部分。See Operators section for details.

  • <scalar_function_expression>

    表示由函数调用结果定义的值。Represents a value defined by a result of a function call.

  • udf_scalar_function

    用户定义的标量函数的名称。Name of the user-defined scalar function.

  • builtin_scalar_function

    内置标量函数的名称。Name of the built-in scalar function.

  • <create_object_expression>

    表示通过使用指定属性及其值创建一个新对象来获得的值。Represents a value obtained by creating a new object with specified properties and their values.

  • <create_array_expression>

    表示通过使用指定值作为元素创建一个新数组来获得的值。Represents a value obtained by creating a new array with specified values as elements

  • parameter_name

    表示指定参数名称的值。Represents a value of the specified parameter name. 参数名称必须以单个 @ 作为第一个字符。Parameter names must have a single @ as the first character.

备注Remarks

调用内置的或用户定义的标量函数时,必须定义所有参数。When calling a built-in or user-defined scalar function, all arguments must be defined. 如果未定义任何参数,则不会调用该函数,且结果也将为未定义。If any of the arguments is undefined, the function will not be called and the result will be undefined.

创建对象时,将跳过任何已分配未定义值的属性,并且在创建的对象中不包含该属性。When creating an object, any property that is assigned undefined value will be skipped and not included in the created object.

创建数组时,将跳过任何已分配“未定义”值 的元素值,并且在创建的对象中不包含该值。When creating an array, any element value that is assigned undefined value will be skipped and not included in the created object. 这将导致下一个定义的元素取代该值时,创建的数组将不含跳过的索引。This will cause the next defined element to take its place in such a way that the created array will not have skipped indexes.

示例Examples

    SELECT ((2 + 11 % 7)-2)/3

结果有:The results are:

    [{
      "$1": 1.33333
    }]

在以下查询中,标量表达式的结果是一个布尔值:In the following query, the result of the scalar expression is a Boolean:

    SELECT f.address.city = f.address.state AS AreFromSameCityState
    FROM Families f

结果有:The results are:

    [
      {
        "AreFromSameCityState": false
      },
      {
        "AreFromSameCityState": true
      }
    ]

后续步骤Next steps