$()
Returns the value of a named variable stored in the query context.
Syntax
$(KEY[, DEFAULT])
Parameters
KEY- The name of the context variable to look up. Required. Must be a string. If
null, error code 90611 is raised. DEFAULT- The value to return when the specified variable does not exist in the context. Optional. If omitted, a missing variable returns
null. Any expression can be used.
Description
The $() function returns the value of the variable identified by KEY from the query context at execution time. Context variables can be set with the set or evalc commands, or passed as arguments when calling a procedure.
If the specified KEY exists in the context, its value is returned. If it does not exist, DEFAULT is returned. If DEFAULT is also not specified, null is returned.
Passing null as KEY or omitting the argument causes an error during the parsing phase.
Error codes
| Error code | Message | Description |
|---|---|---|
| 90610 | No value provided. | No argument was passed. |
| 90611 | Null value provided. | null was passed as KEY. |
Usage examples
To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.
-
Referencing a variable set with the
setcommandset threshold = 100 | json "{'val': 150}" | eval result = $("threshold") | # result: 100 -
Querying a table for the last 7 days from the current time
set from = ago("7d") | set to = str(now()) | table from=$("from") to=$("to") WEB_APACHE_SAMPLE | # Retrieves records within the time range specified by the from and to variables. -
Using the default value when the context variable does not exist
json "{'val': 1}" | eval result = $("undefined_var", "default_value") | # result: default_value -
Default value is ignored when the context variable exists
set greeting = "hello" | json "{'val': 1}" | eval result = $("greeting", "fallback") | # result: hello -
Returns null when no default is specified and the variable does not exist
json "{'val': 1}" | eval result = $("undefined_var") | # result: null
Compatibility
The $() function has been available since before Sonar 4.0.