I have to add many registries to a table and I need help since I am starting to work with PL/SQL
I have a table with three columns (ID, KEY, VALUE). The ID & KEY has to be unique, and if there is not a value for a KEY called ‘process_name’ is have to create a new registry where the value has to be the same as the ID.
for example, I could have this in my table
ID KEY VALUE
A cycles 4
A process_name A
A number 2
B cycles 3
B number 2
C cycles 5
So, I would need to insert 2 records to the DB:
B process_name B
C process_name C
How should I start creating the script?
Thanks
SOLUTION
INSERT INTO my_table
(SELECT distinct my_table.ID, 'process_name', my_table.ID
FROM my_table
WHERE my_table.ID NOT IN (SELECT distinct my_table.ID
FROM my_table
WHERE key = 'process_name'));
Assuming
distinct_idis not nullable, you could use:If
distinct_idis nullable, use NOT EXISTS instead: