if简单示例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'; 简单查询判断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 + '%'; endelse begin print @id + '%'; print @id + '#' + @pid; select * from ab_area where pid = @pid; endgo
2、 while…continue…break循环语句
基本语法
while begin [break] [continue] end
示例
--while循环输出到declare @i int; set @i = 1;while (@i < 11) begin print @i; set @i = @i + 1; endgo --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; endgo --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; endgo
3、 case
基本语法
case when then when then when then [else ]end
示例
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' endfrom ab_area;