1

I have a SAP HANA SQL query with two Common CTE's and followed by delete SQL on a regular Table. The SQL execution fails with incorrect syntax. Below is the error code and the HANA SQl script.

WITH CTE_A AS
(
  SELECT COL_1 FROM TABLE_A WHERE
  CONDITION1 = 'ABC'
  AND CONTITION2 = 'XYZ'
),

     CTE_B AS
(
  SELECT COL_1, COL_2 FROM TABLE_A WHERE
  CONDITION3 = 'DEF'
  AND COL_1 IN (SELECT COL_1 FROM CTE_A)
)

DELETE FROM TABLE_A
WHERE ACCOUNT_NO IN (SELECT COL_2 FROM CTE_B);

Error:

(dberror) [257]: sql syntax error: incorrect syntax near "DELETE": line 64 col 0 (at pos 2273);

When replaced the 'DELETE FROM' to 'SELECT * Start', the SQL execution works as expected.

Could one of you please let us know the proper syntax to delete the records?

1 Answer 1

1

CTE's in SAP HANA only supports SELECT statements. UPDATE/ DELETE statements and recursive CTE's are not supported.

You can use subqueries to achieve the same functionality as shown below.

DELETE FROM TABLE_A
WHERE ACCOUNT_NO IN (SELECT COL_2 FROM 
                        ( SELECT COL_1, COL_2 FROM TABLE_A WHERE
                          CONDITION3 = 'DEF'
                           AND COL_1 IN ( SELECT COL_1 FROM TABLE_A WHERE
                                          CONDITION1 = 'ABC'
                                          AND CONTITION2 = 'XYZ'
                                         )
                        )
                     ) ;
Sign up to request clarification or add additional context in comments.

2 Comments

Similar topic has been discussed before in stack link: stackoverflow.com/questions/42487648/…
I think another option is to create temporary views as discussed in above link. The solution depends on if you need to use the temporary results anywhere else, other than the delete query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.