07_05 插入新课程的记录

CREATE PROCEDURE usp_insert_subject
    @SubjectNo int OUTPUT,
    @GradeId int OUTPUT,
	@GradeName varchar(50),
	@SubjectName varchar(50),
    @ClassHour int = 36
AS 
    DECLARE @errNum int
	SET @errNum = 0
	
	IF (LEN(RTRIM(@SubjectName))=0 OR LEN(RTRIM(@GradeName))=0)
		RETURN -1

    BEGIN TRANSACTION
    IF NOT EXISTS(SELECT * FROM Grade WHERE GradeName = @GradeName)
	  BEGIN
		INSERT INTO Grade (GradeName) VALUES (@GradeName)
		SET @errNum = @errNum + @@ERROR
		SELECT @GradeId=@@IDENTITY
	  END
	ELSE
		SELECT @GradeId=GradeId FROM Grade WHERE GradeName = @GradeName

	INSERT INTO Subject (SubjectName,ClassHour,GradeId) 
		VALUES (@SubjectName,@ClassHour,@GradeId)
	SET @errNum = @errNum + @@ERROR

	SELECT @SubjectNo=@@IDENTITY

	IF (@errNum > 0)
	  BEGIN
		ROLLBACK TRANSACTION
		RETURN 0 
	  END
	ELSE
	  BEGIN
		COMMIT TRANSACTION
		RETURN 1
	  END
GO

--调用存储过程
DECLARE @SubjectNo int
DECLARE @GradeId int
DECLARE @GradeName varchar(50)
DECLARE @SubjectName varchar(50)
DECLARE @ClassHour int
DECLARE @rt int

SET @GradeName = 'Y2'
SET @SubjectName = 'Linux'
SET @ClassHour = 10
EXEC @rt=usp_insert_subject @SubjectNo OUTPUT,@GradeId OUTPUT,@GradeName,@SubjectName,@ClassHour
IF (@rt = 1)
  BEGIN
	PRINT '增加课程'+@SubjectName+'记录成功'
	PRINT '学期编号是' + CAST(@GradeId AS varchar(10)) + ',学期名称是' + @GradeName
	PRINT '课程编号是' + CAST(@SubjectNo AS varchar(10)) + ',课程名称是' + @SubjectName
  END
ELSE if (@rt = 0)
	PRINT '增加课程记录失败!'
ELSE
	PRINT '学期名称或课程名称不能为空,请重新执行!'
GO

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值