优化Myschool数据库设计第三章 使用IF...else
USE MySchool
GO
--示例1:查找与李文才学号相邻的同学信息
DECLARE @StudentNo int --定义变量保存李文才的学号
SELECT @StudentNo = StudentNo FROM Student --使用SELECT为变量赋值
WHERE StudentName = '李文才'
SELECT StudentNo, StudentName, BornDate, Address
FROM Student
WHERE (StudentNo = @StudentNo+1) OR (StudentNo = @StudentNo-1)
GO
--练习1:--查询java logic的课程编号
DECLARE @SubjectNo int
SELECT @SubjectNo = SubjectNo From Subject where SubjectName = 'java logic'
--查询获得"JAVA Logic"课程最近一次考试的日期
DECLARE @date datetime --考试日期
SELECT @date = MAX(ExamDate) FROM Result
WHERE SubjectNo = @SubjectNo
--查询java Logic课程最近一次考试平均分
DECLARE @result float --平均分
SELECT @result = AVG(StudentResult)
FROM Result
WHERE ExamDate=@date
AND SubjectNo = @SubjectNo
PRINT @result --输出平均分
--示例2:获取服务器名称
PRINT '服务器的名称: ' + @@SERVERNAME
PRINT 'SQL Server的版本' + @@VERSION
SELECT @@SERVERNAME AS '服务器名称'
SELECT @@VERSION AS 'SQL Server的版本'
--示例3:使用全局变量获得SQL语句错误号
INSERT INTO Grade(GradeId,GradeName) VALUES(4,'Y3')
print @@ERROR --如果@@ERROR大于0,表示上一条语句执行有错误
INSERT INTO Grade(GradeName) VALUES('Y3')
print @@ERROR --如果@@ERROR等于0,表示上一条语句执行正确
--示例4:数据类型转换
DECLARE @age int
SET @age = 17
PRINT '年龄:' + Convert(varchar(5),@age) --使用Convert函数转换
PRINT '年龄:' + Cast(@age as varchar(5)) --使用Cast函数转换
USE MySchool
GO
SELECT StudentName + '的出生日期是:' + CAST(BornDate as varchar(50))
FROM Student
SELECT StudentName + '的出生日期是:' + Convert(VARCHAR(50),BornDate,102) --cast函数可以指定日期格式
FROM Student
--示例5:使用IF...ELSE语句
USE MySchool
GO
DECLARE @SubjectNo int
SELECT @SubjectNo = SubjectNo From Subject where SubjectName = 'java logic'
--查询获得"JAVA Logic"课程最近一次考试的日期
DECLARE @date datetime --考试日期
SELECT @date = MAX(ExamDate) FROM Result
WHERE SubjectNo = @SubjectNo
--查询java Logic课程最近一次考试平均分
DECLARE @result float --平均分
SELECT @result = AVG(StudentResult)
FROM Result
WHERE ExamDate=@date
AND SubjectNo = @SubjectNo
PRINT '平均分:' + Convert(varchar(5),@result)
--根据平均分输出结果
IF(@result>70)
BEGIN
PRINT '考试成绩优秀,前三名的成绩为'
SELECT TOP 3 *FROM Result
WHERE SubjectNo = @SubjectNo and ExamDate = @date
ORDER BY StudentResult DESC
END
ELSE
BEGIN
PRINT '考试成绩较差,后三名的成绩为'
SELECT TOP 3 *FROM Result
WHERE SubjectNo = @SubjectNo and ExamDate = @date
ORDER BY StudentResult ASC
END
--示例6:使用while循环语句
--查询winforms课程编号
DECLARE @subNO int
SELECT @subNo=SubjectNo FROM Subject
WHERE SubjectName='Winforms'
--查询winforms最近考试日期
DECLARE @examdate datetime
SELECT @examdate=max(ExamDate) FROM Result
WHERE SubjectNo=@subNO
DECLARE @n int --不及格人数
WHILE(1=1) --条件永远成立
BEGIN
SELECT @n=COUNT(*) FROM Result
WHERE SubjectNo=@subNO AND ExamDate=@examdate AND StudentResult<60 --统计不及格人数
IF (@n>0) --每人加2分
UPDATE Result SET StudentResult=StudentResult+2 FROM Result
WHERE SubjectNo=@subNO AND ExamDate=@examdate AND StudentResult<95
ELSE
BREAK --退出循环
END
PRINT '加分后的成绩如下:'
SELECT * FROM Result
WHERE SubjectNo=@subNO AND ExamDate=@examdate
--示例7:使用case-end语句
SELECT 学号=StudentNo, 成绩=
CASE
WHEN StudentResult < 60 THEN 'E'
WHEN StudentResult BETWEEN 60 AND 69 THEN 'D'
WHEN StudentResult BETWEEN 70 AND 79 THEN 'C'
WHEN StudentResult BETWEEN 80 AND 89 THEN 'B'
ELSE 'A'
END
FROM Result
--示例8:批处理命令
use MySchool
go
create table temp1
(
myint int
)
go
insert intos temp1 values('abc')
go
create table temp2
(
myint int
)
go
insert into temp2 values('abc')