CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2008
    Posts
    45

    how to pass table name as parameter...

    hi all,
    can anyone give me some suggestion s HOW TO PASS TABLE NAME AS A PARAMETER to procedure...
    i vil show some example .....

    Code:
    Create a procedure that takes table name and column names as parameter and displays data.
    this is my question...
    i tried something but am getting error...so anyone help me...my code is...
    Code:
    create or replace
    PROCEDURE TO_CALL_TABLE
    ( TABLENAME IN varchar,E_NAME OUT EMP.ENAME%TYPE,E_NO IN EMP.EMPNO%TYPE) is
    BEGIN
    SELECT ENAME INTO E_NAME FROM tablename
    where empno=e_no;
    END;

  2. #2
    Join Date
    Feb 2008
    Location
    Bangalore
    Posts
    149

    Re: how to pass table name as parameter...

    study the syntax of Sp_executeSql System Stored Procedure If you are using sql server 2000 and Above
    Encourage the efforts of fellow members by rating

    Lets not Spoon Feed and create pool of lazy programmers

    - ComIT Solutions

  3. #3
    Join Date
    Jan 2008
    Posts
    45

    Re: how to pass table name as parameter...

    And My Database Is Oracle.....

  4. #4
    Join Date
    Jun 2006
    Posts
    437

    Re: how to pass table name as parameter...

    Hi chinnaraj

    This ins't a easy work; you have to use SQL dynamic.
    Take a look at this.

    Code:
    PROCEDURE TO_CALL_TABLE(pTableName IN VARCHAR2,     -- your TABLENAME
                            pTableField IN VARCHAR2,    -- you need the name of field, aren't you?
                            pE_NO IN EMP.EMPNO%TYPE,    -- your E_NO, the filter
                            pEName OUT EMP.ENAME%TYPE)   -- your E_NAME, the result 
    IS
          vSelectString  VARCHAR2(4000) := NULL;
          vCursorId      INTEGER := 0;
          vDummy         INTEGER := 0;
          vResult        EMP.ENAME%TYPE;
    
    BEGIN
    
        -- 1. Create the query
        vSelectString := 'SELECT ' || pTableField
                         ' FROM ' || pTableName ||
                         ' WHERE EMPNO = ' || TO_CHAR(pE_NO);
                         
        -- 2. Create the cursor
        vCursorId := DBMS_SQL.OPEN_CURSOR;
        DBMS_SQL.PARSE(vCursorId, vSelectString, DBMS_SQL.Native);
        
        -- 3. Set the column
        DBMS_SQL.DEFINE_COLUMN(vCursorId, 1, vResult);
        
        -- 4. Execute the query
        vDummy := DBMS_SQL.EXECUTE(vCursorId);
      
        -- 5. Fetch
        LOOP
            IF DBMS_SQL.FETCH_ROWS(vCursorId) = 0 THEN
                EXIT;
            END IF;
            DBMS_SQL.COLUMN_VALUE(vCursorId, 1, vResult);
        END LOOP;
    
        -- 6. Close the cursor
        DBMS_SQL.CLOSE_CURSOR(vCursorId);
    
        pEName := vResult;
    
    --SELECT ENAME INTO E_NAME FROM tablename
    --WHERE empno=e_no;
    END;
    I haven't tried, I hope this will work.
    Are you doing an assignment?

  5. #5
    Join Date
    Jan 2008
    Posts
    45

    Re: how to pass table name as parameter...

    yeah am doing assignment

  6. #6
    Join Date
    Jun 2006
    Posts
    437

    Re: how to pass table name as parameter...

    Quote Originally Posted by chinnaraj
    yeah am doing assignment
    Well.

    You should study the theory first, and then you should try to write the code yourself : if you only see the code written by others you won't learn a lot...
    Moreover Oracle is an advanced topic, so I think you're attending some computer science course: in this case is better for you if you will be able to do what the teacher teaches, more than passing the exams only.
    Last edited by davide++; February 25th, 2008 at 07:27 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured