自学内容网 自学内容网

陈志泊主编《数据库原理及应用教程第4版微课版》的实验题目参考答案实验6

  • 实验目的

1.能够使用流程控制语句完成简单程序的编写。

2.能够使用系统函数。

3.能够自定义简单地函数,并调用函数。

  • 实验设备

操作系统:Win11

开发软件:SQL Server

  • 实验内容

针对第6章设计的电子商务系统,做如下操作。

1.如果商品表中有价格在6000元以上的商品,把该商品的商品名称、商品类别、商品价格、生产厂家、商品的详细信息和商品的缩略图查询出来,否则输出“没有价格在6000元以上的商品”。

2.在商品表中,查询某种商品,如果有,就修改该商品的名称,并输出商品的信息,否则输出“没有该商品!”

3.查询商品购买信息,将商品的购买数量都加1(提示:使用流程控制语句while)。

4.定义一个用户自定义的函数,能够根据订单号,查询商品的购买数量,如果购买数量>2,输出订单号、商品名称和购买数量。

  • 实验步骤及实验结果
if exists(select* from Product where Price>=6000)
begin
 select ProductName,Category,Price,Manufacturer,ProductDetails,ThumbnailImage
 from Product where Price>=6000
 end
 else
 begin
 print '没有价格在6000元以上的商品'
 end
 declare @productname varchar(100)='iPhone 12'
 declare @newname varchar(100)='iPhone 20'
 if exists
 (
 select *from Product where ProductName='iPhone 12'
 )
 begin
 update Product set ProductName=@newname where ProductName=@productname
 select *from Product where ProductName=@newname
 end
 else
 begin
 print '没有该商品'
 end
 declare @cnt int =1
 declare @row int
 select @row=count(*) from PurchaseInfo
 while @cnt<=@row
 begin
 update PurchaseInfo set Quantity=Quantity+1
 where PurchaseID=@cnt
 set @cnt=@cnt+1
 end
 select*from PurchaseInfo
 create function dbo.findbyid(@id int)
 returns table
 as
 return
 (
 select PurchaseID,ProductName,Quantity from PurchaseInfo
 where PurchaseID=@id and Quantity>2
 )
 go
 select * from dbo.findbyid(1)


原文地址:https://blog.csdn.net/weixin_74027669/article/details/140524373

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!