sql 自定义函数 示例_SQL Server Choose()函数介绍和示例

本文介绍了SQL Server 2012及更高版本中的Choose()函数,作为Case语句的一种简洁替代,用于根据指定的索引位置返回值。文章详细展示了多个示例,包括使用文字值、十进制索引、字符串值和与JSON数据结合的使用情况,以及与CASE语句的比较,得出两者在逻辑上等效的结论。
摘要由CSDN通过智能技术生成

sql 自定义函数 示例

In the article, a CASE statement in SQL, we explored one of the important logical expressions – the Case statement in SQL Server to return the values based on the specified condition. Today, we will talk about another useful logical Choose() function in SQL Server 2012 onwards and its comparison with the Case statement.

在本文中, SQL中CASE语句探讨了重要的逻辑表达式之一-SQL Server中的Case语句,用于根据指定条件返回值。 今天,我们将讨论SQL Server 2012及更高版本中另一个有用的逻辑Choose()函数及其与Case语句的比较。

SQL中的CASE语句概述 (An Overview of the CASE statement in SQL )

Before we proceed with the Choose function, let’s have a quick overview of the Case statement in SQL

在继续执行Choose函数之前,让我们快速了解一下SQL中的Case语句

  • It is a logical function, and we define conditions ( in When clause) and actions followed by Then clause

    这是一个逻辑函数,我们定义条件(在When子句中)和随后的Then子句的操作
  • Once the expression or value satisfies in the when clause, it returns corresponding value or expression in the output

    表达式或值在when子句中满足后,它将在输出中返回相应的值或表达式
  • If no conditions are satisfied, it returns the value specified in the Else clause

    如果不满足任何条件,则返回Else子句中指定的值

In the below example, we specify product id 1 in the variable, and it returns the output ‘Bread and Biscuits’.

在下面的示例中,我们在变量中指定产品ID 1,并返回输出“面包和饼干”。

SQL Case Statement

SQL Server Choose()函数简介 (Introduction to SQL Server Choose() function)

Suppose you need to specify multiple conditions in the case statement. In this case, the overall code will be lengthy. Sometimes, it is difficult to examine more extended code, and we always look for functions that could do similar work with minimum efforts and without any performance impact. Choose function does the same work for us and can be used as a replacement of Case statement.

假设您需要在case语句中指定多个条件。 在这种情况下,整个代码将很长。 有时,很难检查更多扩展的代码,并且我们总是寻找能够以最小的努力完成类似工作并且不影响性能的函数。 选择功能为我们完成了相同的工作,可以用作Case语句的替代。

选择功能的语法 (Syntax of Choose function)

We use Choose () function to return an item at a specific index position from the list of items.

我们使用Choose()函数从项目列表中的特定索引位置返回项目。

Syntax of Choose function: CHOOSE ( index, value[1], value[2] ,….. value[N] )

选择函数的语法 : CHOOSE(index,value [1],value [2],….. value [N])

  • Index: It is an integer that specifies the index position of the element we want in the output. Choose function does not use a zero-based indexing method. In this function, the first item starts at first, the second element at the 2nd index position, and so on. If we do not use an integer in this argument, SQL converts that into integer else returns a NULL value Index :它是一个整数,指定我们想要的元素在输出中的索引位置。 选择函数不使用基于零的索引方法。 在此功能中,第一个项目首先开始,第二个元素在第二个索引位置,依此类推。 如果在此参数中不使用整数,SQL会将其转换为整数,否则返回NULL值
  • Items: It is a comma-separated list of any type. Choose function picks the items as per the index defined in the first argument Items :它是任何类型的逗号分隔列表。 选择函数根据第一个参数中定义的索引选择项目

For the index, choose function returns value[1] for index position 1. Let’s understand choose function in SQL using various examples.

对于索引,select函数返回索引位置1的value [1]。让我们使用各种示例来了解SQL中的select函数。

示例1:带文字值SQL Server CHOOSE()函数 (Example 1: SQL Server CHOOSE() function with literal values)

In this example, we use choose() function to return values from different index positions. The first select statement returns NULL because this function does not use a zero indexing method.

在此示例中,我们使用choose()函数从不同的索引位置返回值。 第一条选择语句返回NULL,因为此函数未使用零索引方法。

SELECT CHOOSE(0, 'Apple', 'Banana', 'Mango', 'Grapes', 'Strawberry') AS 'Zero Index value';
SELECT CHOOSE(1, 'Apple'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值