I am developing an application with FastAPI. It contains a function that brings two variables into a SQL query using the
sqlalchemy.sql.text as shown here.
SELECT ATTR3 FROM TABLE WHERE ATTR1 = TO_NUMBER(:var1) AND (CASE WHEN TO_NUMBER(:var1) NOT IN (1460, 1478, 1481) THEN 'ATTR2 IS NULL' ELSE ATTR2 = TO_NUMBER(:var2) END);
So, after pasting the variables, the SQL query will look as following
SELECT ATTR3 FROM TABLE WHERE ATTR1 = TO_NUMBER('1460') AND (CASE WHEN TO_NUMBER('1460') NOT IN (1460, 1478, 1481) THEN 'ATTR2 IS NULL' ELSE ATTR2 = TO_NUMBER('000') END);
However, I am getting the following error:
ORA-00905: missing keyword
So, now I am wondering how can I resolve this issue? As well as if it is possible to nest a CASE-statement into the WHERE clause?
Here is the logic of m SQL statements:
I have seen some related topics, but they were not helpful.