Using the PL/SQL Function body returning SQL query












-1















I am working with Oracle APEX and I'm trying to store a dynamic sql query in the PL/SQL Function body. Here is my current SQL query for a classic report:



SELECT 
RELEASE,
COUNT(*) as "Total Tests", --total
SUM(CASE WHEN TYPE = 'P1' THEN 1 ELSE 0 END) as "P1",
SUM(CASE WHEN TYPE = 'P2' THEN 1 ELSE 0 END) as "P2",
SUM(CASE WHEN TYPE = 'P3' THEN 1 ELSE 0 END) as "P3",
SUM(CASE WHEN TYPE = 'P4' THEN 1 ELSE 0 END) as "P4"
FROM TABLENAME
group by RELEASE
ORDER BY case
when RELEASE = '19.3' then 1
when RELEASE = '18.11' then 2
when RELEASE = '18.9' then 3
when RELEASE = '18.7' then 4
when RELEASE = '17.3' then 5
else 6
end asc


I am trying to store the 'tablename' in a page item so that whenever a user changes the page item, this query will automatically pull from the given table. I've attempted to build a pl/sql function around this but continue to run into errors with the 'strings'.



Does anyone know how to convert this in to a dynamic pl/sql function?



Thanks in advance.










share|improve this question

























  • what is the error mesage?

    – Eray Balkanli
    Nov 15 '18 at 16:16











  • ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! <p>ORA-06550: line 24, column 8: ORA-00904: "18.7": invalid identifier</p>".

    – aphibui
    Nov 15 '18 at 16:44











  • Can you present your code for the PL/SQL Function?

    – Stilgar
    Nov 15 '18 at 20:34
















-1















I am working with Oracle APEX and I'm trying to store a dynamic sql query in the PL/SQL Function body. Here is my current SQL query for a classic report:



SELECT 
RELEASE,
COUNT(*) as "Total Tests", --total
SUM(CASE WHEN TYPE = 'P1' THEN 1 ELSE 0 END) as "P1",
SUM(CASE WHEN TYPE = 'P2' THEN 1 ELSE 0 END) as "P2",
SUM(CASE WHEN TYPE = 'P3' THEN 1 ELSE 0 END) as "P3",
SUM(CASE WHEN TYPE = 'P4' THEN 1 ELSE 0 END) as "P4"
FROM TABLENAME
group by RELEASE
ORDER BY case
when RELEASE = '19.3' then 1
when RELEASE = '18.11' then 2
when RELEASE = '18.9' then 3
when RELEASE = '18.7' then 4
when RELEASE = '17.3' then 5
else 6
end asc


I am trying to store the 'tablename' in a page item so that whenever a user changes the page item, this query will automatically pull from the given table. I've attempted to build a pl/sql function around this but continue to run into errors with the 'strings'.



Does anyone know how to convert this in to a dynamic pl/sql function?



Thanks in advance.










share|improve this question

























  • what is the error mesage?

    – Eray Balkanli
    Nov 15 '18 at 16:16











  • ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! <p>ORA-06550: line 24, column 8: ORA-00904: "18.7": invalid identifier</p>".

    – aphibui
    Nov 15 '18 at 16:44











  • Can you present your code for the PL/SQL Function?

    – Stilgar
    Nov 15 '18 at 20:34














-1












-1








-1








I am working with Oracle APEX and I'm trying to store a dynamic sql query in the PL/SQL Function body. Here is my current SQL query for a classic report:



SELECT 
RELEASE,
COUNT(*) as "Total Tests", --total
SUM(CASE WHEN TYPE = 'P1' THEN 1 ELSE 0 END) as "P1",
SUM(CASE WHEN TYPE = 'P2' THEN 1 ELSE 0 END) as "P2",
SUM(CASE WHEN TYPE = 'P3' THEN 1 ELSE 0 END) as "P3",
SUM(CASE WHEN TYPE = 'P4' THEN 1 ELSE 0 END) as "P4"
FROM TABLENAME
group by RELEASE
ORDER BY case
when RELEASE = '19.3' then 1
when RELEASE = '18.11' then 2
when RELEASE = '18.9' then 3
when RELEASE = '18.7' then 4
when RELEASE = '17.3' then 5
else 6
end asc


I am trying to store the 'tablename' in a page item so that whenever a user changes the page item, this query will automatically pull from the given table. I've attempted to build a pl/sql function around this but continue to run into errors with the 'strings'.



Does anyone know how to convert this in to a dynamic pl/sql function?



Thanks in advance.










share|improve this question
















I am working with Oracle APEX and I'm trying to store a dynamic sql query in the PL/SQL Function body. Here is my current SQL query for a classic report:



SELECT 
RELEASE,
COUNT(*) as "Total Tests", --total
SUM(CASE WHEN TYPE = 'P1' THEN 1 ELSE 0 END) as "P1",
SUM(CASE WHEN TYPE = 'P2' THEN 1 ELSE 0 END) as "P2",
SUM(CASE WHEN TYPE = 'P3' THEN 1 ELSE 0 END) as "P3",
SUM(CASE WHEN TYPE = 'P4' THEN 1 ELSE 0 END) as "P4"
FROM TABLENAME
group by RELEASE
ORDER BY case
when RELEASE = '19.3' then 1
when RELEASE = '18.11' then 2
when RELEASE = '18.9' then 3
when RELEASE = '18.7' then 4
when RELEASE = '17.3' then 5
else 6
end asc


