Create a procedure which will pass table name as an argument

Please let me know, I want to create a procedure which will take
table name as an argument and will display total no. of rows of
that table.

Hopefully this code will help u.
CREATE OR REPLACE PROCEDURE P1(TAB_NAME IN VARCHAR2, ROW_NO
OUT NUMBER)
AS
SQL_STMT VARCHAR2(1000);
BEGIN
SQL_STMT := 'SELECT COUNT(*) FROM ' || TAB_NAME;
EXECUTE IMMEDIATE SQL_STMT INTO ROW_NO;
END;
SQL > VAR A NUMBER
SQL > EXEC P1('EMP',:A);
SQL > PRINT A
A
12
SQL > SELECT COUNT(*) FROM EMP;
COUNT(*)
12

Similar Messages

  • I want to create stored procedure which will give output rows from "values that are passed as one parameter (comma seperated) to store procedure".

    Hello,
    I want to create stored procedure which will give output rows from "values that are passed as one parameter (comma seperated) to store procedure".
    Suppose , 
    Parameter value : person 1,person2,person3 
    table structure : 
    Project Name | officers 1 | officers 2
    here, officers 1 or officers 2 may contain names of multiple people.
    expected OUTPUT : distinct list(rows) of projects where person 1 or person 2 or person 3 is either officer1 or officer 2. 
    please explain or provide solution in detail 
    - Thanks

    Hi Visakh,
    Thanks for reply.
    But the solution you provided giving me right output only if officer 1 or officer 2 contains single value , not with comma seperated value.
    Your solution is working fine for following scenario : 
    Project 
    Officers 1
    Officers 2
    p1
    of11
    off21
    p2
    of12
    off22
    with parameter : of11,off22 : it will give expected output
    And its not working in case of :
    Project 
    Officers 1
    Officers 2
    p1
    of11,of12
    off21,off23
    p2
    of12,of13
    off22,off24
    with parameter : of11,off22 : it will not give any row in output
    I need patten matching not exact match :) 
    ok
    if thats the case use this modified logic
    CREATE PROC GetProjectDetails
    @PersonList varchar(5000)
    AS
    SELECT p.*
    FROM ProjectTable p
    INNER JOIN dbo.ParseValues(@PersonList,',')f
    ON ',' + p.[officers 1] + ',' LIKE '%,' + f.val + ',%'
    OR ',' + p.[officers 2] + ',' LIKE '%,' + f.val + ',%'
    GO
    Keep in mind that what you've done is a wrong design approach
    You should not be storing multiples values like this as comma separated list in a single column. Learn about normalization . This is in violation of 1st Normal Form
    Please Mark This As Answer if it solved your issue
    Please Mark This As Helpful if it helps to solve your issue
    Visakh
    My MSDN Page
    My Personal Blog
    My Facebook Page

  • How 2 call BAPI & pass table name so that it will insert table data 2 SAP

    Hi guys,
    Has anyone tried calling BAPI from BODS; Please share screen shots and details. I want to call BAPI fron BODS which will take table name as a parameter and insert that table data to SAP.

    HI,
    in case you mean BusinessObjects Data Services with BODS then I would suggest you post your question into the EIM area of the SDN forums.
    Ingo

  • Passing table name to a procedure and then need to open a cursor ..

    Hi All,
    I have been out of touch in plsql for sometime and need to do something like :
    passing table name to a procedure while calling(fp_table_old captures this value) and then need to open a cursor for this table in 'for i in (select * from fp_table_old )', one of the ways I tried is : for i in (execute immediate Lv_sql_query ) but its not working for me, please find the code below for reference, I know I am sounding stupid but I have been very out of touch, please help.
    code :
    create procedure p_table_compare(fp_table_old in varchar2, fp_table_new in varchar2)
    as
    Lv_sql_query varchar2(2000);
    begin
         Lv_sql_query := 'select * from '||fp_table_old;
         for i in (Lv_sql_query)
         loop
              dbms_output.put_line(i.employee_id);
         end loop;
    end;
    Regards
    Rahul

    if I am doing something like this : for i in (execute immediate Lv_sql_query)
    I am getting an error which is :
    PLS-00103: Encountered the symbol "IMMEDIATE" when expecting one of the following:
    . ( ) , * @ % & | = - + < / > at in is mod remainder not
    range rem => .. <an exponent (**)> <> or != or ~= >=
    My code:
    create or replace procedure p_table_compare(fp_table_old in varchar2, fp_table_new in varchar2)
    as
    Lv_sql_query varchar2(2000);
    begin
         --dbms_output.put_line('Hello World');
         Lv_sql_query := 'select * from '||fp_table_old;
         for i in (execute immediate Lv_sql_query)
         loop
              dbms_output.put_line(i.employee_id);
         end loop;
    end;
    Please help.
    Regards
    Rahul

  • Help passing table name as parameter to a procedure

    Hello,
    i'm trying to write a procedure that takes a table name as input and uses a cursor to select a column,count(1) from the passed table to the cursor. The procedure i've come up with is as follows,
    CREATE OR REPLACE
    PROCEDURE excur(
        p_tbl user_tables.table_name%type )
    AS
      type rc is ref cursor;
      c rc;
      res BOOLEAN;
    BEGIN
      open c for 'SELECT columnA,COUNT(1) FROM'|| p_tbl||';';
      close c;
    END excur;When i try to execute it, an error pops up informing that a table cannot be used in this context. As in i cannot pass a table name as an argument to a procedure. Kindly guide as to how to solve this situation.

    vishm8 wrote:
    Hello,
    i'm trying to write a procedure that takes a table name as input and uses a cursor to select a column,count(1) from the passed table to the cursor. The procedure i've come up with is as follows,
    CREATE OR REPLACE
    PROCEDURE excur(
    p_tbl user_tables.table_name%type )
    AS
    type rc is ref cursor;
    c rc;
    res BOOLEAN;
    BEGIN
    open c for 'SELECT columnA,COUNT(1) FROM'|| p_tbl||';';
    close c;
    END excur;When i try to execute it, an error pops up informing that a table cannot be used in this context. As in i cannot pass a table name as an argument to a procedure. Kindly guide as to how to solve this situation.Generally speaking, Dynamic code is a bad idea for a staggering number of reasons.
    That aside, what do you want to return? You're selecting a column and applying an aggregate function (count) but you're not grouping, that doesn't usually work out too well.
    TUBBY_TUBBZ?select owner, count(*) from all_objects;
    select owner, count(*) from all_objects
    ERROR at line 1:
    ORA-00937: not a single-group group functionWhy do you perceive the need to be able to take in ANY table name, can't you design an application where table names are known at compile time and not run time?

  • Passing Table Name to Stored Procedure for From Clause

    Is it possible to pass a table name to a stored procedure to be used in the From clause? I have the same task to perform with numerous tables and I'd like to use the same SP and just pass the table name in. Something like this:
    =======================================
    CREATE OR REPLACE PROCEDURE SP_TEST(
    in_TABLE IN VARCHAR2,
    AS
    V_TABLE VARCHAR2(10);
    BEGIN
    V_TABLE := 'st_' || in_TABLE; -- in_TABLE is 2-3 character string
    SELECT some_columns
    INTO some_variables
    FROM V_TABLE
    WHERE some_conditions...;
    END;
    =======================================
    I'm also using the passed table name to assign to variables in the Select and Where clauses. What I'm getting is an error that V_TABLE must be declared. When I hard code the table name, I don't get any errors, even though I'm also using the same method to assign values in the Select and Where clauses.
    Thanks,
    Ed Holloman

    You need to use dynamic SQL whenever you are swapping out object names (tables, columns).
    create or replace procedure sp_test
      (in_table in varchar2)
    is
      -- variables
    begin
      execute immediate 'select a, b, c from st_' || in_table || ' where x = :xval and y = :yval'
         into v_a, v_b, v_c using v_x, v_y;
    end;

  • How i will pass table within procedure

    how i will pass table within procedure and call another procedure.
    can anybody give some example

    You are not specific at all. Do you mean you want to pass table_name as parameter to a procedure? Do you want to pass tables_name dynamically while the procedure is running? If so, use dynamic sql. You can directly call another procedure within a procedure.
    create or replace procedure sample_proc
    as
    v_table_name varchar2(10);
    begin
       select table_name into v_table_name
       from user_tables
       where table_name = 'EMP';
       procedure_2(v_table_name);
    end;And the procedure_2 would be
    create or replace procedure procedure_2(p_table_name varchar2)
    as
    begin
       execute immediate 'DROP TABLE '||p_table_name;
    end;For God's sake please completely describe what you want, otherwise we don't know what on Earth you want!
    Cheers
    Sarma.

  • In JDBC Sender Adapter , the server is Microsoft SQL .I need to pass current date as the input column while Executing stored procedure, which will get me 10 Output Columns. Kindly suggest me the SQL Query String

    In JDBC Sender Adapter , the server is Microsoft SQL .I need to pass current date as the input column while Executing stored procedure, which will get me 10 Output Columns. Kindly suggest me the SQL Query String , for executing the Stored Procedure with Current date as the input .

    Hi Srinath,
    The below blog might be useful
    http://scn.sap.com/community/pi-and-soa-middleware/blog/2013/03/06/executing-stored-procedure-from-sender-adapter-in-sap-pi-71
    PI/XI: Sender JDBC adapter for Oracle stored procedures in 5 days
    regards,
    Harish

  • Create a procedure which requires displaying information from two tables

    What if we want to create a procedure which requires displaying information from two tables. In this situation,
    I need to display all the details of an employee from an 'emp' table based on the deptno. and also their salary information which is
    stored in "Salgrade" Table. Is the use of "And" the correct way of using it?
    Do I need to declare all the attributes before begin or I can just access them directly? Thanks
    Create Procedure findDept
    ( I_deptno IN number
    ) As
    Begin
    Select Ename, Job, MGR, Hiredate, Sal, Comm, Dept_no from emp and exp, grade , location from salgrade where dept_no
    = I_Deptno;
    Exception
    When NO_Data_Found then
    DBMS_Output.Put_Line(' Department not found"|| I_deptno);
    End findDept;

    Hi,
    Besides Qwerty's remark, you've also got a quoting error:
    Change
    DBMS_Output.Put_Line(' Department not found"|| I_deptno);
    {code}
    into
    {code}
    dbms_output.put_line(' Department not found: '|| i_deptno);
    {code}
    Which leads to the question:
    What do you want to do when a record is found?
    Display 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.

  • 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

  • Create a Procedure that will accept a variable

    Hi,
    I'm new to pl/sql and I'm trying to create a procedure that will accept a variable. I want to use this sql to create restore points that can store the name of each batch job as it runs.
    The code works fine on it's own, but if I try to create the procedure it asks me for a value for 1, how do I stop it doing that?
    CREATE OR REPLACE PROCEDURE create_restore_point AS
    DECLARE
    pointno number(5);
    pointname varchar2(128) := upper('&1');
    dbname v$database.name%TYPE;
    scn_time date;
    scn number;
    BEGIN
    SELECT restore_point_seq.nextval into pointno from dual;
    SELECT name into dbname from v$database;
    SELECT sysdate into scn_time from dual;
    SELECT dbms_flashback.get_system_change_number into scn from dual;
    INSERT into restore_point
    values
    (pointno,pointname,dbname,scn_time,scn);
    commit;
    END create_restore_point;
    Many thanks in advance,
    Tom.

    It asks for value of 1 because you have &1 in your code. If you want to pass the value for pointname, then you can change your procedure like below.
    CREATE OR REPLACE PROCEDURE create_restore_point (svar varchar2)AS
    DECLARE
    pointno number(5);
    pointname varchar2(128) := upper(svar);
    dbname v$database.name%TYPE;
    scn_time date;
    scn number;
    BEGIN
    SELECT restore_point_seq.nextval into pointno from dual;
    SELECT name into dbname from v$database;
    SELECT sysdate into scn_time from dual;
    SELECT dbms_flashback.get_system_change_number into scn from dual;
    INSERT into restore_point
    values
    (pointno,pointname,dbname,scn_time,scn);
    commit;
    END create_restore_point;

  • Passing table names through variables

    Hi All,
    Is it possible to pass the table name in a query using a variable.
    For example - can we perform a select on a table where the table name is passed using a variable.
    Pl assist. Thanks.

    Hello,
    Are you wanting to do this in a procedure? Say the table name being passed in as Input Parameter?
    We can do that using Dynamic SQL:
    CREATE PROCEDURE "PROC_01"
    (IN v_IN_Var1 NVARCHAR)
    LANGUAGE SQLSCRIPT AS
    BEGIN
        DECLARE ABC NVARCHAR(300) := NULL;
        ABC := 'SELECT * FROM ' || :v_IN_Var1;
        EXEC (:ABC);
    END;
    CALL PROC_01 ('DUMMY');
    But the only thing is you'll not be able to see the result set directly.
    You might wan2 fire the select to insert the data into a different table.

  • How to write a procedure with the table name as an argument

    I'm very new to Oracle, usually developing in SQL server.
    I would like to write a procedure that accepts a table name and then does a
    SELECT * FROM [table name] order by CollectionDate.
    In SQL I would build up a varchar variable and then execute it.
    Thanks

    I often wonder why so many people new to Oracle want to do this as a first step, when most people who know what they are doing try and avoid it.
    SQL> create or replace function f (p_tab varchar2)
      2  return sys_refcursor as
      3    c sys_refcursor;
      4  begin
      5    open c for
      6      'select * from ' || p_tab || ' order by hiredate';
      7    return c;
      8  end;
      9  /
    Function created.
    SQL> var c refcursor
    SQL> exec :c := f('emp')
    PL/SQL procedure successfully completed.
    SQL> print c
    EMPNO ENAME      JOB          MGR HIREDATE      SAL   COMM DEPTNO
      7369 SMITH      CLERK       7902 12/17/1980    800            20
      7499 ALLEN      SALESMAN    7698 02/20/1981   1600    300     30
      7521 WARD       SALESMAN    7698 02/22/1981   1250    500     30
      7566 JONES      MANAGER     7839 04/02/1981   2975            20
      7698 BLAKE      MANAGER     7839 05/01/1981   2850            30
      7782 CLARK      MANAGER     7839 06/09/1981   2450            10
      7844 TURNER     SALESMAN    7698 09/08/1981   1500      0     30
      7654 MARTIN     SALESMAN    7698 09/28/1981   1250   1400     30
      7839 KING       PRESIDENT        11/17/1981   5000            10
      7900 JAMES      CLERK       7698 12/03/1981    950            30
      7902 FORD       ANALYST     7566 12/03/1981   3000            20
      7934 MILLER     CLERK       7782 01/23/1982   1300            10
      7788 SCOTT      ANALYST     7566 12/09/1982   3000            20
      7876 ADAMS      CLERK       7788 01/12/1983   1100            20
    14 rows selected.
    SQL>Note if you pass in a value to be used in the where clause like deptno, you should not use this technique, but use bind variables intead like this -
    Wrote file afiedt.sql
      1  create or replace function f (p_tab varchar2, p_deptno number)
      2  return sys_refcursor as
      3    c sys_refcursor;
      4  begin
      5    open c for
      6      'select * from ' || p_tab
      7        || ' where deptno = :p_deptno order by hiredate'
      8        using p_deptno;
      9    return c;
    10* end;
    SQL> /
    Function created.
    SQL> exec :c := f('emp', 10)
    PL/SQL procedure successfully completed.
    SQL> print c
    EMPNO ENAME      JOB          MGR HIREDATE      SAL   COMM DEPTNO
      7782 CLARK      MANAGER     7839 06/09/1981   2450            10
      7839 KING       PRESIDENT        11/17/1981   5000            10
      7934 MILLER     CLERK       7782 01/23/1982   1300            10
    SQL>

  • 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

Maybe you are looking for

  • Error While saving Basic Detail in iRecruitment External Candidate

    Guys Actually data was already saved by IEC responsibility after some changing then we accessed this page again then basic data was blank while address information was appearing properly, and we update again basic information then system is being rai

  • Ipod: Unable to reset or restore

    My Ipod (click wheel) will not reset or restore. The apple logo appears on the Ipod screen, followed by the file with an exclamation point. I have tried to use Ipod updater without success. The Ipod does not register that it is charging either when p

  • Can I take ilife 08 off my mac??

    Hi ya'll does anyone know if you can take ilife 08 off my macbook and put it on my imac (which is about three years old and running ilife 05 i think)? The story is we were just given the imac and my macbook was the main computer and ilife 08 is a goo

  • Need external antenna connector for old Nokia 5110...

    Hi all, this is a long shot I admit, but doesn anyone know what type of connector (and where to purchase) was used on the old 5110/6110 Nokia phones, it is the external connector located on the rear behind the rubber grommit use for the car kits... I

  • I'm missing part of a song I downloaded from itunes store. How do I fix this?

    How can I fix a song that is missing the last minute? I downloaded it from iTunes store.