Reputation: 18848
I am newbie to SQL.
I want to insert data into table using below for-loop query. But unable to identify the issue
declare
cursor mac is SELECT DISTINCT(MAC) FROM DEVICES;
cmd varchar2(200);
begin
for c in mac loop
cmd := 'INSERT INTO MAC VALUES(DEVICES_ID_SEQ.nextVal,'||c.MAC||',"ABC","123")';
execute immediate cmd;
end loop;
end;
For each MAC in existing table, I want to insert new record.
Upvotes: 0
Views: 2634
Reputation: 425291
You don't need a cursor for that.
Just run:
INSERT
INTO mac
SELECT DEVICES_ID_SEQ.nextval, mac, 'abc', 123
FROM (
SELECT DISTINCT
mac
FROM devices
)
Upvotes: 7