M[UMPS] Functions - $Q[UERY]

Introduced in the 1990 ANSI M[UMPS] language standard.

This function returns information about other defined information in a data-structure, based on the collating sequence in use for that data-structure.

Assume that a global variable ^A exists as depicted in the diagram with the function $ORDER. The code:
SET REF="^A"
FOR I=1:1:20 SET REF=$QUERY(@REF) WRITE " ",REF

will produce the following print-out:

^A(-34) ^A(-4) ^A(-3) ^A(-2) ^A(2) ^A(3) ^A(3,1)
^A(3,2) ^A(3,10) ^A(3,10,3) ^A(4) ^A(34) ^A("-5A")
^A("5A") ^A("A") ^A("AB") ^A("AD) ^A(B")  ^A(-34)

KILL ^ABC
SET ^ABC(1,2,3)="Demo"
SET X=$QUERY(^ABC(1,""))

The effect of this reference to the function $QUERY is not portable. Use of the empty string as a subscript is only portable in the context of the function $ORDER.
While some implementations may return the value "^ABC(1,2,3)" in local variable X, others may return an error.

KILL ^X
SET ^X(1,2,1)=""
SET ^X(1,2,2)=""
SET ^X(1,3)=""
SET A=$QUERY(^X(1,1))

will return the value "^X(1,2,1)" in local variable A.

KILL ^X
SET ^X("-80 apples")="X"
SET ^X(-30)="N"
SET ^X(-7)="A"
SET ^X(-3.5)="B"
SET ^X(0)="W"
SET NAM="^X"
FOR  SET NAM=$QUERY(@NAM) QUIT:NAM="" WRITE " ",@NAM
N A B W X

Note that the string "-80 apples" collates after the highest number.

Addition in the 1995 ANSI M[UMPS] language standard.

The return value of $QUERY contains an environment specification if and only if the parameter value contains an environment specification.

Assume that a process is operating in an environment called "first" and that the following variables exist:
lvn(1) = "local"
^one = 1
^one(1) = 1
^one(3) = 3

Also, assume that in an environment called "second" the following variables exist:
^two = 2
^two(2) = 22
^two(2,2,2,2)=2222

Reference Value
$QUERY(lvn) "lvn(1)"
$QUERY(^one) "^one(1)"
$QUERY(^|"first"|one) "^|""first""|one(1)"
$QUERY(^|"second"|two) "^|""second""|two(2)"
$QUERY(^one(^|"second"|two)) "^one(3)"
SET x=^|"second"|two(2)
SET y=$QUERY(^(2))
y="^two(2,2,2,2)"
SET x=$QUERY(^|"second"|two)
SET y=$QUERY(lvn)
x="^|""second""|two(2)"
y="lvn(1)"
SET x=$QUERY(^|"second"|two)
SET y=$QUERY(^one)
x="^|""second""|two(2)"
y="^one(1)"

Additions in a future ANSI M[UMPS] language standard.

KILL ^ABC
SET ^ABC(1,2,3)="Demo"
WRITE "-->""",$REFERENCE,""""
-->"^ABC(1,2,3)"
SET A=$QUERY(^ABC(1))
WRITE "-->""",A,""", and """,$REFERENCE,"""."
-->"^ABC(1,2,3)", and "".

After a reference to the function $QUERY, the naked indicator becomes undefined, which means that $REFERENCE becomes equal to the empty string.

Reverse $QUERY will work in a manner analogous to reverse $ORDER:
$QUERY(^A("-5A"),-1) = "^A(34)"

When the function $QUERY returns the name of a standardized structured system variable, the name will be returned in a "canonical" form. In its canonical form, the parts of these names that may be abbreviated will be fully spelled, and those parts of these names that are case insensitive will be converted to upper case only.

E.g. $QUERY(^$C("M")) could return "^CHARACTER("M","COLLATE")"

Examples with naked references:

$QUERY(GLVN)
SET ^ABC(1,2)="reset naked indicator"
; Naked indicator is now ^ABC(1,
SET x=$QUERY(^(3,4))

; 1. fetch ^(3,4) = ^ABC(1,3,4)
; 2. the naked indicator becomes undefined after a reference to $QUERY


This document is © Ed de Moel, 1995-2005.
It is part of a book by Ed de Moel that is published under the title "M[UMPS] by Example" (ISBN 0-918118-42-5).
Printed copies of the book are no longer available.