sqlserver------数据库的存储过程(练习)

本文涉及的产品
RDS SQL Server Serverless,2-4RCU 50GB 3个月
推荐场景:
云数据库 RDS SQL Server,基础系列 2核4GB
简介: sqlserver------数据库的存储过程(练习)

对于数据库的存储过程之前的专题有讲过


这里具体讲述存储过程的编写方法:


例题:有heat表和eatables两张表,分别为:


eatables



heat:protein(蛋白质),fat(脂肪),sugar(含糖量),kj(热量),以100克为单位



1.写一个存储过程Diet,以食物重量(克)为参数,输出该重量的不同类产品中,蛋白质成分最高的食物及其蛋白质含量。输出类似如下:


exec Diet 200


由于检索内容较为复杂:


所以先写检索内容:


s

elect name,a.type,max
  from(select type,max(protein)*(@num/100) max from eatables,heat
  where heat.id = eatables.id
  group by type)p,
  eatables as a,heat as b
  where b.protein*(@num/100)=p.max and a.id=b.id

接着加上存储过程:


create procedure Diet(
  @num int
)
as
begin
  declare @name varchar(10),@type varchar(10),@max decimal(8,2)
  declare diet_cursor cursor for
  select name,a.type,max
  from(select type,max(protein)*(@num/100) max from eatables,heat
  where heat.id = eatables.id
  group by type)p,
  eatables as a,heat as b
  where b.protein*(@num/100)=p.max and a.id=b.id
open diet_cursor
fetch next from diet_cursor into @name,@type,@max
while @@FETCH_STATUS=0
begin
print '在' +@type+'食品中'
print '每'+convert(varchar,@num)+'克'+@name+'含有的蛋白质最高,达到'+convert(varchar,@max)+'克'
fetch next from diet_cursor into @name,@type,@max
end
close diet_cursor
deallocate diet_cursor
end

输入


exec Diet 200




2.写一个存储过程HeatReport,找出平均糖含量最高的食物及其糖含量。输出类似如下:

exec HeatReport;


先写检索过程:


select top 1 type,avg_sugar from
  (select type,sum(sugar)/count(1) as avg_sugar from eatables,heat
  where eatables.id=heat.id
  group by type)p
  order by p.avg_sugar desc

再写存储过程


create procedure HeatReport
as
begin
  declare @type varchar(10),@avg_sugar decimal(8,2)
  declare diet_cursor cursor for
  select top 1 type,avg_sugar from
  (select type,sum(sugar)/count(1) as avg_sugar from eatables,heat
  where eatables.id=heat.id
  group by type)p
  order by p.avg_sugar desc
  open diet_cursor
  fetch next from diet_cursor into @type,@avg_sugar
  while @@FETCH_STATUS=0
  begin
  print'在面类,米面类,水产类食品中'
  print @type+'的平均糖含量最高,达到每100克食物含有'+convert(varchar,@avg_sugar)+'克'
  fetch next from diet_cursor into @type,@avg_sugar
  end
close diet_cursor
deallocate diet_cursor
end

输入

exec HeatReport


得:


相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情: https://www.aliyun.com/product/rds/sqlserver
目录
相关文章
|
24天前
|
SQL 数据库
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
SQL Server附加数据库出现错误823,附加数据库失败。数据库没有备份,无法通过备份恢复数据库。 SQL Server数据库出现823错误的可能原因有:数据库物理页面损坏、数据库物理页面校验值损坏导致无法识别该页面、断电或者文件系统问题导致页面丢失。
88 12
数据库数据恢复—SQL Server数据库报错“错误823”的数据恢复案例
|
1天前
|
存储 SQL 数据库
SQL Server 临时存储过程及示例
SQL Server 临时存储过程及示例
15 3
|
4天前
|
存储 SQL 关系型数据库
MySql数据库---存储过程
MySql数据库---存储过程
17 5
|
19天前
|
SQL 关系型数据库 MySQL
创建包含MySQL和SQLServer数据库所有字段类型的表的方法
创建一个既包含MySQL又包含SQL Server所有字段类型的表是一个复杂的任务,需要仔细地比较和转换数据类型。通过上述方法,可以在两个数据库系统之间建立起相互兼容的数据结构,为数据迁移和同步提供便利。这一过程不仅要考虑数据类型的直接对应,还要注意特定数据类型在不同系统中的表现差异,确保数据的一致性和完整性。
26 4
|
1月前
|
SQL 存储 数据管理
SQL Server数据库
SQL Server数据库
43 11
|
1月前
|
SQL 安全 数据库
基于SQL Server事务日志的数据库恢复技术及实战代码详解
基于事务日志的数据库恢复技术是SQL Server中一个非常强大的功能,它能够帮助数据库管理员在数据丢失或损坏的情况下,有效地恢复数据。通过定期备份数据库和事务日志,并在需要时按照正确的步骤恢复,可以最大限度地减少数据丢失的风险。需要注意的是,恢复数据是一个需要谨慎操作的过程,建议在执行恢复操作之前,详细了解相关的操作步骤和注意事项,以确保数据的安全和完整。
76 0
|
SQL 数据库
如何快速备份还原Sql Server 数据库
备份数据库 选择你要备份的数据库,鼠标右键单击,选择任务-备份   弹出备份数据库窗口,选择添加    弹出选择备份目标窗口,点击浏览,选择存放备份数据库的目录,输入文件名,后缀名输入.bak,点击确定,确定,备份完成     还原数据库  鼠标右键单击数据库,选择还原文件和文件   ...
1351 0