cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with Combination of INSERT + CTE (with clause) + Dynamic query (IDENTIFIER function)

UdayRPai
New Contributor II

Hi,

We are trying to insert into a table using a CTE (WITH clause query).
In the insert we are using the INDENTIFIER function as the catalog name is retrieved dynamically.
This is causing the insert to fail with an error - The table or view `cte_query` cannot be found.
Any idea how this cam be fixed?

Example

CREATE TABLE catalog_name.schema_name.target_table1
(
        col1 string,
        col2 date
);
 
DECLARE VARIABLE var_catalog_name STRING;

SET var_catalog_name = (select catalog_name from system.information_schema.catalogs where catalog_name like 'my_catalog%');
 
INSERT INTO IDENTIFIER(var_catalog_name || '.schema_name.target_table1')
(
col1,
col2
)  
WITH cte_query as
(
select  'A' as col1,
        current_date() as col2
)
select  col1,
        col2
from    cte_query
;

 

3 REPLIES 3

UdayRPai
New Contributor II

Please mark this as resolved.

Hi @UdayRPai ,

Could you share solution of you find one? This way you can help community 😉

Hi,

We used the USE CATALOG statement with EXECUTE IMMEDIATE at the beginning and removed the catalog name from individual insert statements.

 

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group