0
点赞
收藏
分享

微信扫一扫

第八十三章 SQL函数 $LISTFIND



文章目录

  • ​​第八十三章 SQL函数 $LISTFIND​​
  • ​​大纲​​
  • ​​参数​​
  • ​​描述​​
  • ​​示例​​
  • ​​注意​​
  • ​​无效的参数值​​


第八十三章 SQL函数 $LISTFIND

列表函数,用于在指定列表中搜索请求的值。

大纲

$LISTFIND(list,value[,startafter])

参数

  • ​list​​​ - 计算结果为有效列表的表达式。列表是包含一个或多个元素的编码字符串。您可以使用 SQL 或 ObjectScript 的​​$LISTBUILD​​​ 或​​$LISTFROMSTRING​​​ 函数创建列表。可以使用 SQL 或 ObjectScript​​$LIST​​ 函数从现有列表中提取列表。
  • ​value​​ - 包含搜索元素的表达式。一个字符串。
  • ​startafter​​​ - 可选 — 解释为列表位置的整数表达式。搜索从该位置之后的元素开始。​​0​​​和​​–1​​​ 是有效值;​​–1​​ 从不返回元素。零是默认值。

描述

​$LISTFIND​​​ 在指定列表中搜索请求值的第一个实例。搜索从 ​​startafter​​​ 参数指示的位置之后的元素开始。如果省略 ​​startafter​​​ 参数,则 ​​$LISTFIND​​​ 假定 ​​startafter​​​ 值为 ​​0​​​ 并从第一个元素(元素 ​​1​​​)开始搜索。如果找到该值,则 ​​$LISTFIND​​​ 返回匹配元素的位置。如果未找到该值,则 ​​$LISTFIND​​​ 返回 ​​0​​​。如果 ​​startafter​​​ 参数的值引用不存在的列表成员,则 ​​$LISTFIND​​​ 函数也将返回 ​​0​​。

此函数返回 ​​SMALLINT​​ 类型的数据。

示例

以下嵌入式 SQL 示例返回 ​​2​​,即请求字符串第一次出现的位置:

/// d ##class(PHA.TEST.SQLFunction).ListFind()
ClassMethod ListFind()
{
s a = $lb("Red","Blue","Green")
&sql(
SELECT $LISTFIND(:a, 'Blue') INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind()

The position is 2

以下嵌入式 ​​SQL​​​ 示例返回 ​​0​​,表示未找到请求的字符串:

/// d ##class(PHA.TEST.SQLFunction).ListFind1()
ClassMethod ListFind1()
{
s a = $lb("Red","Blue","Green")
&sql(
SELECT $LISTFIND(:a, 'Orange') INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind1()

The position is 0

以下三个嵌入式 SQL 示例显示了使用 ​​startafter​​​ 参数的效果。第一个示例没有找到请求的字符串并返回 ​​0​​​,因为请求的字符串出现在 ​​startafter​​ 位置:

/// d ##class(PHA.TEST.SQLFunction).ListFind2()
ClassMethod ListFind2()
{
s a = $lb("Red","Blue","Green")
&sql(
SELECT $LISTFIND(:a, 'Blue', 2) INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind2()

The position is 0

第二个示例通过将 ​​startafter​​ 设置为零(默认值)来在第一个位置找到请求的字符串:

/// d ##class(PHA.TEST.SQLFunction).ListFind3()
ClassMethod ListFind3()
{
s a = $lb("Red","Blue","Green")
&sql(
SELECT $LISTFIND(:a, 'Red', 0) INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind3()

The position is 1

第三个示例找到请求的字符串的第二次出现并返回 ​​5​​​,因为第一次出现在 ​​startafter​​ 位置之前:

/// d ##class(PHA.TEST.SQLFunction).ListFind4()
ClassMethod ListFind4()
{
s a = $lb("Red","Blue","Green","Yellow","Blue")
&sql(
SELECT $LISTFIND(:a, 'Blue', 3) INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind4()

The position is 5

​$LISTFIND​​​ 函数只匹配完整的元素。因此,以下示例返回 ​​0​​​,因为列表中没有元素等于字符串​​“B”​​​,尽管所有元素都包含​​“B”​​:

/// d ##class(PHA.TEST.SQLFunction).ListFind5()
ClassMethod ListFind5()
{
s a = $lb("ABC", "BCD", "BBB")
&sql(
SELECT $LISTFIND(:a,'B') INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind5()

The position is 0

注意

无效的参数值

如果列表参数中的表达式未计算为有效列表,则 ​​$LISTFIND​​​ 函数会生成 ​​SQLCODE -400​​ 致命错误。

/// d ##class(PHA.TEST.SQLFunction).ListFind6()
ClassMethod ListFind6()
{
s a = "Blue"
&sql(
SELECT $LISTFIND(:a, 'Blue') INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind6()

Error code -400

如果 ​​startafter​​​ 参数的值为 ​​-1​​​,则 ​​$LISTFIND​​​ 始终返回零 ​​(0)​​。

/// d ##class(PHA.TEST.SQLFunction).ListFind7()
ClassMethod ListFind7()
{
s a = $lb("Red", "Blue", "Green")
&sql(
SELECT $LISTFIND(:a, 'Blue', -1) INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind7()

The position is 0

如果 ​​startafter​​​ 参数的值小于 ​​-1​​​,则调用 ​​$LISTFIND​​​ 函数会生成 ​​SQLCODE -400​​ 致命错误。

/// d ##class(PHA.TEST.SQLFunction).ListFind8()
ClassMethod ListFind8()
{
s a = $lb("Red", "Blue", "Green")
&sql(
SELECT $LISTFIND(:a, 'Blue', -3) INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The position is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListFind8()

Error code -400



举报

相关推荐

0 条评论