--- title: "SQL Client | Query methods | N3uron KB V1.22" slug: "sql-query-methods" description: "SQL Client is a N3uron module designed to allow users to quickly and easily connect to the following SQL databases: Microsoft SQL Server, Oracle MySQL…" updated: 2026-11-20T07:33:08Z published: 2026-11-20T07:33:08Z canonical: "docs.n3uron.com/sql-query-methods" --- > ## Documentation Index > Fetch the complete documentation index at: https://docs.n3uron.com/llms.txt > Use this file to discover all available pages before exploring further. # Query methods A Query Method defines a named database operation with specified input arguments and a return format. Callers can include Web Vision through its Call Module Method action, or MCP clients through the MCP Server. When a caller invokes the method, the SQL Client validates the arguments, builds and executes the SQL statement on the selected channel, and returns the result in the configured format. > [!WARNING] > **Note**: > > **From N3uron version 1.22.8**, it is available in the SQL Client instance under **Query methods**. ## Create a Query Method 1. Open **System > Config** and select the **SQL Client** instance. 2. Create and save the channel that connects to the target database. The example in this article uses the **WindFarmSCADA** channel and the `turbine_telemetry`` `view in the `wind_farm_scada` database. 3. Select **Query methods**. Open the **Model** menu and select **New QueryMethod**. Name the method `GetTurbineTelemetry`. 4. Select the method and configure its settings, pipeline and schemas as described below. Use **Run** in the **Pipeline** header to test the editor configuration before saving it. 5. After the test succeeds, click **Save** and confirm the module restart to load the configuration. The published method name is `queryMethods.<channel>.<method>`. For this example, it is `queryMethods.WindFarmSCADA.GetTurbineTelemetry`. The channel and method names must not contain a dot. ## Method Settings | **Setting** | **Description** | | --- | --- | | Enabled | Publishes the method. The default is **Yes**. A disabled method is not listed and cannot be called. | | Description | Explains what the method does and what it returns. Use one or two sentences. The input property descriptions are appended automatically, one line per property. | | Channel | Selects the channel whose database receives the statement. The list uses the **saved** instance configuration and marks disabled channels. Save a newly created channel before selecting it here. A channel name can also be typed or supplied as an expression. If the channel is missing or disabled, the method is still published, but calls fail with `CHANNEL_NOT_FOUND`. The module also logs a warning when it starts. | | Modifies the database | Declares whether the method can change database data or structure. The default is **No**. Select **Yes** for statements such as `INSERT`, `UPDATE` or `DELETE`, structure changes such as `CREATE`, `ALTER`, `DROP` or `TRUNCATE`, and procedures that write. A method marked **Yes** requires write permission on the module and is hidden from read-only clients. | > [!WARNING] > **Note**: > > The module does not inspect the SQL to decide whether a method writes. Set **Modifies the database** to **Yes** for every method that can write. Leaving it at **No** does not make the statement read-only. ## Pipeline A normal call runs these stages in order: 1. Validate the caller's argument against **Input schema**. The **Query script** reads the validated value through `$.input` and puts the statement in `$.output`. 2. Execute the statement on the selected channel within **Statement timeout** and commit the database transaction. 3. Convert the database result into the answer. **Rows** and **Single value** use fixed answer formats. **Script** runs the **Parser script** and validates its answer against **Output schema**. The controls below follow their order in the WebUI. The **Run** test has different commit behavior, described in **Test the Pipeline**. | **Setting** | **Description** | | --- | --- | | Statement timeout | Maximum time for the database operation, in milliseconds. The default is **30000**. The valid range is **1000 to 300000**. Calls to the same method run one at a time, in arrival order. Waiting behind those calls and waiting for a pooled database connection both count toward this limit. If the limit expires while waiting, the call fails without running its Query script. Query and Parser script execution time is separate from this setting. Each script has its own **30-second** execution limit. | | Parser type | Chooses how the database result becomes the answer: **Rows** (the default), **Single value** or **Script**. | | Query script | Builds the SQL statement once per call. Set `$.output` to a bound statement with SQL text and separate values, or to a complete SQL string. An empty statement fails the call. | | Parser script | Shown only when **Parser type** is **Script**. Reads the database result through `$.input` and puts the answer in `$.output`. The answer must match **Output schema**. | ### **Query Script** Use a bound statement to keep argument values separate from the SQL text. Put a `:name` placeholder in `text` and the matching entry in `values`. The driver binds the values; do not add SQL quotes around the placeholders. For example, the following PostgreSQL query returns the latest telemetry row for one turbine. It uses the required String input property `turbineId`, configured in the example below. The view contains `turbine_id`, `active_power_kw`, `wind_speed_ms` and `observed_at`. ```javascript $.output = {  text: "SELECT turbine_id, active_power_kw, wind_speed_ms, observed_at FROM turbine_telemetry WHERE turbine_id = :turbineId ORDER BY observed_at DESC LIMIT 1",  values: { turbineId: $.input.turbineId } }; ``` The `LIMIT 1` clause bounds this example to one row. Query Methods do not truncate result rows. Use a row limit appropriate to your database and operation. Bound values can include strings, numbers, booleans, dates, binary buffers and `null`. If an optional argument is omitted, the script reads `undefined`. Convert it to an explicit value such as `null` before binding it. For example: `$.input.limit === undefined ? null : $.input.limit`. The editor checks these scripts as ES2019; it does not accept `??` or `?.`. The `values` field can also be an array with driver-specific positional placeholders, such as `?` for MySQL/MariaDB or `$1`, `$2` for PostgreSQL/TimescaleDB. A bound statement runs one SQL statement. On PostgreSQL, multiple statements separated by semicolons require the string form. > [!WARNING] > Note: > > A complete SQL string is also accepted, but it does not bind caller arguments. The script must escape values inserted into the SQL text. The module logs a warning on every call that uses this form. Prefer the bound statement for caller-supplied values. ### **Script Context** The Query script and the Parser script use the following context. Their `$.input` and `$.output` have different meanings. | **Member** | **Purpose** | | --- | --- | | `$.input` | In the **Query script**, the validated call argument. With an Object input, read a property with `$.input.turbineId`. With None, it is an empty object. With another root type, it is the whole argument value. In the **Parser script**, it is the database result: one Dataset, an array of Datasets for multiple result sets, or an empty Dataset if there is no result set. | | `$.output` | In the **Query script**, the SQL string or bound statement. In the **Parser script**, the JSON-compatible answer that must match Output schema. For a None output, leave it empty. | | `$.event.ts` | Read-only timestamp at which the call was received, in Unix epoch milliseconds. Assigning to it fails the script. | | `$.local` | Stores values between calls of this method. The Query and Parser scripts of the same method share it. A module restart clears it. | | `$.logger` | Writes custom messages with `error`, `warn`, `info`, `debug` or `trace`. For example, `$.logger.info("Turbine: %s", $.input.turbineId)` in a Query script. | | `moment`, `sprintf`, `Buffer` | Provide date/time handling, formatted strings and binary data handling. Moment includes Moment Timezone. See [User-defined scripts](https://docs.n3uron.com/docs/sql-client-user-defined-scripts) for the shared library references. | ### **Parser Types and Answers** | **Parser type** | **Answer** | | --- | --- | | Rows | Returns every result set with column names and types, row arrays and a row count. The answer contains `ok`, `durationMs` and `results`. Each entry in `results` has `columns`, `rows` and `rowCount`. There is no truncation flag. | | Single value | Returns `ok`, `durationMs` and `value`. The value is the first column of the first row of the first result set. It is `null` if no row is returned. | | Script | Returns the value built by the Parser script, checked against Output schema. It does not add the Rows or Single value answer wrapper. Use it to return a number, text, an array or a structured answer for a specific caller. | For Rows, the answer has the following structure. This one-row example illustrates the fields; telemetry values and execution time vary between calls. ```json {  "ok": true,  "durationMs": 31,  "results": [    {      "columns": [        {          "name": "turbine_id",          "type": "TEXT"        },        {          "name": "active_power_kw",          "type": "DOUBLE"        },        {          "name": "wind_speed_ms",          "type": "DOUBLE"        },        {          "name": "observed_at",          "type": "TIMESTAMP"        }      ],      "rows": [        [          "WTG01",          1758,          8.5,          "2026-11-20T00:21:18.396Z"        ]      ],      "rowCount": 1    }  ] } ``` ## Input Schema Input schema defines the argument that callers can pass. Validation occurs before the Query script runs. The root **Data type** defaults to **Object**. This is the usual choice for named arguments, with one entry in **Properties** for each argument. | **Data type** | **Accepted value** | | --- | --- | | None | No arguments. Call the method with `{}`; the Query script receives an empty object. | | Boolean | `true` or `false`. | | Numeric | A number. **Numeric type** selects a decimal or an integer type. | | Date | An ISO 8601 date-time string, such as `2026-01-31T12:00:00Z`. Without a time zone, the node's local time applies. Invalid date text is rejected. The Query script receives a JavaScript Date. | | String | Any text. | | Fixed string | Exactly one entry from **Allowed values**. Matching is case-sensitive. Add at least one allowed value; the choices are published to callers. | | Any | Any JSON value, without a type check. | | Array | A list. Configure **Items** to define the type shared by every item. For an array of objects, configure the properties of one item there. | | Object | Named **Properties**, each with its own type. Nested Object and Array values can describe deeper structures. An Object schema with no properties accepts only `{}`. | ### **Numeric Type** This control is shown when **Data type** is **Numeric**. The default is **Decimal**, which accepts finite numbers. Integer types also check that the number is whole and within range. All numeric types are published as JSON numbers. | **Numeric type** | **Range** | | --- | --- | | Decimal | Any finite number. | | Integer (8, 16, 32 or 48 bits) | For a size of N bits: −2N−1 to 2N−1−1. For example, Integer (32 bits) accepts −2147483648 to 2147483647. | | Unsigned integer (8, 16, 32 or 48 bits) | For a size of N bits: 0 to 2N−1. For example, Unsigned integer (8 bits) accepts 0 to 255. | ### **Array Items** When **Data type** is **Array**, open **Items** to configure the type of each item. The **Items type** setting defaults to **String**. The choices are **Boolean**, **Numeric**, **Date**, **String**, **Fixed string**, **Any** and **Object**. None and Array are not offered as item types. To describe nested arrays, use an Object item with an Array property. These controls apply to both Input schema and the Script parser's Output schema. ### **Object Properties** Open the **Properties** menu and select **New SchemaProperty**. The entry name becomes the JSON property name and the name read by the script. Names can contain letters, digits, underscores and hyphens, but no dots. | **Setting** | **Description** | | --- | --- | | Data type | The property type. The default is **String**. The choices are the same as the root types above, except None. Conditional Numeric type, Allowed values, Items and Properties controls follow the selected type. | | Required | The default is **Yes**: a value without this property is rejected. Set **No** to allow the caller to omit it. An omitted optional input property is `undefined` in the Query script. | | Description | Explains the property, units, format or accepted values. It is appended to the method description so the caller can understand the argument without reading the script. | ## Output Schema Output schema is shown only when **Parser type** is **Script**. It defines the answer that the Parser script leaves in `$.output`. The root type defaults to **Object**; the same type and conditional controls described for Input schema apply. For a Date output, the Parser script can return a Date or a valid ISO 8601 date-time string. A JSON caller receives a string. A None output resolves no value. Any accepts whatever JSON-compatible value the script builds. An optional output property can be omitted when its Required setting is No. If the answer does not match this schema, a normal call fails with `SCRIPT_RUNTIME_ERROR` and identifies the field at fault. A binary answer is represented for JSON callers as `{ "$binary": "base64-encoded-data" }`. > [!NOTE] > Note: > > Rows and Single value have fixed answer schemas, which the module publishes automatically. Switching to either parser type removes the custom Output schema when the configuration is saved. ## Example: Read Wind Turbine Telemetry This example reads the latest row for turbine WTG01. It uses an existing PostgreSQL view named `turbine_telemetry`. Adapt the database, view and column names to your plant. The database user configured on the channel must be able to read that view. 1. Set **Enabled** to **Yes**, **Channel** to **WindFarmSCADA** and **Modifies the database** to **No**. Enter the description *Return the latest telemetry row for one wind turbine.* 2. Keep **Statement timeout** at **30000** and **Parser type** at **Rows**. Paste the bound query from **Query Script** into the Query script editor. 3. Under **Input schema**, keep **Data type** at **Object**. Add a property named `turbineId`. Keep its **Data type** at **String** and **Required** at **Yes**. Enter the description *Wind turbine identifier, for example WTG01.* ![SQL Client GetTurbineTelemetry method on WindFarmSCADA with Rows parser and required String input turbineId](https://cdn.document360.io/54093ab5-6b22-4542-a265-04377931f11a/Images/Documentation/method-settings.png) ### **Test the Pipeline** > [!WARNING] > Note: > > Run executes the scripts currently in the editor without saving them. Its database transaction is rolled back. On **MySQL and Oracle**, structure-changing statements such as `CREATE`, `ALTER`, `DROP` and `TRUNCATE` can commit themselves and earlier changes in the same test. Rollback cannot undo those changes. Use a separate test database for structure changes. See the [MySQL](https://dev.mysql.com/doc/refman/8.4/en/implicit-commit.html) and [Oracle](https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/COMMIT.html) transaction rules. 1. Click **Run** in the **Pipeline** header. 2. Enter `{"turbineId":"WTG01"}` in **Arguments (JSON)**. This is the argument the caller would supply. Leave **Event ts** empty to use the current time. 3. Click **Build statement**. Check the SQL text and the separate `:turbineId` value. This step builds the statement without executing it. 4. Click **Execute and parse (rolled back)**. Check the execution status, returned row, answer and log. The example returns one row for WTG01 with active power, wind speed and observation time. 5. Try `{}` to see the missing required argument error. Try `{"turbineId":123}` to see the type error. Correct the argument before continuing. ![Actual Query Methods Run pipeline: supply turbineId WTG01, build the bound statement, execute it with rollback and inspect the telemetry row](https://cdn.document360.io/54093ab5-6b22-4542-a265-04377931f11a/Images/Documentation/run-pipeline.webp) Missing or incorrectly typed arguments are rejected with `INVALID_ARGUMENT`. For this example, the errors identify `arguments.turbineId` as required or report that a string was expected. ### **Return a Single Value** Set **Parser type** to **Single value** and run the same query. Because `turbine_id` is the first selected column, `value` is `"WTG01"`. If the query returns no row, `value` is `null`. To return active power instead, put that column first in the SQL statement or use the Script parser below. ### **Return Active Power with a Parser Script** 1. Set **Parser type** to **Script**. 2. Under **Output schema**, set **Data type** to **Numeric**. Keep **Numeric type** at **Decimal**. 3. Paste the following code into **Parser script**. Keep the bound Query script and Input schema from the Rows example. ```javascript const dataset = Array.isArray($.input) ? $.input[0] : $.input; if (dataset.getRowCount() === 0) {  throw new Error("No telemetry was found for the requested turbine."); } $.output = dataset.getRow(0).getValue("active_power_kw"); ``` The script selects the first Dataset, checks that a row exists, and reads its `active_power_kw` field. It returns a number in kilowatts, such as `1758`. It does not return a wrapper with `value`. For the Dataset and DatasetRow methods, see [User-defined scripts](https://docs.n3uron.com/docs/sql-client-user-defined-scripts#dataset). ![GetTurbineTelemetry with Script parser and Numeric Decimal Output schema; required String turbineId input is retained](https://cdn.document360.io/54093ab5-6b22-4542-a265-04377931f11a/Images/Documentation/script-settings.png) Run the pipeline again with WTG01 and check the numeric answer. If the Parser script returns text while Output schema is Numeric, Run displays the answer with a schema warning for diagnosis. It states that a real call would fail with `SCRIPT_RUNTIME_ERROR`. Close Run when the tests are complete. Keep the parser type and answer schema that your caller needs, then click **Save** and confirm the module restart. Callers use the saved method configuration.