Skip to content

SQL code block dependencies #2025

@tschaub

Description

@tschaub

In working with SQL views, I struggled to find a way to ensure that code blocks that create views run before code blocks that use those views. I settled on the pattern below, but I'm wondering if there is an alternative way to accomplish the same.

This SQL block creates a view. The id directive allows another code block to depend on it running first.

```sql id=[testView]
CREATE OR REPLACE VIEW test_view AS SELECT 'it works' AS testing;

SELECT 'test_view' AS name;
```

This SQL block depends on the one above. It uses query_table to refer to the view name from the SELECT above (rather than selecting from test_view by name).

```sql
SELECT * from query_table(${testView.name});
```

I'm wondering if I am missing documentation on a code block directive that might be used to indicate that one code block depends on another.

I think I also found that SQL code blocks run from bottom to top if they otherwise don't have references that would sort them in a different order - but I don't know if this is guaranteed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions