Based on any previous record, we can get the donation we should make now:
Create procedure insertjzk_use
(
@ PFLAG int out, -- whether the flag has been issued
@ Pzzxq varchar (20)-funding for the semester
@ Sid varchar (20) -- Student ID
)
As
Declare @ zzje float
Declare @ ylje float
Declare @ slevel varchar (20)
If exists (select * From t_jzk_use where zzxq = @ pzzxq) -- whether this semester has been issued
Begin
Set @ PFLAG = 1
End
Else
Begin
Set @ PFLAG = 0
Select @ ylje = ylzzk from t_student where student_id = @ Sid -- find the reserved amount
If @ ylje <> 0 -- check whether the reserved amount is used up
Begin
If exists (select * From t_jzk_use where student_id = @ Sid)
Begin
Select @ zzje = Top 1 zzje from t_jzk_use where student_id = @ Sid order by student_id (top 1 is incorrect)
Insert into t_jzk_use (student_id, zzxq, zzje) values (@ Sid, @ pzzxq, @ zzje)
End
Else
Begin
Select @ slevel = school_level from t_student where student_id = @ Sid -- get the level
If @ slevel = 'Primary school' -- issue necessary funding based on the level
Set @ zzje = 120
Else if @ slevel = 'Junior high school'
Set @ zzje = 200
Else
Set @ zzje = 500
Insert into t_jzk_use (student_id, zzxq, zzje) values (@ Sid, @ pzzxq, @ zzje) -- the first donation
End
End
End