I am trying to store the 'tablename' in a page item so that whenever a user changes the page item, this query will automatically pull from the given table. I've attempted to build a pl/sql function around this but continue to run into errors with the 'strings'.



Does anyone know how to convert this in to a dynamic pl/sql function?



Thanks in advance.







sql oracle plsql oracle-apex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 20:47









Littlefoot

24.3k71634




24.3k71634










asked Nov 15 '18 at 16:14









aphibuiaphibui

124




124













  • what is the error mesage?

    – Eray Balkanli
    Nov 15 '18 at 16:16











  • ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! <p>ORA-06550: line 24, column 8: ORA-00904: "18.7": invalid identifier</p>".

    – aphibui
    Nov 15 '18 at 16:44











  • Can you present your code for the PL/SQL Function?

    – Stilgar
    Nov 15 '18 at 20:34



















  • what is the error mesage?

    – Eray Balkanli
    Nov 15 '18 at 16:16











  • ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! <p>ORA-06550: line 24, column 8: ORA-00904: "18.7": invalid identifier</p>".

    – aphibui
    Nov 15 '18 at 16:44











  • Can you present your code for the PL/SQL Function?

    – Stilgar
    Nov 15 '18 at 20:34

















what is the error mesage?

– Eray Balkanli
Nov 15 '18 at 16:16





what is the error mesage?

– Eray Balkanli
Nov 15 '18 at 16:16













ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! <p>ORA-06550: line 24, column 8: ORA-00904: "18.7": invalid identifier</p>".

– aphibui
Nov 15 '18 at 16:44





ORA-20999: Parsing returned query results in "ORA-20999: Failed to parse SQL query! <p>ORA-06550: line 24, column 8: ORA-00904: "18.7": invalid identifier</p>".

– aphibui
Nov 15 '18 at 16:44













Can you present your code for the PL/SQL Function?

– Stilgar
Nov 15 '18 at 20:34





Can you present your code for the PL/SQL Function?

– Stilgar
Nov 15 '18 at 20:34












1 Answer
1






active

oldest

votes


















2














You should




  • create a classic report whose source is a function that returns a query


  • that query should look like this:



    return '
    SELECT
    RELEASE,
    COUNT(*) as "Total Tests", --total
    SUM(CASE WHEN TYPE = ''P1'' THEN 1 ELSE 0 END) as "P1",
    SUM(CASE WHEN TYPE = ''P2'' THEN 1 ELSE 0 END) as "P2",
    SUM(CASE WHEN TYPE = ''P3'' THEN 1 ELSE 0 END) as "P3",
    SUM(CASE WHEN TYPE = ''P4'' THEN 1 ELSE 0 END) as "P4"
    FROM ' ||
    SYS.DBMS_ASSERT.sql_object_name(:P1_TABLE_NAME) ||
    ' group by RELEASE
    ORDER BY case
    when RELEASE = ''19.3'' then 1
    when RELEASE = ''18.11'' then 2
    when RELEASE = ''18.9'' then 3
    when RELEASE = ''18.7'' then 4
    when RELEASE = ''17.3'' then 5
    else 6
    end asc';


  • DBMS_ASSERT is here to prevent SQL Injection


  • set "use generic column names" to "Yes"

  • page item's name is :P1_TABLE_NAME (in my example); it should be a text field, submit when press Enter


That's all, I presume.






share|improve this answer
























  • Thank you, this works! Thanks for the DMBS_ASSERT tip as well. :)

    – aphibui
    Nov 15 '18 at 23:29











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/3.0/"u003ecc by-sa 3.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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323611%2fusing-the-pl-sql-function-body-returning-sql-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









2














You should




  • create a classic report whose source is a function that returns a query


  • that query should look like this:



    return '
    SELECT
    RELEASE,
    COUNT(*) as "Total Tests", --total
    SUM(CASE WHEN TYPE = ''P1'' THEN 1 ELSE 0 END) as "P1",
    SUM(CASE WHEN TYPE = ''P2'' THEN 1 ELSE 0 END) as "P2",
    SUM(CASE WHEN TYPE = ''P3'' THEN 1 ELSE 0 END) as "P3",
    SUM(CASE WHEN TYPE = ''P4'' THEN 1 ELSE 0 END) as "P4"
    FROM ' ||
    SYS.DBMS_ASSERT.sql_object_name(:P1_TABLE_NAME) ||
    ' group by RELEASE
    ORDER BY case
    when RELEASE = ''19.3'' then 1
    when RELEASE = ''18.11'' then 2
    when RELEASE = ''18.9'' then 3
    when RELEASE = ''18.7'' then 4
    when RELEASE = ''17.3'' then 5
    else 6
    end asc';


  • DBMS_ASSERT is here to prevent SQL Injection


  • set "use generic column names" to "Yes"

  • page item's name is :P1_TABLE_NAME (in my example); it should be a text field, submit when press Enter


