Monday, March 2, 2009

Update If Found; Insert If Not Found

Declare
v_NewMajor Varchar2(10) := 'Nutrition' ;
v_LastName Varchar2(10) := 'Tobias' ;

Begin
/*----------------------------*/
/* Update The Students Table. */
/*----------------------------*/
Update Students
Set Major = v_NewMajor
Where Last_Name = v_LastName ;

/*----------------------------------------*/
/* If not found, then insert this record. */
/*----------------------------------------*/
If Sql%Notfound
Then Insert Into Students (Id,Last_Name, Major)
Values (Student_Sequence.Nextval,
v_LastName, v_NewMajor);
End If;
End;
/

No comments:

Post a Comment