Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Creating a procedure to insert data into table conditionally

RajivTxMar 12 2016 — edited Mar 14 2016

Hi,

I am trying to create a procedure to insert data into a table, by verifying the in parameter against a column value and insert only if that parameter value is found.

In other words, trying to insert unique values into the table column.

This is what I did:

create or replace procedure sh_ins (v_name in varchar2)

as

v_shname varchar2(10);

cursor sh_cur is select sh_name from superheros;

begin

for v_counter in sh_cur loop

if v_counter.sh_name <> v_name then

insert into superheros (sh_name) values (v_name);

commit;

else

dbms_output.put_line('Name Already Exists');

end if;

end loop;

end;

I know I am reading only 1 value at a time in the cursor and as it is not matching with my in parameter, it is inserting. But I want to read all values and compare every single value and then only insert.

I would appreciate if anyone can guide me through this.

Thanks

Rajiv

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2016
Added on Mar 12 2016
5 comments
3,969 views