We are exploring ways to dynamically generate SQL models in dbt. Is there a strict 1:1 requirement between a model and its .sql
file?
Using macros, I was able to dynamically generate multiple SELECT * FROM table_name
statements. However, I encountered the following error:
('42000', "[42000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Incorrect syntax near the keyword 'SELECT'. (156) (SQLMoreResults)")
Here is my all_tables.sql
{% set tables = get_table_names('SalesLT') %}
{% for table_metadata in tables %}
SELECT *
FROM {{table_metadata[1]}}.{{table_metadata[2]}}.{{ table_metadata[0] }}
{% endfor %}
and it generates this
SELECT *
FROM AdventureWorksLT2022.SalesLT.Address
SELECT *
FROM AdventureWorksLT2022.SalesLT.Customer
SELECT *
FROM AdventureWorksLT2022.SalesLT.CustomerAddress
SELECT *
FROM AdventureWorksLT2022.SalesLT.Product
SELECT *
FROM AdventureWorksLT2022.SalesLT.ProductCategory
SELECT *
FROM AdventureWorksLT2022.SalesLT.ProductDescription
SELECT *
FROM AdventureWorksLT2022.SalesLT.ProductModel
SELECT *
FROM AdventureWorksLT2022.SalesLT.ProductModelProductDescription
SELECT *
FROM AdventureWorksLT2022.SalesLT.SalesOrderDetail
SELECT *
FROM AdventureWorksLT2022.SalesLT.SalesOrderHeader
SELECT *
FROM AdventureWorksLT2022.SalesLT.vProductAndDescription
SELECT *
FROM AdventureWorksLT2022.SalesLT.vProductModelCatalogDescription
SELECT *
FROM AdventureWorksLT2022.SalesLT.vGetAllCategories