Can I use a JSON string to condition the results of a query?How do I limit the number of rows returned by an Oracle query after ordering?Faster 'select distinct thing_id,thing_name from table1' in oracleMerge unique rows based on single columnOracle SQL Merge Statement IssuePl/SQL- Get column names from a queryindexes and NOT, OR, AND operatorshow to get around when normal index or bitmap index isn't usefulHow many times does the value A from the column 2 appears with the value B from the column 1 (metadata)Oracle Stored Function not returning result as sql stringorder by on last column of composite index
Verb ending in -ん with positive meaning?
How big would the ice ball have to be to deliver all the water at once?
What is the logical distinction between “the same” and “equal to?”
How do my husband and I get over our fear of having another difficult baby?
How to bring home documents from work?
Smallest PRIME containing the first 11 primes as sub-strings
Can the President of the US limit First Amendment rights?
Writing a program that will filter the integer solutions
How to add the real hostname in the beginning of Linux cli command
Is it mandatory to use contractions in tag questions and the like?
Top off gas with old oil, is that bad?
Impossible violin chord, how to fix this?
What are one's options when facing religious discrimination at the airport?
How to level a picture frame hung on a single nail?
Why would an airline put 15 passengers at once on standby?
Incomplete iffalse: How to shift a scope in polar coordinate?
How can I visualize an ordinal variable predicting a continuous outcome?
Why does it seem the best way to make a living is to invest in real estate?
How deep is the liquid in a half-full hemisphere?
Why has Speaker Pelosi been so hesitant to impeach President Trump?
Looking for circuit board material that can be dissolved
How do we know neutrons have no charge?
What would influence an alien race to map their planet in a way other than the traditional map of the Earth
Can I pay some of the cost of an activated ability lots of times to get more out of the effect?
Can I use a JSON string to condition the results of a query?
How do I limit the number of rows returned by an Oracle query after ordering?Faster 'select distinct thing_id,thing_name from table1' in oracleMerge unique rows based on single columnOracle SQL Merge Statement IssuePl/SQL- Get column names from a queryindexes and NOT, OR, AND operatorshow to get around when normal index or bitmap index isn't usefulHow many times does the value A from the column 2 appears with the value B from the column 1 (metadata)Oracle Stored Function not returning result as sql stringorder by on last column of composite index
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Some procedures I'm working on accept a JSON string as a varchar2 as an input parameter, which contains parameters sent as key-value pairs from our web server that are intended to filter query results. Depending on the use case, certain parameters may not appear in the incoming JSON. I'm wondering if there's any way that I can use the parameter values in the JSON string as conditions in a query, even if certain parameters may not return a value? Something like this:
v_json VARCHAR2 := '"paramA":"valueA","paramB":"valueB"';
//the JSON can include values for either paramA, paramB, and paramC
SELECT *
FROM table
WHERE colA = paramA
AND colB > paramB
AND colC LIKE paramC;
Since the JSON has no paramC, I'd need to run a query like this:
SELECT *
FROM table
WHERE colA = paramA
AND colB > paramB
//condition C shouldn't be considered, since a null value would have
//produce the wrong results
oracle plsql
add a comment
|
Some procedures I'm working on accept a JSON string as a varchar2 as an input parameter, which contains parameters sent as key-value pairs from our web server that are intended to filter query results. Depending on the use case, certain parameters may not appear in the incoming JSON. I'm wondering if there's any way that I can use the parameter values in the JSON string as conditions in a query, even if certain parameters may not return a value? Something like this:
v_json VARCHAR2 := '"paramA":"valueA","paramB":"valueB"';
//the JSON can include values for either paramA, paramB, and paramC
SELECT *
FROM table
WHERE colA = paramA
AND colB > paramB
AND colC LIKE paramC;
Since the JSON has no paramC, I'd need to run a query like this:
SELECT *
FROM table
WHERE colA = paramA
AND colB > paramB
//condition C shouldn't be considered, since a null value would have
//produce the wrong results
oracle plsql
which Oracle version are you working on?
– Kaushik Nayak
Mar 29 at 3:19
add a comment
|
Some procedures I'm working on accept a JSON string as a varchar2 as an input parameter, which contains parameters sent as key-value pairs from our web server that are intended to filter query results. Depending on the use case, certain parameters may not appear in the incoming JSON. I'm wondering if there's any way that I can use the parameter values in the JSON string as conditions in a query, even if certain parameters may not return a value? Something like this:
v_json VARCHAR2 := '"paramA":"valueA","paramB":"valueB"';
//the JSON can include values for either paramA, paramB, and paramC
SELECT *
FROM table
WHERE colA = paramA
AND colB > paramB
AND colC LIKE paramC;
Since the JSON has no paramC, I'd need to run a query like this:
SELECT *
FROM table
WHERE colA = paramA
AND colB > paramB
//condition C shouldn't be considered, since a null value would have
//produce the wrong results
oracle plsql
Some procedures I'm working on accept a JSON string as a varchar2 as an input parameter, which contains parameters sent as key-value pairs from our web server that are intended to filter query results. Depending on the use case, certain parameters may not appear in the incoming JSON. I'm wondering if there's any way that I can use the parameter values in the JSON string as conditions in a query, even if certain parameters may not return a value? Something like this:
v_json VARCHAR2 := '"paramA":"valueA","paramB":"valueB"';
//the JSON can include values for either paramA, paramB, and paramC
SELECT *
FROM table
WHERE colA = paramA
AND colB > paramB
AND colC LIKE paramC;
Since the JSON has no paramC, I'd need to run a query like this:
SELECT *
FROM table
WHERE colA = paramA
AND colB > paramB
//condition C shouldn't be considered, since a null value would have
//produce the wrong results
oracle plsql
oracle plsql
asked Mar 28 at 19:42
jwasjwas
203 bronze badges
203 bronze badges
which Oracle version are you working on?
– Kaushik Nayak
Mar 29 at 3:19
add a comment
|
which Oracle version are you working on?
– Kaushik Nayak
Mar 29 at 3:19
which Oracle version are you working on?
– Kaushik Nayak
Mar 29 at 3:19
which Oracle version are you working on?
– Kaushik Nayak
Mar 29 at 3:19
add a comment
|
1 Answer
1
active
oldest
votes
In Oracle Version 12c or above, you can achieve this using JSON_EXISTS
conditional and JSON_VALUE
to get the value. Below is sample code based on your updated requirement.
declare
v_json VARCHAR2(4000) := '"paramA":"valueA","paramB":"valueB"';
l_cntA number;
l_cntB number;
l_cntC number;
l_cnt number;
v_sql varchar2(4000);
begin
select count(1) into l_cntA from dual where JSON_EXISTS(v_json,'$.paramA');
select count(1) into l_cntB from dual where JSON_EXISTS(v_json,'$.paramB');
select count(1) into l_cntC from dual where JSON_EXISTS(v_json,'$.paramC');
v_sql := '
SELECT count(1)
FROM table
WHERE '||
case when l_cntA > 0 then
'colA=JSON_VALUE('''||v_json||''',''$.paramA'')' end ||
case when (l_cntA > 0 and l_cntB > 0 ) then
' AND ' end ||
case when l_cntB > 0 then
'colB = JSON_VALUE('''||v_json||''',''$.paramB'')' end ||
case when ((l_cntA > 0 or l_cntB > 0 ) and l_cntC > 0 ) then
' AND ' end ||
case when l_cntC > 0 then
'colC LIKE ''%''||JSON_VALUE('''||v_json||''',''$.paramC'')||''%'''
end;
DBMS_OUTPUT.PUT_LINE('v_sql' || v_sql);
execute immediate v_sql into l_cnt;
DBMS_OUTPUT.PUT_LINE('v_name ' || l_cnt);
exception
when others then
DBMS_OUTPUT.PUT_LINE('No Data Found');
end;
For Oracle version older then 12/18c, you can download,install and use open source PL/JSON
package to achieve this.Click here to Download PL/JSON Hope this helps.
I almost felt like Deja vu !. wasn't the same question asked and answered already or was it deleted?
– Kaushik Nayak
Mar 29 at 6:18
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55405704%2fcan-i-use-a-json-string-to-condition-the-results-of-a-query%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In Oracle Version 12c or above, you can achieve this using JSON_EXISTS
conditional and JSON_VALUE
to get the value. Below is sample code based on your updated requirement.
declare
v_json VARCHAR2(4000) := '"paramA":"valueA","paramB":"valueB"';
l_cntA number;
l_cntB number;
l_cntC number;
l_cnt number;
v_sql varchar2(4000);
begin
select count(1) into l_cntA from dual where JSON_EXISTS(v_json,'$.paramA');
select count(1) into l_cntB from dual where JSON_EXISTS(v_json,'$.paramB');
select count(1) into l_cntC from dual where JSON_EXISTS(v_json,'$.paramC');
v_sql := '
SELECT count(1)
FROM table
WHERE '||
case when l_cntA > 0 then
'colA=JSON_VALUE('''||v_json||''',''$.paramA'')' end ||
case when (l_cntA > 0 and l_cntB > 0 ) then
' AND ' end ||
case when l_cntB > 0 then
'colB = JSON_VALUE('''||v_json||''',''$.paramB'')' end ||
case when ((l_cntA > 0 or l_cntB > 0 ) and l_cntC > 0 ) then
' AND ' end ||
case when l_cntC > 0 then
'colC LIKE ''%''||JSON_VALUE('''||v_json||''',''$.paramC'')||''%'''
end;
DBMS_OUTPUT.PUT_LINE('v_sql' || v_sql);
execute immediate v_sql into l_cnt;
DBMS_OUTPUT.PUT_LINE('v_name ' || l_cnt);
exception
when others then
DBMS_OUTPUT.PUT_LINE('No Data Found');
end;
For Oracle version older then 12/18c, you can download,install and use open source PL/JSON
package to achieve this.Click here to Download PL/JSON Hope this helps.
I almost felt like Deja vu !. wasn't the same question asked and answered already or was it deleted?
– Kaushik Nayak
Mar 29 at 6:18
add a comment
|
In Oracle Version 12c or above, you can achieve this using JSON_EXISTS
conditional and JSON_VALUE
to get the value. Below is sample code based on your updated requirement.
declare
v_json VARCHAR2(4000) := '"paramA":"valueA","paramB":"valueB"';
l_cntA number;
l_cntB number;
l_cntC number;
l_cnt number;
v_sql varchar2(4000);
begin
select count(1) into l_cntA from dual where JSON_EXISTS(v_json,'$.paramA');
select count(1) into l_cntB from dual where JSON_EXISTS(v_json,'$.paramB');
select count(1) into l_cntC from dual where JSON_EXISTS(v_json,'$.paramC');
v_sql := '
SELECT count(1)
FROM table
WHERE '||
case when l_cntA > 0 then
'colA=JSON_VALUE('''||v_json||''',''$.paramA'')' end ||
case when (l_cntA > 0 and l_cntB > 0 ) then
' AND ' end ||
case when l_cntB > 0 then
'colB = JSON_VALUE('''||v_json||''',''$.paramB'')' end ||
case when ((l_cntA > 0 or l_cntB > 0 ) and l_cntC > 0 ) then
' AND ' end ||
case when l_cntC > 0 then
'colC LIKE ''%''||JSON_VALUE('''||v_json||''',''$.paramC'')||''%'''
end;
DBMS_OUTPUT.PUT_LINE('v_sql' || v_sql);
execute immediate v_sql into l_cnt;
DBMS_OUTPUT.PUT_LINE('v_name ' || l_cnt);
exception
when others then
DBMS_OUTPUT.PUT_LINE('No Data Found');
end;
For Oracle version older then 12/18c, you can download,install and use open source PL/JSON
package to achieve this.Click here to Download PL/JSON Hope this helps.
I almost felt like Deja vu !. wasn't the same question asked and answered already or was it deleted?
– Kaushik Nayak
Mar 29 at 6:18
add a comment
|
In Oracle Version 12c or above, you can achieve this using JSON_EXISTS
conditional and JSON_VALUE
to get the value. Below is sample code based on your updated requirement.
declare
v_json VARCHAR2(4000) := '"paramA":"valueA","paramB":"valueB"';
l_cntA number;
l_cntB number;
l_cntC number;
l_cnt number;
v_sql varchar2(4000);
begin
select count(1) into l_cntA from dual where JSON_EXISTS(v_json,'$.paramA');
select count(1) into l_cntB from dual where JSON_EXISTS(v_json,'$.paramB');
select count(1) into l_cntC from dual where JSON_EXISTS(v_json,'$.paramC');
v_sql := '
SELECT count(1)
FROM table
WHERE '||
case when l_cntA > 0 then
'colA=JSON_VALUE('''||v_json||''',''$.paramA'')' end ||
case when (l_cntA > 0 and l_cntB > 0 ) then
' AND ' end ||
case when l_cntB > 0 then
'colB = JSON_VALUE('''||v_json||''',''$.paramB'')' end ||
case when ((l_cntA > 0 or l_cntB > 0 ) and l_cntC > 0 ) then
' AND ' end ||
case when l_cntC > 0 then
'colC LIKE ''%''||JSON_VALUE('''||v_json||''',''$.paramC'')||''%'''
end;
DBMS_OUTPUT.PUT_LINE('v_sql' || v_sql);
execute immediate v_sql into l_cnt;
DBMS_OUTPUT.PUT_LINE('v_name ' || l_cnt);
exception
when others then
DBMS_OUTPUT.PUT_LINE('No Data Found');
end;
For Oracle version older then 12/18c, you can download,install and use open source PL/JSON
package to achieve this.Click here to Download PL/JSON Hope this helps.
In Oracle Version 12c or above, you can achieve this using JSON_EXISTS
conditional and JSON_VALUE
to get the value. Below is sample code based on your updated requirement.
declare
v_json VARCHAR2(4000) := '"paramA":"valueA","paramB":"valueB"';
l_cntA number;
l_cntB number;
l_cntC number;
l_cnt number;
v_sql varchar2(4000);
begin
select count(1) into l_cntA from dual where JSON_EXISTS(v_json,'$.paramA');
select count(1) into l_cntB from dual where JSON_EXISTS(v_json,'$.paramB');
select count(1) into l_cntC from dual where JSON_EXISTS(v_json,'$.paramC');
v_sql := '
SELECT count(1)
FROM table
WHERE '||
case when l_cntA > 0 then
'colA=JSON_VALUE('''||v_json||''',''$.paramA'')' end ||
case when (l_cntA > 0 and l_cntB > 0 ) then
' AND ' end ||
case when l_cntB > 0 then
'colB = JSON_VALUE('''||v_json||''',''$.paramB'')' end ||
case when ((l_cntA > 0 or l_cntB > 0 ) and l_cntC > 0 ) then
' AND ' end ||
case when l_cntC > 0 then
'colC LIKE ''%''||JSON_VALUE('''||v_json||''',''$.paramC'')||''%'''
end;
DBMS_OUTPUT.PUT_LINE('v_sql' || v_sql);
execute immediate v_sql into l_cnt;
DBMS_OUTPUT.PUT_LINE('v_name ' || l_cnt);
exception
when others then
DBMS_OUTPUT.PUT_LINE('No Data Found');
end;
For Oracle version older then 12/18c, you can download,install and use open source PL/JSON
package to achieve this.Click here to Download PL/JSON Hope this helps.
answered Mar 29 at 5:31
AhamedAhamed
2791 silver badge8 bronze badges
2791 silver badge8 bronze badges
I almost felt like Deja vu !. wasn't the same question asked and answered already or was it deleted?
– Kaushik Nayak
Mar 29 at 6:18
add a comment
|
I almost felt like Deja vu !. wasn't the same question asked and answered already or was it deleted?
– Kaushik Nayak
Mar 29 at 6:18
I almost felt like Deja vu !. wasn't the same question asked and answered already or was it deleted?
– Kaushik Nayak
Mar 29 at 6:18
I almost felt like Deja vu !. wasn't the same question asked and answered already or was it deleted?
– Kaushik Nayak
Mar 29 at 6:18
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55405704%2fcan-i-use-a-json-string-to-condition-the-results-of-a-query%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
which Oracle version are you working on?
– Kaushik Nayak
Mar 29 at 3:19