0

Write a function named get_total_records, to pass the table name as a parameter, and get back the number of records that are contained in the table.

Please test your function with multiple tables.

1 Answer 1

2

Which database is it? If Oracle, here's one option:

SQL> CREATE OR REPLACE FUNCTION get_total_records (par_table_name IN VARCHAR2)
  2     RETURN NUMBER
  3  IS
  4     retval  NUMBER;
  5  BEGIN
  6     EXECUTE IMMEDIATE
  7        'select count(*) from ' || DBMS_ASSERT.sql_object_name (par_table_name)
  8        INTO retval;
  9
 10     RETURN retval;
 11  END;
 12  /

Function created.

SQL> SELECT get_total_records ('emp') result FROM DUAL;

    RESULT
----------
        14

SQL> SELECT get_total_records ('dept') result FROM DUAL;

    RESULT
----------
         4

SQL> SELECT get_total_records ('does_not_exist') result FROM DUAL;
SELECT get_total_records ('does_not_exist') result FROM DUAL
       *
ERROR at line 1:
ORA-44002: invalid object name
ORA-06512: at "SYS.DBMS_ASSERT", line 383
ORA-06512: at "SCOTT.GET_TOTAL_RECORDS", line 6


SQL>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.