How to pass Table name as argument in Stored Proc

Hi ,
I have a requirement wherein the table name has to be passed as an argument of SP and the output should be the count from that table based on some filter condition.
Something like this:
create or replace procedure Testing(p_table varchar2,p_count OUT integer) AS
begin
select count(1) into p_count from p_table;
end;
Getting the error on running above code:
PL/SQL: ORA-00942: table or view does not exist
Please let me know how can we do this?
Thanks.

Example :SQL> create or replace procedure Testing(
  2     p_table varchar2,
  3     p_count OUT integer) AS
  4  begin
  5     execute immediate 'select count(1) from '||p_table
  6             into p_count;
  7  end;
  8  /
Procedure created.
SQL> var n1 number;
SQL> exec Testing('user_objects',:n1);
PL/SQL procedure successfully completed.
SQL> print n1
        N1
        37
SQL>

Similar Messages

  • How to pass table name as argument to cursor

    please help me how to solve this.
    SQL> select c1 from test ;
    C1
    1
    7
    18
    4
    10
    28
    30
    I'm able to execute this.
    SQL> get b
    1 set serveroutput on size 2000
    2 declare
    3 cursor c1 is
    4 select c1 from test ;
    5 begin
    6 for rec in c1
    7 loop
    8 dbms_output.put_line(rec.c1);
    9 end loop;
    10* end;
    SQL> sta b
    1
    7
    18
    4
    10
    28
    30
    When I change the above cursor to accept the parameter it is not working.
    SQL> get c
    1 set serveroutput on size 2000
    2 declare
    3 cursor c1(p_table varchar2) is
    4 select c1 from p_table ;
    5 begin
    6 for rec in c1('test')
    7 loop
    8 dbms_output.put_line(rec.c1);
    9 end loop;
    10* end;
    ERROR at line 3:
    ORA-06550: line 3, column 18:
    PL/SQL: ORA-00942: table or view does not exist
    ORA-06550: line 3, column 3:
    PL/SQL: SQL Statement ignored
    ORA-06550: line 7, column 25:
    PLS-00364: loop index variable 'REC' use is invalid
    ORA-06550: line 7, column 4:
    PL/SQL: Statement ignored

    You cannot use a (bind) variable in SQL for an object name (i.e. a table name, column name, etc).
    Why?
    The SQL engine needs to parse the SQL. Part of the parse is to resolve the scope to determine what objects are being reference. It needs to check that the column names referred to is indeed in that object. Etc.
    Then these objects are used to determine the best execution path to run the SQL. What indexes do the objects have? Is the object a partitioned table and the partition criteria used in the predicate? Etc.
    How can the SQL engine perform any of these when you pass it a table name that is a parameter?
    The name of an object (such as the table being selected from) must be explicitly stated in a SQL statement. It cannot be variable.

  • How to pass XMLType as parameters to Java stored procs ?

    How to pass XMLType as parameters to Java stored procs ?
    ORA-00932: inconsistent datatypes: expected an IN argument at position 1 that is an instance of an Oracle type convertible to an instance of a user defined Java class got an Oracle type that could not be converted to a java class
    Java stored proc -->
    CREATE or replace FUNCTION testJavaStoredProcMerge( entity xmltype,event xmltype ) RETURN VARCHAR2 AS LANGUAGE JAVA
    NAME 'XDBMergeOp.merge(org.w3c.dom.Document,org.w3c.dom.Document) return java.lang.String';
    PL/SQL -->
    declare
    theQuote VARCHAR2(50);
    entity xmltype;
    event xmltype;
    begin
    entity := xmltype('<Quote><Fields><Field1>f1</Field1></Fields></Quote>');
    event := xmltype('<Quote><Fields><Field2>f2</Field2></Fields></Quote>');
    theQuote := testJavaStoredProcMerge(entity,event);
    dbms_output.put_line(theQuote);
    end;
    Java class -->
    public class XDBMergeOp {
    public static String merge(Document entity, Document event) throws Exception {
    return ...
    Thanks in advance.

    I think you'll need to use XMLType and then extract the DOM inside java..
    create or replace package SAXLOADER
    as
      procedure LOAD(P_PARAMETERS XMLTYPE, P_DATASOURCE BFILE);
    end;
    create or replace package body SAXLOADER
    as
    procedure LOAD(P_PARAMETERS XMLTYPE, P_DATASOURCE BFILE)
    AS
    LANGUAGE JAVA
    NAME 'com.oracle.st.xmldb.pm.saxLoader.SaxProcessor.saxLoader ( oracle.xdb.XMLType, oracle.sql.BFILE)';
    end;
      public static void saxLoader(XMLType parameterSettings, BFILE dataSource)
      throws Exception {
        Document parameters = parameterSettings.getDocument();
        SaxProcessor app = new SaxProcessor(parameters);
        app.processXMLFile(dataSource);
      Edited by: mdrake on Apr 6, 2009 11:28 AM

  • How to pass a refcursor to a java stored proc

    Hi all,
    Please forgive me as I am new to Java and JDeveloper....
    I want to pass a refcursor to a java stored proc. Does anyone know how to accomplish this?
    Thanks,
    dayneo

    Hi,
    As Avi has indicated, you can map ref cursor to java.sql.ResultSet
    here are Call Specs and a code snippet from chapter 3 in my book.
    procedure rcproc(rc OUT EmpCurTyp)
    as language java
    name 'refcur.refcurproc(java.sql.ResultSet[])';
    function rcfunc return EmpCurTyp
    as language java
    name 'refcur.refcurfunc() returns java.sql.ResultSet';
    * Function returning a REF CURSOR
    public static ResultSet refcurfunc () throws SQLException
    Connection conn = null;
    conn = DriverManager.getConnection("jdbc:oracle:kprb:");
    ((OracleConnection)conn).setCreateStatementAsRefCursor(true);
    Statement stmt = conn.createStatement();
    ((OracleStatement)stmt).setRowPrefetch(1);
    ResultSet rset = stmt.executeQuery("select * from EMP order by empno");
    // fetch one row
    if (rset.next())
    System.out.println("Ename = " + rset.getString(2));
    return rset;
    Kuassi

  • How to pass default value of argument to stored function?

    Hi,
    in procedure I don't have to pass all arguments, because I set it with name, but in stored function I have to set arguments with index som I must set all arguments. Some arguments have default value. So how to pass default value?
    thx for any comments

    It really helps me if someone tell me that it isn't possiblle.
    thx.

  • How to pass Table name as parameter

    For example, you have several tables (TableA, TableB, TableC...TableN) that have the same structure.
    Ex.
    CREATE TABLE TableA(
    id VARCHAR(5),
    name VARCHAR(20)
    CREATE TABLE TableB(
    id VARCHAR(5),
    name VARCHAR(20)
    And you want to create a stored procedure in Oracle that can be used for all of the tables (TableA, TableB, ...)
    Ex. SELECT * FROM <tablename>
    WHERE ID > 1;
    How do you write the prepareCall and Callable Statement for that?
    Thanks in advance.

    You can't, not directly.
    You have two choices:
    -Write the SQL in java, then you can use string concatenation.
    -Use 'dynamic sql' in Oracle. There is a standard package that will take dynamic sql and run it.

  • Pass table name to procedure

    hi
    Can any one plz suggest me , how to pass
    table name (that would be varchar) to a procedure,such that i
    should be able to do DML and DDL on the table refered !!

    EXECUTE IMMEDIATE executes the SQL statement held in a specified
    VARCHAR2. So, with a bit of string concatenation, you can make
    the statement dynamic, thus:
    EXECUTE IMMEDIATE 'DELETE * FROM emp' ;
    could become:
    EXECUTE IMMEDIATE 'DELETE * FROM '||p_table_name ;
    where p_table_name is a VARCHAR2 parameter passed into your
    procedure. Check out the PL/SQL User's Guide and Reference,
    chapter 10, for full details.

  • Pass table name as parameter in prepared Statement

    Can I pass table name as parameter in prepared Statement
    for example
    select * from ? where name =?
    when i use setString method for passing parameters this method append single colon before and after of this parameter but table name should be send with out colon as SQL Spec.
    I have another way to make sql query in programing but i have a case where i have limitation of that thing so please tell me is it possible with prepared Statment SetXXx methods or not ?
    Thanks
    Haroon Idrees.

    haroonob wrote:
    I know ? is use for data only my question is this way to pass table name as parameterI assume you mean "how can I do it?" As I have already answered "is this the way?" with no.
    Well, I would say (ugly as it is) String concatenation, or stored procedures.

  • How to pass column name as a   values from one page  to another

    hi
    i have created a report(pivot) from a table
    SQL> SELECT * FROM T;
    C1  C2          C3 D                SEQ
    A   AA           2                    1
    A   AB           3                    2
    A   AC           2                    3
    B   AB           5                    4
    B   AC           6                    5
    SQL> SELECT C1
      2  ,NVL(MAX(CASE WHEN C2='AA' THEN C3 END),'') AA
      3  ,NVL(MAX(CASE WHEN C2='AB' THEN C3 END),'') AB
      4  ,NVL(MAX(CASE WHEN C2='AC' THEN C3 END),'') AC
      5  ,SUM(C3) FROM T GROUP BY C1;
    C1          AA         AB         AC    SUM(C3)
    A            2          3          2          7
    B                       5          6         11
    SQL>
    my requirement in Apex is like this(reverse)
    eg-
    when i click on cell values '2' then,it should return
    C1  C2          C3 D                SEQ
    A   AA           2                    1
    {quote}how to pass column name as a  values from one page to another
    for example i have to pass 'c2' as a value to next page{quote}for report pivot you can reffer below link
    Report
    Amu

    thanks for your reply
    i 'm doing what exactly you mention here .
    my problem here is
    i have 15 columns
    i am executing a query based on the values of the column(column name)  in the target page
    1)here i am passing(all) the column values to the next page-but  i want to pass only one column values(column name)
    when i click on any cell of that  column
    OR
    2)i can pass all column name to target page -there(in the target page) i can filter out
    i think option 1 would good if you filter out the unwanted columns
    Regards
    Amul

  • How to use table name dynamically in report  procedure

    Hello every body
    I want to use table name dynamically means at runtime i want to pass table name.
    I can do this by lexical parameter in main query.
    But my problem is that i want to crate new format trigger or new function that use 1 cursor in which that lexical parameter : table name is used.
    so how to do that?
    i can not use that lexical parameter it is giving error.
    please help me how to do that.

    Call a database function which you pass the lexical parameter. Then in the database function you use dynamic sql or the execute immediate option to build the SQL string.
    Return the information from this function to build your business logic on in your format trigger.
    Marcos

  • How to Concatenate Table name and Where condition at runtime

    I am passing parameter as User and Zone to Stored Procedure.How to concatenate Table Name
    and WHERE CONDITION in SQL Statement.i have different type of users and zones.

    Hi !
    declare
      cur sys_refcursor;
      r emp%rowtype;
      v_sql varchar2(512);
    begin
    -- do your logic here
      v_sql := 'select * from emp';
      open cur for v_sql;
      loop
        fetch cur into r;
        exit when cur%notfound;
        dbms_output.put_line(r.ename);
      end loop;
      close cur;
    end;In this example you can see how can be done this with cursor vars .. You should concatenate v_sql string according to your requirements.
    But as in further posts has already been mentioned , be carefull at publishing such kind of procedures and think on security.
    Also when you want dynamicaly change from clause , you should consider using different records to accept data ? Maybe all your tables has the same structure and then this problem will be smaller.
    T
    T

  • How get all table name from database

    hi master
    sir
    how get all table name from database

    The big question is 'why'.
    Selecting from view 'dba_tables' will indeed give the list of all tables in the database, but that includes the dictionary tables and the internal tables, and many others that are probably not of interet to a person who needs to ask this question. Besides, the dba_tables view requires access to a DBA account.
    There are several other views: "user_tables" will list all the tables in this user's schema; and "all_tables" will list all the tables this user can access in some way.
    The above do not, of course, include any information about synonyms, sequences, views, indexes and so on.
    The correct answer and the meaningful answer may be two different things.

  • Passing TABLE NAME as parameter is possible or not?

    I want develop a small/simple report like this
    TABLE NAME :
    WHERE :
    ORDER BY :
    QUERY ROWS
    In the above model i want to pass all the three (TABLE NAME,WHERE and ORDER BY) as a parameter.
    My doubt, is that possible to pass TABLE NAME as a parameter? If so!
    When i enter any TABLE NAME it has to fetch me out the records of that table (Based on WHERE condition and ORDER BY).
    Is that possible to do?
    Need some help!
    Edited by: Muthukumar Seshadri on Aug 10, 2012 6:19 PM

    Yes, it is possible with lexical parameters. Look in the help for examples:
    SELECT Clause
    SELECT &P_ENAME NAME, &P_EMPNO ENO, &P_JOB ROLE  FROM EMP
    P_ENAME, P_EMPNO, and P_JOB can be used to change the columns selected at runtime.  For example, you could enter DEPTNO as the value for P_EMPNO on the Runtime Parameter Form. 
    Note that in this case, you should use aliases for your columns.  Otherwise, if you change the columns selected at runtime, the column names in the SELECT list will not match the Report Builder columns and the report will not run.
    FROM Clause
    SELECT ORDID, TOTAL FROM &ATABLE
    ATABLE can be used to change the table from which columns are selected at runtime.  For example, you could enter ORD for ATABLE at runtime. 
    If you dynamically change the table name in this way, you may also want to use lexical references for the SELECT clause (look at the previous example) in case the column names differ between tables.
    WHERE Clause
    SELECT ORDID, TOTAL FROM ORD WHERE &CUST
    ORDER BY Clause
    SELECT ORDID, SHIPDATE, ORDERDATE, TOTAL  FROM ORD ORDER BY &SORT You have to be really careful with this approach. Dynamic SQL may cause serious performance problems.
    Edited by: InoL on Aug 10, 2012 10:06 AM

  • Pass table name as a parameter to function

    Is there a way to pass table name as a parameter to functions? Then update the table in the function.
    Thanks a lot.
    Jiaxin

    Hi, Harm,
    Thank you very much for your suggestion and example. But to get my program work, i need to realise code like follows:
    CREATE OR REPLACE FUNCTION delstu_func(stuno char) RETURN NUMBER AS
    BEGIN
    EXECUTE IMMEDIATE 'DELETE FROM student s' ||
    'WHERE' || 's.student_number' || '=' || stuno;
    LOOP
    DBMS_OUTPUT.PUT_LINE('record deleted');
    END LOOP;
    END;
    SELECT delstu_func('s11') FROM STUDENT;
    The intention is to check if such a function can perform operations such as update, delete and insert on occurence of certain values. When executing the above statement, the system returns an error message:
    ERROR at line 1:
    ORA-00933: SQL command not properly ended
    ORA-06512: at "SCMJD1.DELSTU_FUNC", line 3
    Could you tell me where is wrong?
    Jiaxin

  • How to find table name for the fields from Standard Extractor in CRM system

    How to find table name of fields from the standard extractor in CRM system ?
    e.g. We use LBWE TCode in R/3 system to find table name for the field from Extractor VCSCL(e.g.).
    Likewise is there any way to find table name for the fields from Standard extractor like 0CRM_LEAD_I.

    Hi ,
    Please find the link below for understanding BW CRM analysis.
    http://help.sap.com/bp_biv135/html/bw.htm
    activate the CRM DSs by scenario:
    1) Activate the application component hierarchy (tcode RSA9). Changes made to the application component hierarchy in the CRM system can be transferred to the BW using the "Edit Application Component Hierarchy" (SBIW - Postprocessing of DataSources).
    SAP Note 434886 must be implemented in CRM 3.0 before the application component hierarchy is activated.
    2) Activate the Business Content DataSources (tcode RSA5).
    Select/enter the application component and choose Execute (F8).
    To compare the shipped and active versions, choose the 'Select Delta' pushbutton. If there is no active version of the DataSource, it is selected automatically.
    To activate the shipped version, choose the 'Transfer DataSources' pushbutton.
    3) Management of the versions of the BW-Adapter metadata (tcode BWA5). All DataSources are displayed that are managed by the BW Adapter.
    As in transaction RSA5 (Service API Metadata Activation), the 'Select Delta' function can be used to select the inactive DataSources or compare shipped and active versions.
    You can also go directly to the screen for maintaining DataSources that are managed by the BW Adapter.
    The 'Compare Version' function makes a detailed comparison of the shipped and active versions.
    All BW-Adapter metadata is considered when versions are compared:
    Header information (Table SMOXHEAD)
    Mapping information (Table SMOXRELP)
    Global selection conditions (Table SMOXGSEL)
    Attribute key fields (Table SMOXAFLD)
    Hope this helps.
    Regards,
    csm reddy

Maybe you are looking for