passing in table name as plsql parameter












2














I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:



create or replace function get_table_count (table_name IN varchar2)
return number
is
tbl_nm varchar(100) := table_name;
table_count number;
begin
select count(*)
into table_count
from tbl_nm;
dbms_output.put_line(table_count);
return table_count;
end;


I get this error:



FUNCTION GET_TABLE_COUNT compiled
Errors: check compiler log
Error(7,5): PL/SQL: SQL Statement ignored
Error(9,8): PL/SQL: ORA-00942: table or view does not exist


I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.










share|improve this question





























    2














    I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:



    create or replace function get_table_count (table_name IN varchar2)
    return number
    is
    tbl_nm varchar(100) := table_name;
    table_count number;
    begin
    select count(*)
    into table_count
    from tbl_nm;
    dbms_output.put_line(table_count);
    return table_count;
    end;


    I get this error:



    FUNCTION GET_TABLE_COUNT compiled
    Errors: check compiler log
    Error(7,5): PL/SQL: SQL Statement ignored
    Error(9,8): PL/SQL: ORA-00942: table or view does not exist


    I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.










    share|improve this question



























      2












      2








      2







      I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:



      create or replace function get_table_count (table_name IN varchar2)
      return number
      is
      tbl_nm varchar(100) := table_name;
      table_count number;
      begin
      select count(*)
      into table_count
      from tbl_nm;
      dbms_output.put_line(table_count);
      return table_count;
      end;


      I get this error:



      FUNCTION GET_TABLE_COUNT compiled
      Errors: check compiler log
      Error(7,5): PL/SQL: SQL Statement ignored
      Error(9,8): PL/SQL: ORA-00942: table or view does not exist


      I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.










      share|improve this question















      I want to write a function to return the row count of a table whose name is passed in as a variable. Here's my code:



      create or replace function get_table_count (table_name IN varchar2)
      return number
      is
      tbl_nm varchar(100) := table_name;
      table_count number;
      begin
      select count(*)
      into table_count
      from tbl_nm;
      dbms_output.put_line(table_count);
      return table_count;
      end;


      I get this error:



      FUNCTION GET_TABLE_COUNT compiled
      Errors: check compiler log
      Error(7,5): PL/SQL: SQL Statement ignored
      Error(9,8): PL/SQL: ORA-00942: table or view does not exist


      I understand that tbl_nm is being interpreted as a value and not a reference and I'm not sure how to escape that.







      plsql oracle11g






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 22 '14 at 17:07









      Vadim K.

      2,2881726




      2,2881726










      asked Dec 21 '14 at 18:22









      Serge

      328918




      328918
























          1 Answer
          1






          active

          oldest

          votes


















          8














          You can use dynamic SQL:



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          execute immediate 'select count(*) from ' || table_name into table_count;
          dbms_output.put_line(table_count);
          return table_count;
          end;


          There is also an indirect way to get number of rows (using system views):



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          select num_rows
          into table_count
          from user_tables
          where table_name = table_name;

          return table_count;
          end;


          The second way works only if you had gathered statistics on table before invoking this function.






          share|improve this answer

















          • 3




            I would add, the second option as the major advantage the you don't have to validate the table_name (remember Little Bobby Tables...)
            – Sylvain Leroux
            Dec 21 '14 at 19:15












          • @SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
            – APC
            Apr 10 at 7:25











          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%2f27592366%2fpassing-in-table-name-as-plsql-parameter%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









          8














          You can use dynamic SQL:



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          execute immediate 'select count(*) from ' || table_name into table_count;
          dbms_output.put_line(table_count);
          return table_count;
          end;


          There is also an indirect way to get number of rows (using system views):



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          select num_rows
          into table_count
          from user_tables
          where table_name = table_name;

          return table_count;
          end;


          The second way works only if you had gathered statistics on table before invoking this function.






          share|improve this answer

















          • 3




            I would add, the second option as the major advantage the you don't have to validate the table_name (remember Little Bobby Tables...)
            – Sylvain Leroux
            Dec 21 '14 at 19:15












          • @SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
            – APC
            Apr 10 at 7:25
















          8














          You can use dynamic SQL:



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          execute immediate 'select count(*) from ' || table_name into table_count;
          dbms_output.put_line(table_count);
          return table_count;
          end;


          There is also an indirect way to get number of rows (using system views):



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          select num_rows
          into table_count
          from user_tables
          where table_name = table_name;

          return table_count;
          end;


          The second way works only if you had gathered statistics on table before invoking this function.






          share|improve this answer

















          • 3




            I would add, the second option as the major advantage the you don't have to validate the table_name (remember Little Bobby Tables...)
            – Sylvain Leroux
            Dec 21 '14 at 19:15












          • @SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
            – APC
            Apr 10 at 7:25














          8












          8








          8






          You can use dynamic SQL:



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          execute immediate 'select count(*) from ' || table_name into table_count;
          dbms_output.put_line(table_count);
          return table_count;
          end;


          There is also an indirect way to get number of rows (using system views):



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          select num_rows
          into table_count
          from user_tables
          where table_name = table_name;

          return table_count;
          end;


          The second way works only if you had gathered statistics on table before invoking this function.






          share|improve this answer












          You can use dynamic SQL:



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          execute immediate 'select count(*) from ' || table_name into table_count;
          dbms_output.put_line(table_count);
          return table_count;
          end;


          There is also an indirect way to get number of rows (using system views):



          create or replace function get_table_count (table_name IN varchar2)
          return number
          is
          table_count number;
          begin
          select num_rows
          into table_count
          from user_tables
          where table_name = table_name;

          return table_count;
          end;


          The second way works only if you had gathered statistics on table before invoking this function.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 21 '14 at 19:01









          Dmitriy

          4,836111833




          4,836111833








          • 3




            I would add, the second option as the major advantage the you don't have to validate the table_name (remember Little Bobby Tables...)
            – Sylvain Leroux
            Dec 21 '14 at 19:15












          • @SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
            – APC
            Apr 10 at 7:25














          • 3




            I would add, the second option as the major advantage the you don't have to validate the table_name (remember Little Bobby Tables...)
            – Sylvain Leroux
            Dec 21 '14 at 19:15












          • @SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
            – APC
            Apr 10 at 7:25








          3




          3




          I would add, the second option as the major advantage the you don't have to validate the table_name (remember Little Bobby Tables...)
          – Sylvain Leroux
          Dec 21 '14 at 19:15






          I would add, the second option as the major advantage the you don't have to validate the table_name (remember Little Bobby Tables...)
          – Sylvain Leroux
          Dec 21 '14 at 19:15














          @SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
          – APC
          Apr 10 at 7:25




          @SylvainLeroux - Oracle provides the DBMS_ASSERT package so we can easily handle SQL injection attacks. The problem with the second approach is that its dependence on statistics means the returned value may be inaccurate or misleading, unless we gather stats on the table immediately before running the count which is (a) time-consuming (b) potentially destabilising to ongoing performance.
          – APC
          Apr 10 at 7:25


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f27592366%2fpassing-in-table-name-as-plsql-parameter%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

          Lallio

          Unable to find Lightning Node

          Futebolista