How can I use variable value as a table name?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bhavanirayala
    New Member
    • Jul 2007
    • 25

    How can I use variable value as a table name?

    Hi All,

    I have a variable which stores the table name.
    i.e
    [code=oracle]

    DECLARE
    V_TEMP VARCHAR2(10) := 'EMP';
    BEGIN
    SELECT * FROM V_TEMP;
    END;

    [/code]


    BUT I am getting the error like V_TEMP is not the table name.

    How can I access the value of the V_TEMP value?
    First of all Is It acceptable or not?

    please help me.

    Thankyou.
    Last edited by amitpatel66; Jan 10 '08, 01:04 PM. Reason: code tags
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    Take a Look at this Thread

    Regards
    Veena

    Comment

    • amitpatel66
      Recognized Expert Top Contributor
      • Mar 2007
      • 2358

      #3
      Originally posted by bhavanirayala
      Hi All,

      I have a variable which stores the table name.
      i.e
      [code=oracle]

      DECLARE
      V_TEMP VARCHAR2(10) := 'EMP';
      BEGIN
      SELECT * FROM V_TEMP;
      END;

      [/code]


      BUT I am getting the error like V_TEMP is not the table name.

      How can I access the value of the V_TEMP value?
      First of all Is It acceptable or not?

      please help me.

      Thankyou.
      [code=oracle]
      SQL> ed
      Wrote file afiedt.buf

      1 declare
      2 v_temp varchar2(10):= 'emp_det';
      3 e_rec emp_Det%ROWTYPE ;
      4 BEGIN
      5 EXECUTE IMMEDIATE 'SELECT * FROM '||v_temp ||' WHERE empno = 1' INTO e_rec;
      6 DBMS_OUTPUT.PUT _LINE(e_rec.emp no||','||e_Rec. ename);
      7* END;
      SQL> /

      PL/SQL procedure successfully completed.

      SQL> set serveroutput on
      SQL> /
      1,AAAA

      PL/SQL procedure successfully completed.

      SQL>
      [/code]

      Comment

      • bhavanirayala
        New Member
        • Jul 2007
        • 25

        #4
        Thankyou.Now I am able to run successfully.


        Originally posted by amitpatel66
        [code=oracle]
        SQL> ed
        Wrote file afiedt.buf

        1 declare
        2 v_temp varchar2(10):= 'emp_det';
        3 e_rec emp_Det%ROWTYPE ;
        4 BEGIN
        5 EXECUTE IMMEDIATE 'SELECT * FROM '||v_temp ||' WHERE empno = 1' INTO e_rec;
        6 DBMS_OUTPUT.PUT _LINE(e_rec.emp no||','||e_Rec. ename);
        7* END;
        SQL> /

        PL/SQL procedure successfully completed.

        SQL> set serveroutput on
        SQL> /
        1,AAAA

        PL/SQL procedure successfully completed.

        SQL>
        [/code]

        Comment

        • amitpatel66
          Recognized Expert Top Contributor
          • Mar 2007
          • 2358

          #5
          Originally posted by bhavanirayala
          Thankyou.Now I am able to run successfully.
          You are welcome :)

          Happy coding :)

          Comment

          Working...