Skip to content
Advertisement

how to use select into to insert many records

i have a stored procedure that will copy data from table A to table B and return id that is generated from table B to update table a record . this is sample data table A is :

Table B is :

i’ve tried the following i’ve defined a rec type

my problem is the select into will return many records , i believe my code will work if only return 1 record

Advertisement

Answer

The simplest solution is to use a Cursor loop:


Note: I have removed the unnecessary BEGIN … END notations. Only declare nested blocks when there’s real value in doing so.

In real life I would want to make this bulk operation more efficient by using collections and forall statements for a set-based approach.

Also I would consider whether I needed to lock the selected rows from A FOR UPDATE OF KPI, in case my whole transaction failed due to another session having a lock on one row. Doing this would allow you to use the WHERE CURRENT OF syntax when updating A.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement