SQL事务应用语句翻译

来源:百度知道 编辑:UC知道 时间:2024/05/15 09:39:27
begin transaction
use jiaoxuedb
go
declare @person_num tinyint,@cno char(5),@tno1 char(6),@tno2 char(6),@tno3 int
select @cno=cno from course where cname='数据结构'
select @person_num=count(*) from tc where cno=@cno
if @person_num>=2
begin /*不能招聘*/
rollback transaction /*回滚事务*/
print '因“数据结构”课程的任课人数已满,故程前老师不能再应聘该课程岗位!'
end
else
begin /*接受应聘*/
/*计算该老师的教师号*/
select @tno1=tno from teacher order by tno
select @tno3=@tno1+1
select @tno2=replace(@tno1,right(@tno1,len(@tno3)),@tno3)
/*把老师基本信息插入表teacher中*/
insert into teacher(tno,tname,sex,prof,dept)
values(@tno2,'程前','男','副教授','计算机')
/*把老师任课信息插入到表TC表中*/
insert into tc(tno,cno) values(@tno2,@cno)
commit /*提交事务*/
print '程前老师任聘课程“数据结构”成功!'
end
go

就是一个判断,如果输入的是数据结构就回滚,输出:'因“数据结构”课程的任课人数已满,故程前老师不能再应聘该课程岗位!'
如果输入的不是数据结构就 把老师任课信息插入表中,并输出程前老师任聘课程“数据结构”成功!',,,没有了