(十二)逻辑控制语句

     大家好,今天给大家分享下SQL Server相关知识,本教程内容全部由本人亲测有效才放出来,网友们可以自己在本地自行练习。写此教程的目的也是希望可以加深自己对数据库操作语句的理解,同时也帮助到初学者,这里主要以代码的形式帮助大家动手练习,全文没有过多言语,也是为了简化大家对文字阅读的繁琐,最后希望能帮助到大家,喜欢就点个赞+关注,欢迎一起学习交流! 

--(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 '定时三秒后执行';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_6o1

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值