I have a query which fetches json format data from a column. i want to fetch the data of json by passing a field_name dynamically from the column.
for example
SELECT SUBJECT_MARKS
FROM STUDENT
WHERE STUDENT_ID = 101
result is:
{
"English": "70",
"Hindi": "80",
"Maths": "90",
"Science": "90",
"Social": "85"
}
If I want to get the particular subject marks, then the query is:
SELECT JSON_VALUE(SUBJECT_MARKS,'$.Maths')
FROM STUDENT
WHERE STUDENT_ID = 101
Now the result is:
90
Now my requirement is to get the SUBJECT MARKS dynamically while giving the subject name as parameter in the query
SELECT JSON_VALUE(SUBJECT_MARKS,:pSubjectMarks)
FROM STUDENT
WHERE STUDENT_ID = 101
while execute the query when we give the :pSubjectMarks
as '$.Science'
then it is throwing the error message
ORA-40454: path expression not a literal 40454. 00000 - "path expression not a literal" *Cause: The provided path expression was not a literal (a constant). *Action: Provide a constant path expression. Error at Line: 29 Column: 45
Can anyone help me to find the solution for the query Thanks in advance
You could build the subject you want to get info from into an
EXECUTE IMMEDIATE
statement. Since all of your subjects are simple strings, you can use theDBMS_ASSERT
package to validate the input of thep_subject_name
parameter to prevent any SQL injection from happening.Below is an example on how to build the procedure.
Setup