Variable as column or table name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • devikacs
    New Member
    • Jun 2007
    • 96

    Variable as column or table name

    Can I use a variable as the column name or table name in a stored procedure. I want to use it for update and insert statements
  • sakumar9
    Recognized Expert New Member
    • Jan 2008
    • 127

    #2
    Originally posted by devikacs
    Can I use a variable as the column name or table name in a stored procedure. I want to use it for update and insert statements
    Yes, it is possible to have table/column names as variables. In that case, you will have to use dynamic statements to execute the commands. For example:

    Code:
        CREATE PROCEDURE test
        (IN tabname CHAR(6))
        LANGUAGE SQL
        BEGIN
        DECLARE stmt VARCHAR(1000);
    
        SET stmt = 'INSERT INTO '|| tabname || 'values (1)';
        PREPARE s1 FROM stmt;
        EXECUTE s1;
        END @
    Regards
    -- Sanjay

    Comment

    • devikacs
      New Member
      • Jun 2007
      • 96

      #3
      Thanks a lot. ..........
      Originally posted by sakumar9
      Yes, it is possible to have table/column names as variables. In that case, you will have to use dynamic statements to execute the commands. For example:

      Code:
          CREATE PROCEDURE test
          (IN tabname CHAR(6))
          LANGUAGE SQL
          BEGIN
          DECLARE stmt VARCHAR(1000);
      
          SET stmt = 'INSERT INTO '|| tabname || 'values (1)';
          PREPARE s1 FROM stmt;
          EXECUTE s1;
          END @
      Regards
      -- Sanjay

      Comment

      Working...