数据库学习笔记(十一)



--IF语句
Declare @point as int
Set @point = 87
IF @point >= 60
begin
PRINT 'pass, very goog!'
end
ELSE
begin
PRINT 'no pass , try again!' 
END






--BEGIN END语句
DECLARE @MyVar float
Set @MyVar = 5.7
IF @MyVar > 10.8
BEGIN
SET @MyVar = 123.456
PRINT '变量@MyVar的值为: '
PRINT CAST(@MyVar AS VARCHAR(12))
END
ELSE
PRINT CAST(@MyVar AS varchar(12))








--WHILE语句
DECLARE @count as INT
SET @count  = 0
WHILE EXISTS(select * from student where point > 800)
BEGIN
SET @count = @count + 1
BREAK
END
PRINT '入学成绩 > 800 分的至少有: ' + CAST(@count AS CHAR(2)) + '人'








--使用CASE语句
SELECT tname AS '姓名', department AS '院系',
CASE prof
WHEN '教授' THEN '高级'
WHEN '副教授' THEN '高级'
WHEN '讲师' THEN '中级'
WHEN '助教' THEN '初级'
END 
AS '职称类别'
FROM teacher
where department = '计算机学院'
GO




--CASE 语句举例
SELECT studentno as '学号', courseno as '课程',
CASE
WHEN usually * 0.2 + final * 0.8 >= 90 THEN '优秀'
WHEN usually * 0.2 + final * 0.8 >= 80 THEN '良好'
WHEN usually * 0.2 + final * 0.8 >= 70 THEN '中等'
WHEN usually * 0.2 + final * 0.8 >= 60 THEN '及格'
WHEN usually * 0.2 + final * 0.8 <  60 THEn '不及格'
END
AS '总评成绩'
FROM score
WHERE courseno in ('c06208', 'c08106', 'c05109')
GO


--批处理语句GOTO这里的使用类似于循环
declare @sum int, @count int
select @sum = 0, @count = 0
label_1:
select @count = @count + 1
select @sum = @sum + @count
if @count <= 6
goto label_1
select @count as '数值', @sum as '1-7累加和'




--waitfor:等到多长时间才执行
BEGIN
WAITFOR TIME '22:20:17'
EXECUTE sp_help
END



你可能感兴趣的:(数据库)