I have to select values from a table in all rows like this:
select distinct SCHM_CODE,
sum(DEP_AMT) as AMOUNT
from DLY_DEP_VIEW
where Schm_code in (select SCHM_CODE
from DLY_DEP_VIEW )
group by schm_code
I will be taking input from user input, I do not want the select statement in the brackets, I need to return a value for all in there like:
select distinct SCHM_CODE,
sum(DEP_AMT) as AMOUNT
from DLY_DEP_VIEW
where Schm_code in (ALL_SCHM_CODES)
group by schm_code
And this is Giving me Invalid Identifier: (EDITS)
select distinct SCHM_CODE,
sum(DEP_AMT) as AMOUNT
from DLY_DEP_VIEW
where Schm_code in (select regexp_substr('" + c + "', '[^,]+',1,level) p
from dual t
connect by level <= regexp_count('" + c + "', ',') + 1
)
group by schm_code;
Since the value in the brackets keep changing in my application. What is the best way to achieve this? The query is inside Java Code.
You can try something like this:
:your_stringis the string you got as input from the user which can contain one value or many (comma separated)Here is a sqlfiddle demo
BTW, use a prepared statement with a bind variable, don’t just concatenate the input string.
Read more here