4

Lets create a table first

create table test
(
  id number,
  name varchar2(20)
);

Now during insert, I want to hold the data into variable first & then dynamically pass the variable into the VALUES clause like this:

declare
  v_data varchar2(50);
begin
  v_data:='1,sunny';
  execute immediate 'insert into test values(v_data)';
  commit;
end;

But its showing some errors(Not enough values)...... plz help how to achieve this??

1
  • Try creating a :dynstmt string by doing a string operation, including the quotes for the srting value and then use EXEC SQL EXECUTE IMMEDIATE :dynstmt. Note that this is not a good practise as it would be prone to SQL injection. Commented Mar 30, 2009 at 12:13

4 Answers 4

7

You need to use different variables for each value

declare 
  v_data1 number
  v_data2 varchar2(50);
begin 
  v_data1 :=1
  v_data2 = 'sunny'; 

  insert into test values(v_data1,v_data2);
  -- Alternatively insert into test (Name) values (v_data2);
commit; 
end;
Sign up to request clarification or add additional context in comments.

1 Comment

But my requirement is different, i have to pass the variable which holds the value string commaseparated.
5

Table test has two columns. You're only inserting one and not naming which column it is hence "not enough values". So you need:

INSERT INTO test (name) VALUES (data)

or probably better is to put in an ID:

INSERT INTO test (id, name) VALUES (1, data)

or simply:

INSERT INTO test VALUES (1, data)

For this kind of thing though I would use a cursor rather than dynamic SQL (or even inline SQL).

Comments

4

The normal way to pass values into dynamic SQL statements is with bind variables like this:

declare 
   v_id integer;
   v_name varchar2(50);
begin
   v_id := 1;
   v_name := 'sunny';
   execute immediate
      'insert into test (id, name) values(:b1, :b2)'
      using v_id, v_name; 
   commit; 
end;

That needs one variable per value.

5 Comments

:b1 is replaced by the string "v_id" for me
@Eildosa did you put using 'v_id', 'v_name'?
So you didn't follow the method in my answer, and you got a different result? Interesting but not sure how I can help with that?
no, sorry it was ambiguous, I mean since it was not working I used the other method instead. when I use your method it puts the name of the variable in the statement not the content of the variable. || however put the content of the variable
No, it doesn't. If that happened you did not use my code I can assure you.
1

Your approach works, but you need to adjust your query a little:

execute immediate 'insert into test values(' || v_data|| ')';

so that the contents of your v_data variable are actually inserted into the string, not the value "v_data" itself.

3 Comments

thanx buddy...but its still showing error - ORA-00984: column not allowed here
bind variables should be recommended, but for occasional inserts there may not be a performance penalty, although this is an opportunity for sql injection attacks.
That's because the data would need to be: '1,''sunny''' for this to work. Best to avoid this approach anyway, as Martlark days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.