0

Is it allowed to use Insert query with variables?

Like: Insert Into var_1 (col1_Var_Name, col2_vAr_Name) Values (var_3, var_4)

?

1
  • We are looking for questions that are well researched and demonstrate some actual work done by the author of the OP. Commented Jun 5, 2014 at 6:37

2 Answers 2

1

Table and column names cannot be passed to a statement as variables. One way to accomplish what you seem to be trying to do is to build the statement as a string and then execute it dynamically, as in:

DECLARE
  strStmt   VARCHAR2(2000);
  strTable  VARCHAR2(30) := 'SOME_TABLE';
  strCol1   VARCHAR2(30) := 'COL1';
  strCol2   VARCHAR2(30) := 'COL2';
  strCol3   VARCHAR2(30) := 'COL3';
  nVal1     NUMBER       := 42;
  strVal2   VARCHAR2(30) := 'HELLO';
  nVal3     NUMBER       := 100;
BEGIN
  strStmt := 'INSERT INTO ' || strTable ||
               '(' || strCol1 || ',' ||
                      strCol2 || ',' ||
                      strCol3 || ')' ||
               ' VALUES (' ||
                         nVal1   || ','    ||
                         '''' || strVal2 || ''',' ||
                         nVal3 || ')';

  EXECUTE IMMEDIATE strStmt;
END;

Share and enjoy.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, great answer, though google replied faster :) now i have Dynamic sql queries:)
0
DECLARE
   TYPE type_name IS TABLE OF
      (column_type |
      variable%TYPE |
      table.column%TYPE
         [NOT NULL]
            INDEX BY BINARY INTEGER;

fOR MORE INFO Declare Table type variable

2 Comments

Can you please explain what is %TYPE using a nice example
Did you see the link below the syntax? @Batousik

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.