USING GOTO statement in PL/SQL Block
GOTO statement is used to branch a section of code unconditionally.
SYNATX:
Goto Label;
<<Label>>
begin
end;
Note: a branch can be forward/backward.
Here You have a table called Student
create table student2(id number(2),
name varchar2(20),
constraint PK_ID primary key(ID)
)
name varchar2(20),
constraint PK_ID primary key(ID)
)
Insert 2 records into Student
begin
for i in 1..2
loop
insert into student2 values(i,concat('student ',i));
dbms_output.put_line('inserted...');
end loop;
end;
for i in 1..2
loop
insert into student2 values(i,concat('student ',i));
dbms_output.put_line('inserted...');
end loop;
end;
SQL> select * from student2;
ID NAME
---------- --------------------
1 student 1
2 student 2
ID NAME
---------- --------------------
1 student 1
2 student 2
Again user wants to add records from the begining, it will throw an exception
using GOTO statement avoid those exceptions continue inserting remaning items.
begin
for i in 1..5
loop
<<Label>>
begin
insert into student2 values(i,concat('student ',i));
exception
when others then
goto Label;
end;
end loop;
end;
for i in 1..5
loop
<<Label>>
begin
insert into student2 values(i,concat('student ',i));
exception
when others then
goto Label;
end;
end loop;
end;
The above code throws exception for ID 1,2, using goto statement ignore those 2 records and continue remaining 3 records
SQL> select * from student2;
ID NAME
---------- --------------------
1 student 1
2 student 2
3 student 3
4 student 4
5 student 5
ID NAME
---------- --------------------
1 student 1
2 student 2
3 student 3
4 student 4
5 student 5
Tags:USING GOTO statement in PL/SQL Block, Oracle GOTO statement, PL/SQL goto statement syntax, usage of GOTO Statement in PL/SQL, How to use goto statement in PL/SQL block
Looks like this code has an issue it never comes out..
ReplyDelete