首页 > 其他 > 详细

分支语句及游标的使用测试

时间:2017-02-24 16:00:56      阅读:231      评论:0      收藏:0      [点我收藏+]

--1 查询是否有“拉尔森”这个顾客,若有,则显示该顾客的信息,没有,则作出相应提示。
if exists(select * from [dbo].[购物者] where [名]=‘拉尔森‘)
begin
 select * from [dbo].[购物者] where [名]=‘拉尔森‘
end
else
begin
 print(‘没有此顾客‘)
end
--2 需要获得代码为“000003”的货物的运送状态,如果该批货物已经投递,则显示消息“货物已经到达”,
--否则显示消息“货物尚未到达”
--(提示:如果已经投递,则运输状态属性值为“d”,未投递为“s”)。
select case [运送状态] when ‘d‘ then ‘货物已经到达‘
        when ‘s‘ then ‘货物尚未到达‘
    end as [运送状态]
 from [dbo].[运输情况] where [定单号]=‘000003‘
 --3 一个新的订单信息如下:
--insert into 定单详情
--values(‘000011’,‘000008’,100,‘N’,NULL,NULL,14.99)
--但是需要先检查玩具的库存是否充足,如果库存量不足,则给出相应的提示,并拒绝新记录的增加。
if(select [数量] from [dbo].[玩具] where [玩具号]=‘000008‘)<100
begin
 print(‘此货物库存不足!‘)
end
else
begin
 insert into 定单详情 values (‘000011‘,‘000008‘,100,‘N‘,NULL,NULL,14.99)
end
--4 统计各个玩具的库存情况,按以下格式显示
--玩具号  玩具名   库存量   状态
--其中状态取决于,若库存量在80以上,状态则为“充足”,在60到80之间,状态则为“一般”,否则为“不足”。
select [玩具号],[玩具名],[数量],
    case
     when [数量] > 80 then ‘充足‘
     when [数量]>=60 and [数量]<=80 then ‘一般‘
     else ‘不足‘ end as [状态]
from [dbo].[玩具]
--5修改各包装的价格,若价格金额达到2或以上,则增加1,在1.5和2之间,则增加1.5,否则增加2。
declare @price as decimal(5,2),@id as varchar(10)
declare cur cursor
for select [包装号],[包装价格] from [dbo].[包装]
open cur
fetch next from cur into @id,@price
while @@FETCH_STATUS=0
begin
 if(@price)>=2
 begin
  set @price=@price+1
 end
 else if(@price)>=1.5 and @price<2
 begin
  set @price=@price+1.5
 end
 else
 begin
  set @price=@price+2
 end
 update [dbo].[包装] set [包装价格]=@price where [包装号]=@id
 fetch next from cur into @id,@price
end
close cur
deallocate cur
--6 显示一份格式化报表,格式如下:
--购物者号:000002                 购物者名: 拉尔森
--订单号          玩具号           数量
--————       ————         ————
--0000002        000016            2
--0000009        000018            1
--0000010        000020            2
--0000010        000021            1
--购物者号:000003                 购物者名: 史密斯
--订单号          玩具号           数量
--————       ————         ————
--0000001        000016            2
--0000003        000018            1
declare @sql varchar(100),@nid varchar(10),@nname varchar(30)
declare cur1 cursor for select [购物者号],[名] from [dbo].[购物者]
open cur1
fetch next from cur1 into @nid,@nname
while @@FETCH_STATUS=0
begin
 set @sql=‘购物者号:‘+@nid+‘   购物者名:‘+@nname
 print @sql
 if exists(select [定单号],[玩具号],[购买数量] from [dbo].[定单详情] where [定单号] in (select [定单号] from [dbo].[定单] where [购物者号]=@nid))
 begin
 print ‘订单号          玩具号         数量‘
 print ‘--———       ————      ————‘
 declare @sql2 varchar(1000),@did varchar(10),@wid varchar(10),@num varchar(10)
 declare cur2 cursor for select [定单号],[玩具号],[购买数量] from [dbo].[定单详情] where [定单号] in (select [定单号] from [dbo].[定单] where [购物者号]=@nid)
 open cur2
 fetch next from cur2 into @did,@wid,@num
 while @@FETCH_STATUS=0
 begin
  set @sql2=@did+‘          ‘+@wid+‘         ‘+@num
  print @sql2
  fetch next from cur2 into @did,@wid,@num
 end
 close cur2
 deallocate cur2
 end
 else
 begin
  print ‘此人无订单!‘
 end
 fetch next from cur1 into @nid,@nname
end
close cur1
deallocate cur1

分支语句及游标的使用测试

原文:http://www.cnblogs.com/zhouqiang0701/p/6438883.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!