Reputation: 31
I am using CTE with clause in Snowflake and trying to insert records into the target table ent.P_ACCOUNTS_EXTRACT_SHR. But a compilation error throwing while executing the query.
Error: syntax error line 27 at position 4 unexpected 'INSERT'.
I am using two CTE temp_product and latest_custodian and then inserting into the target table.
with temp_Product
AS (
select Product_EDW_Id From ent.PRODUCT
Where
PRODUCT_CLASS_NM IN ('Individual', 'Composite')
UNION ALL
SELECT R.Participating_Product_EDW_Id FROM ent.PRODUCT p
INNER JOIN ent.Product_Group_Relationship R
ON P.PRODUCT_EDW_ID = R.PRODUCT_EDW_ID
),
Latest_Custodian
AS (
SELECT
c.Src_Sys_Custodian_Account_Id,
c.client_edw_id,
c.Create_dt,
ROW_NUMBER() OVER(PARTITION BY c.Src_Sys_Custodian_Account_Id, c.client_edw_id
ORDER BY c.Create_dt DESC) as RNUM1
FROM ent.Product P
INNER JOIN temp_Product TP
ON P.Product_EDW_Id = TP.Product_EDW_Id
Left Join ent.custodian C
ON P.UDF7_TX = C.SRC_SYS_CUSTODIAN_ACCOUNT_ID AND
P.CLIENT_EDW_ID = C.CLIENT_EDW_ID
)
INSERT INTO ent.P_ACCOUNTS_EXTRACT_SHR (
IS_FLAG,
CREATE_DT,
ACCOUNT_CODE,
TRADABLE_DATE,
CLOSE_DATE,
ACCOUNT_TYPE,
TRUST_OFFICER,
TRUST_OFFICER_CITY,
TRUST_OFFICER_PHONE,
STATEMENT_ACCOUNT_NUMBER,
CUSTODIAN_CODE,
CUSTODIAN,
TAXABLE,
ACCOUNT_ATTRIBUTE,
ACCOUNT_NAME
)
SELECT
'0',
CURRENT_TIMESTAMP(),
P.Client_Product_Id,
P.INCEPTION_DT,
P.src_sys_close_dt,
P.UDF31_TX,
P.UDF15_TX,
P.UDF16_TX,
P.UDF17_TX,
P.UDF7_TX,
C1.custodian_mnemonic_nm,
P.UDF8_TX,
P.tax_exempt_fl,
P.udf35_tx,
P.PRODUCT_NM
from ent.PRODUCT P
INNER JOIN temp_Product TP
ON P.Product_EDW_Id = TP.Product_EDW_Id
LEFT JOIN Latest_Custodian C
ON P.UDF7_TX = C.SRC_SYS_CUSTODIAN_ACCOUNT_ID AND
P.client_edw_id = C.client_edw_id AND
RNUM1 = 1
LEFT JOIN ent.CUSTODIAN C1
ON C.SRC_SYS_CUSTODIAN_ACCOUNT_ID = C1.src_sys_custodian_account_id AND
C.create_dt = C1.CREATE_DT
ORDER BY P.product_structure_level_nm;
Upvotes: 2
Views: 1646
Reputation: 176114
The correct syntax is:
INSERT INTO ...
WITH cte AS (...)
SELECT ...
FROM cte;
Sample:
Upvotes: 4