Skip to main content

What is an Internal variable?

Internal variables in Kvery are a powerful feature for creating dynamic and flexible queries within the system. They allow you to define variables directly in your query, which can then be used to modify the query based on certain conditions or inputs.

Here's an overview of internal variables before we dive into examples:

info

Internal variables are declared within a query using a specific syntax. They are prefixed with a colon (:) to distinguish them from other elements in the query.

For example, :variableName.

Initialization

You can initialize internal variables with fixed values, or their values can be derived from the results of other queries. This makes them extremely versatile for creating adaptive queries. For example, you can set :variableName = 5; or use a select statement to assign a value, such as :variableName = (SELECT COUNT(*) FROM users);.

Usage

Once defined, these variables can be used throughout the query to dynamically filter results, join tables, or in any other part of the SQL statement where dynamic values might be needed. This enables you to write more reusable and adaptable SQL scripts.

Types of Values

Internal variables can hold various types of values, including numbers, strings, or even the results of subqueries. This flexibility allows for sophisticated query construction and manipulation based on the data within your database.

note

The scope of an internal variable is limited to the query in which it is defined. It exists only for the duration of the query execution and is not stored or available outside of that context.

tip

Internal variables are particularly useful in scenarios where the query needs to be adjusted based on external inputs or the results of previous queries within the same execution context. They offer a way to write more efficient, cleaner SQL code by reducing redundancy and increasing the adaptability of your queries.