That's all, I presume.






share|improve this answer
























  • Thank you, this works! Thanks for the DMBS_ASSERT tip as well. :)

    – aphibui
    Nov 15 '18 at 23:29
















2














You should




  • create a classic report whose source is a function that returns a query


  • that query should look like this:



    return '
    SELECT
    RELEASE,
    COUNT(*) as "Total Tests", --total
    SUM(CASE WHEN TYPE = ''P1'' THEN 1 ELSE 0 END) as "P1",
    SUM(CASE WHEN TYPE = ''P2'' THEN 1 ELSE 0 END) as "P2",
    SUM(CASE WHEN TYPE = ''P3'' THEN 1 ELSE 0 END) as "P3",
    SUM(CASE WHEN TYPE = ''P4'' THEN 1 ELSE 0 END) as "P4"
    FROM ' ||
    SYS.DBMS_ASSERT.sql_object_name(:P1_TABLE_NAME) ||
    ' group by RELEASE
    ORDER BY case
    when RELEASE = ''19.3'' then 1
    when RELEASE = ''18.11'' then 2
    when RELEASE = ''18.9'' then 3
    when RELEASE = ''18.7'' then 4
    when RELEASE = ''17.3'' then 5
    else 6
    end asc';


  • DBMS_ASSERT is here to prevent SQL Injection


  • set "use generic column names" to "Yes"

  • page item's name is :P1_TABLE_NAME (in my example); it should be a text field, submit when press Enter


That's all, I presume.






share|improve this answer
























  • Thank you, this works! Thanks for the DMBS_ASSERT tip as well. :)

    – aphibui
    Nov 15 '18 at 23:29














2












2








2







You should




  • create a classic report whose source is a function that returns a query


  • that query should look like this:



    return '
    SELECT
    RELEASE,
    COUNT(*) as "Total Tests", --total
    SUM(CASE WHEN TYPE = ''P1'' THEN 1 ELSE 0 END) as "P1",
    SUM(CASE WHEN TYPE = ''P2'' THEN 1 ELSE 0 END) as "P2",
    SUM(CASE WHEN TYPE = ''P3'' THEN 1 ELSE 0 END) as "P3",
    SUM(CASE WHEN TYPE = ''P4'' THEN 1 ELSE 0 END) as "P4"
    FROM ' ||
    SYS.DBMS_ASSERT.sql_object_name(:P1_TABLE_NAME) ||
    ' group by RELEASE
    ORDER BY case
    when RELEASE = ''19.3'' then 1
    when RELEASE = ''18.11'' then 2
    when RELEASE = ''18.9'' then 3
    when RELEASE = ''18.7'' then 4
    when RELEASE = ''17.3'' then 5
    else 6
    end asc';


  • DBMS_ASSERT is here to prevent SQL Injection


  • set "use generic column names" to "Yes"

  • page item's name is :P1_TABLE_NAME (in my example); it should be a text field, submit when press Enter


That's all, I presume.






share|improve this answer













You should




  • create a classic report whose source is a function that returns a query


  • that query should look like this:



    return '
    SELECT
    RELEASE,
    COUNT(*) as "Total Tests", --total
    SUM(CASE WHEN TYPE = ''P1'' THEN 1 ELSE 0 END) as "P1",
    SUM(CASE WHEN TYPE = ''P2'' THEN 1 ELSE 0 END) as "P2",
    SUM(CASE WHEN TYPE = ''P3'' THEN 1 ELSE 0 END) as "P3",
    SUM(CASE WHEN TYPE = ''P4'' THEN 1 ELSE 0 END) as "P4"
    FROM ' ||
    SYS.DBMS_ASSERT.sql_object_name(:P1_TABLE_NAME) ||
    ' group by RELEASE
    ORDER BY case
    when RELEASE = ''19.3'' then 1
    when RELEASE = ''18.11'' then 2
    when RELEASE = ''18.9'' then 3
    when RELEASE = ''18.7'' then 4
    when RELEASE = ''17.3'' then 5
    else 6
    end asc';


  • DBMS_ASSERT is here to prevent SQL Injection


  • set "use generic column names" to "Yes"

  • page item's name is :P1_TABLE_NAME (in my example); it should be a text field, submit when press Enter


That's all, I presume.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 20:47









LittlefootLittlefoot

24.3k71634




24.3k71634













  • Thank you, this works! Thanks for the DMBS_ASSERT tip as well. :)

    – aphibui
    Nov 15 '18 at 23:29



















  • Thank you, this works! Thanks for the DMBS_ASSERT tip as well. :)

    – aphibui
    Nov 15 '18 at 23:29

















Thank you, this works! Thanks for the DMBS_ASSERT tip as well. :)

– aphibui
Nov 15 '18 at 23:29





Thank you, this works! Thanks for the DMBS_ASSERT tip as well. :)

– aphibui
Nov 15 '18 at 23:29




















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323611%2fusing-the-pl-sql-function-body-returning-sql-query%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Bressuire

Vorschmack

Quarantine