Skip to main content
Executes another SQL file and returns its results as a JSON array.

Signature

sqlpage.run_sql(file_path TEXT, parameters JSON DEFAULT NULL) -> JSON

Parameters

file_path
TEXT
required
Path to the SQL file to execute, relative to web root
parameters
JSON
Optional JSON object with variables to pass to the included file

Return Value

return
JSON
Array of result rows as JSON objects

Description

The run_sql() function executes another SQL file and returns all query results as a JSON array. This enables code reuse and modular SQL applications.

Examples

Include Common Header

SELECT 'dynamic' as component,
    sqlpage.run_sql('common_header.sql') as properties;

Reuse Query Logic

SET results = sqlpage.run_sql('get_user_data.sql');
SET user_name = $results->>0->>''name'';

SELECT 'text' as component,
    'Hello, ' || $user_name as contents;

Pass Parameters

SET results = sqlpage.run_sql(
    'search.sql',
    json_object('query', :search_term, 'limit', 10)
);

Build docs developers (and LLMs) love