0
点赞
收藏
分享

微信扫一扫

第八十二章 SQL函数 $LISTDATA

逸省 2022-03-30 阅读 64



文章目录

  • ​​第八十二章 SQL函数 $LISTDATA​​
  • ​​大纲​​
  • ​​参数​​
  • ​​描述​​
  • ​​参数​​
  • ​​list​​
  • ​​position​​
  • ​​示例​​
  • ​​注意​​
  • ​​无效的参数值​​


第八十二章 SQL函数 $LISTDATA

列表函数,指示指定元素是否存在并具有数据值。

大纲

$LISTDATA(list[,position])

参数

  • ​list​​​ - 计算结果为有效列表的表达式。列表是包含一个或多个元素的编码字符串。您可以使用 SQL 或 ObjectScript 的​​$LISTBUILD​​​ 或​​$LISTFROMSTRING​​​ 函数创建列表。您可以使用 SQL 或 ObjectScript​​$LIST​​ 函数从现有列表中提取列表。
  • ​position​​ - 可选 — 指定列表中元素的整数表达式。

描述

​$LISTDATA​​​ 检查列表中请求元素中的数据。如果位置参数指示的元素在列表中并且具有数据值,则 ​​$LISTDATA​​​ 返回值 1。如果元素不在列表中或没有数据值,则 ​​$LISTDATA​​ 返回值 0。

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

参数

list

包含一个或多个元素的编码字符串。可以使用 SQL ​​$LISTBUILD​​​ 函数或 ObjectScript ​​$LISTBUILD​​​ 函数创建列表。可以使用 SQL ​​$LISTFROMSTRING​​​ 函数或 ObjectScript ​​$LISTFROMSTRING​​ 函数将分隔字符串转换为列表。可以使用 SQL L I S T 函 数 或 O b j e c t S c r i p t ‘ LIST 函数或 ObjectScript ` LIST函数或ObjectScript‘LIST` 函数从现有列表中提取列表。

position

如果省略 ​​position​​​ 参数,​​$LISTDATA​​​ 将计算第一个元素。如果 ​​position​​​ 参数的值为 ​​-1​​​,则相当于指定列表的最后一个元素。如果 ​​position​​​ 参数的值引用了一个不存在的列表成员,则 ​​$LISTDATA​​​ 返回 ​​0​​。

示例

以下嵌入式 SQL 示例显示了 ​​position​​ 参数的各种值的结果。

以下所有 ​​$LISTDATA​​​ 语句都返回值 ​​1​​:

/// d ##class(PHA.TEST.SQLFunction).ListData()
ClassMethod ListData()
{
k Y
s a = $lb("Red", , Y, "", "Green")
&sql(
SELECT $LISTDATA(:a), $LISTDATA(:a,1),
$LISTDATA(:a,4), $LISTDATA(:a,5), $LISTDATA(:a,-1)
INTO :b,:c, :d, :e, :f
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"1st element status ",b ; 1st element default
w !,"1st element status ",c ; 1st element specified
w !,"4th element status ",d ; 4th element null string
w !,"5th element status ",e ; 5th element in 5-element list
w !,"last element status ",f ; last element in 5-element list
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListData()

1st element status 1
1st element status 1
4th element status 1
5th element status 1
last element status 1

以下 ​​$LISTDATA​​​ 语句为相同的五元素列表返回值 ​​0​​:

/// d ##class(PHA.TEST.SQLFunction).ListData1()
ClassMethod ListData1()
{
k Y
s a = $LISTBUILD("Red", , Y, "", "Green")
&sql(
SELECT $LISTDATA(:a,2), $LISTDATA(:a,3),
$LISTDATA(:a,0), $LISTDATA(:a,6)
INTO :b,:c, :d, :e
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"2nd element status ",b ; 2nd element is undefined
w !,"3rd element status ",c ; 3rd element is killed variable
w !,"0th element status ",d ; zero position nonexistent
w !,"6th element status ",e ; 6th element in 5-element list
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListData1()

2nd element status 0
3rd element status 0
0th element status 0
6th element status 0

注意

无效的参数值

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

/// d ##class(PHA.TEST.SQLFunction).ListData2()
ClassMethod ListData2()
{
&sql(
SELECT $LISTDATA('fred') INTO :b
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"The the element is ",b
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListData2()

Error code -400

如果 ​​position​​​ 参数的值小于 ​​-1​​​,则会发生 ​​SQLCODE -400​​ 致命错误:

/// d ##class(PHA.TEST.SQLFunction).ListData3()
ClassMethod ListData3()
{
s a = $lb("Red", "Blue", "Green")
&sql(
SELECT $LISTDATA(:a, -3) INTO :c
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"A neg-num position status ",c
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListData3()

Error code -400

当 ​​position​​ 是一个非数字值时,不会报错:

/// d ##class(PHA.TEST.SQLFunction).ListData4()
ClassMethod ListData4()
{
s a = $lb("Red", "Blue", "Green")
&sql(
SELECT $LISTDATA(:a, 'g') INTO :c
)
if SQLCODE '= 0 {
w !,"Error code ",SQLCODE
} else {
w !,"A nonnumeric position status ",c
}
}
DHC-APP>d ##class(PHA.TEST.SQLFunction).ListData4()

A nonnumeric position status 0



举报

相关推荐

0 条评论