I need to create a stored procedure to insert data into one table from another based on some conditions. In the existing table, if two columns (primary role and secondary role) have the same value, then I want just one row in the new table with role as primary role.
In case if a row in the old table has different values in primary role and secondary role, I want two rows in the new table, one having the value of role as primary role of old table, and another as secondary.
What is the best way to achieve this?
Right now my query looks something like this
create procedure proc as
begin
insert into newTable values(role)
select primary_role as role from oldTable
where primary_role = secondary_role
end
This does not handle the case where primary role is not the same as secondary role.
Sample
sample row oldTable
PrimaryRole | SecondaryRole | Name
admin | analyst | Sara
sample row newTable
Role | Name
admin | Sara
analyst | Sara