Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert dynamically through a variable in PL/SQL?

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 Answers

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;
like image 63
Dheer Avatar answered Sep 05 '25 00:09

Dheer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!