IF-ELSE关键字
IF(条件)
语句或语句块
ELSE
语句或语句块
语句或语句块用BEGIN...END表示
IF(条件)
BEGIN
语句1
语句2
END
例如:
DECLARE @test INT
SET @test=1
SELECT @test=avg(score) FROM stu WHERE stu.name=‘lilu‘
IF(@test=1)
PRINT @test
ELSE
BEGIN
IF exists(SELECT score FROM stu WHERE stu.name=‘aa‘)
BEGIN
DELETE FROM stu WHERE stu.name=‘aa‘ and score=99
END
INSERT INTO stu VALUES(‘aa‘,99)
SELECT score FROM stu
WHERE name=‘aa‘
END
case关键字
case:
CASE
WHEN 条件1 THEN 结果1
WHEN 条件2 THEN 结果2
END
例如:
create table ss
(
name varchar(10),
score float
)
insert into ss values(‘小白‘,99)
insert into ss values(‘小黑‘,77)
insert into ss values(‘小李‘,58)
SELECT name,
score=CASE
WHEN score<60 THEN ‘E‘
WHEN score BETWEEN 60 AND 69 THEN ‘D‘
WHEN score BETWEEN 70 AND 79 THEN ‘C‘
WHEN score BETWEEN 80 AND 89 THEN ‘B‘
ElSE ‘A‘
END
FROM ss
WHILE 关键字
WHILE (条件)
语句或语句块
[BREAKE]
DECLARE @test INT
SET @test=2
WHILE (@test<20)
BEGIN
SET @[email protected]+2
IF(@test<>7)
BEGIN
PRINT @test
IF(@test=18)
BREAK
END
ELSE
IF (@test=7)
CONTINUE
END