0

I´m kind of new to SQL-Procedures, but I´ve written a procedure which adds a line to my datatable. This works pretty fine, but when i run trough the procedure twice i want to avoid the line being added again into the datatable based on an Oracle-Database. So my question is, how to set the "if already exists"-condition within the case command.

My code looks like that:

...
case number

when 5 then 

/* if not exists` <<--- i need something to avoid doubled entrys */

  pi_event_line_add(5,'xxx' ....);


end case;
end procedure;
Mattis Seehaus
  • 114
  • 1
  • 11

2 Answers2

0

One solution, is to interrogate your table first then decide to insert. Like:

declare cnt number;

begin
select count(*) into cnt from your_table where id = 5;

if cnt = 0 then
   pi_event_line_add(5,'xxx'...);
end if;

Also, you probably want to add a unique constraint on the column that stores "5" if it truly should be unique.

Nick
  • 2,524
  • 17
  • 25
0

Use a MERGE statement in pi_event_line_add instead of an INSERT. In the MERGE only implement the WHEN NOT MATCHED INSERT portion of the statement.

Best of luck.