(十二)逻辑控制语句
--(1)if-else 判断语句
--简单示例
if 2>3
print '2>3';
else
print '2<3';
if(2>3)
print '2>3';
else if(3>2)
print '3>2';
else
print 'other';
--简单查询判断(注意这里是相同类型,可以把address换成int类型
declare @id char(10),
@pid char(20),
@name varchar(20);
set @name = '广州';
select @id = id from ab_area where areaName = @name;
select @pid = pid from ab_area where id = @id;
print @id + '#' + @pid;
if @pid > @id
begin
print @id + '%';
select * from ab_area where pid like @id + '%';
end
else
begin
print @id + '%';
print @id + '#' + @pid;
select * from ab_area where pid = @pid;
end
go
--(2)while...continue...break循环语句
declare @i int;
set @i = 1;
while (@i < 11)
begin
print @i;
set @i = @i + 1;
end
go
--while continue 输出到
declare @i int;
set @i = 1;
while (@i < 11)
begin
if (@i < 5)
begin
set @i = @i + 1;
continue;
end
print @i;
set @i = @i + 1;
end
go
--while break 输出到
declare @i int;
set @i = 1;
while (1 = 1)
begin
print @i;
if (@i >= 5)
begin
set @i = @i + 1;
break;
end
set @i = @i + 1;
end
go
--(3)case
select *,
case sex
when 1 then '男'
when 0 then '女'
else '火星人'
end as '性别'
from student;
select areaName, '区域类型' = case
when areaType = '省' then areaName + areaType
when areaType = '市' then 'city'
when areaType = '区' then 'area'
else 'other'
end
from ab_area;
--(4)其他语句
--批处理语句go
Use master
Go
--延时执行,类似于定时器、休眠等
waitfor delay '00:00:03';--定时三秒后执行
print '定时三秒后执行';
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了