> For the complete documentation index, see [llms.txt](https://docs.rulecube.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rulecube.com/v2.5/language-reference/sql.md).

# SQL

Collection of SQL functions.

### Methods

#### execute

```javascript
execute(connectionString: string | {
                                    server: string,
                                    authenticationType?: string,
                                    userName: string,
                                    password: string,
                                    database: string,
                                    port?: Number,
                                    encrypt?: Boolean,
                                }, query: string, params?: { name: string, value: any, type: SQL.TYPES }[])
```

Executes a T-SQL query. Returns an array of rows with columns as key/value pairs.

**Parameters**

&#x20;   **connectionString&#x20;*****string | { server: string, authenticationType?: string, userName: string, password: string, database: string, port?: Number, encrypt?: Boolean, }***

&#x20;       The connection string or object to the SQL database.

&#x20;   **query&#x20;*****string***

&#x20;       The SQL statement to be executed.

&#x20;   **params&#x20;*****{ name: string, value: any, type: SQL.TYPES }\[]***

&#x20;       (optional) Parameters to be used within the query. Use parameters to avoid SQL injection. See [OWASP SQL Injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html).

**Return type**

&#x20;   { \[key: string]: any; }\[]

**Examples**

```javascript
// Calculates the average of the field 'age' for all users in the table 'Persons':
SQL.execute(connectionString, "SELECT AVG(age) as avg FROM Persons")     // [ { avg: 30 } ]

// Gets all persons with first name 'Joe' from  able 'Persons', using a parameter:
SQL.execute(connectionString, "SELECT * FROM Persons WHERE firstname = @firstname", { name: "firstname", value: "Joe", type: SQL.TYPES.NVarChar })
```

#### executeStoredProcedure

```javascript
executeStoredProcedure(connectionString: string | {
                                    server: string,
                                    authenticationType: string,
                                    userName: string,
                                    password: string,
                                    database: string,
                                    port: Number,
                                    encrypt: Boolean,
                                }, query: string, params?: { name: string, value: any, type: SQL.TYPES }[])
```

Executes a SQL Stored Procedure. Use parameters to avoid SQL injection. [OWASP SQL Injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html).

**Parameters**

&#x20;   **connectionString&#x20;*****string | { server: string, authenticationType: string, userName: string, password: string, database: string, port: Number, encrypt: Boolean, }***

&#x20;       The connection string or object to the SQL database.

&#x20;   **query&#x20;*****string***

&#x20;       The SQL statement to be executed.

&#x20;   **params&#x20;*****{ name: string, value: any, type: SQL.TYPES }\[]***

&#x20;       (optional) Parameters to be used within the query. Use parameters to avoid SQL injection. See [OWASP SQL Injection cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/SQL_Injection_Prevention_Cheat_Sheet.html).

**Return type**

&#x20;   { \[key: string]: any; }\[]

**Examples**

```javascript
// Execute the Stored Procedure / Stored Function / SQL Function with the name 'GetAllCustomers'
SQL.executeStoredProcedure(connectionString, "GetAllCustomers")
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rulecube.com/v2.5/language-reference/sql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
