热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

14、SQL基础整理(存储过程)

存储过程procedure(proc)数据库—可编程性—存储过程新建存储过程:createprocfirstprocasselect*fromfenshugo执行存储过程:存储过程
存储过程procedure(proc)

数据库—可编程性—存储过程

新建存储过程:

create proc firstproc

as

select *from fenshu

go

执行存储过程:

存储过程—右键—执行存储过程

declare @fanhuizhi  int

exec @fanhuizhi = firstproc

select ‘Return Value‘ = @fanhuizhi

return  value执行成功/失败的返回值(0为成功)

execute/exec firstproc

修改存储过程

alter proc firstproc

as

select yuwen,shuxue,yingyu,name from student,fenshu where student.code = fenshu.code

go

execute firstproc

查询多个表

create proc secondproc

as

begin

  select*from student

  select*from teacher

  select*from fenshu

end

带子查询的查询过程

create proc threeproc

as

begin

  declare @count int

  select @count=count(*) from (select *from student where code = any(select code from fenshu where code in

  (select code from student where MT = ( select code from teacher where name = ‘数一‘))and shuxue>80)

  )as new

  if @count> 3

    print‘达标‘

  else

    print‘不达标‘

end

exec threeproc

 

带参数的查询过程

create proc fourthproc

@hello varchar(20),

@ercan varchar(20)—参数部分

as

begin

    print @hello+@ercan

end

go

exec fourthproc ‘helloworld‘,‘,你好世界!‘

复杂参数

create proc fifthproc

@name varchar(20)

as

begin

    declare @countjs int,@lesson varchar(20)

    select @countjs = COUNT(*)from teacher where name = @name

    if @countjs = 0

    begin

       print ‘没找到这个老师‘

    end

    else

    begin

      select @lesson = course from teacher where name = @name

      declare @count int

      if @lesson = ‘语文‘

        begin

        select @count= count(*)from student where code = any(select code from fenshu where code in

        (select code from student where CT = ( select code from teacher where name = @name))and yuwen>80)

        end

      else

      if @lesson = ‘数学‘

        begin

             select @count= count(*)from student where code = any(select code from fenshu where code in

             (select code from student where MT = ( select code from teacher where name = @name))and shuxue>80)

        end

      else

      if @lesson = ‘英语‘

        begin

             select @count= count(*)from student where code = any(select code from fenshu where code in

             (select code from student where ET = ( select code from teacher where name = @name))and yingyu>80)

        end

      if @count>=3

        print ‘达标‘

      else

        print ‘不达标‘

    end

end

go

exec fifthproc (@name=可忽略)‘数一‘

exec fifthproc ‘数‘

删除存储过程

drop proc 存储过程名

练习

------------输入学生的学号,看是否结业(三门课都超过分发优秀证书,有两门课或以上不及格不结业,有一门课不及格结业)------------

 

alter proc jieye

@xuehao int

as

begin

    declare @shuxue int

    declare @yuwen int

    declare @yingyu int

    declare @zongfen int

   

    select @shuxue = count(*) from fenshu where code=@xuehao and shuxue>80

    select @yuwen = count(*) from fenshu where code=@xuehao and yuwen>80

    select @yingyu = count(*) from fenshu where code=@xuehao and yingyu>80

   

    set @zOngfen= @shuxue+@yuwen+@yingyu

    if @zOngfen= 3

    print ‘优秀‘

    if @zOngfen= 2

    print ‘结业‘

    if @zongfen <2

    print ‘不结业‘

end

go

 

返回值

-----------输入一个数,使其+10返回------定义变量接收执行存储过程返回的值

create proc jisuan

@sum int(可以设默认值@sum int=10)

as

begin

    set @sum = @sum +10

    return @sum

end

declare @shu int

exec @shu = jisuan 2(把2改为default即可按照默认值的输出结果打印)

print @shu

---------返回总分的个数并打印出来-----------

create proc jieye2

@xuehao int

as

begin

    declare @shuxue int

    declare @yuwen int

    declare @yingyu int

    declare @zongfen int

   

    select @shuxue = count(*) from fenshu where code=@xuehao and shuxue>80

    select @yuwen = count(*) from fenshu where code=@xuehao and yuwen>80

    select @yingyu = count(*) from fenshu where code=@xuehao and yingyu>80

   

    set @zOngfen= @shuxue+@yuwen+@yingyu

    return @zongfen

end

declare @count int

exec @count = jieye2 1

print @count

return后面的存储过程将不再运行(放在if等句子里的例外)

练习

--输入一个数n,求n+n-1+……+1的和---

alter proc qiuhe

@n int

as

begin

    declare  @sum int =0

    while @n>=1

    begin

       set @sum = @n+@sum

       set @n = @n-1

    end

    return @sum

end

go

declare @n1 int

exec @n1 = qiuhe 5

print @n1

--------带返回值,返回参数,输入参数的存储过程--------

---输入学号,返回三门课的成绩

create proc sixproc

@yuwen decimal(18,2) output,

@shuxue decimal(18,2) output,

@yingyu decimal(18,2) output,

@code int

as

begin

    declare @count int  

    select @yuwen = yuwen,@shuxue = shuxue,@yingyu = yingyu from fenshu where code = @code

end

go

---定义变量接受存储过程带出来的输出参数的值

declare @yuwen decimal(18,2),@yingyu decimal(18,2),@shuxue decimal(18,2)

exec sixproc @yuwen output,@shuxue output, @yingyu output,1

print @yuwen+@yingyu+@shuxue

----------------------------------------------

alter proc sixproc

@yuwen decimal(18,2) output,

@shuxue decimal(18,2) output,

@yingyu decimal(18,2) output,

@code int

as

begin

    declare @count int

    select @count = COUNT(*)from student where code = @code

    select @yuwen = yuwen,@shuxue = shuxue,@yingyu = yingyu from fenshu where code = @code

    return @count

end

go

 

declare @yuwen decimal(18,2),@yingyu decimal(18,2),@shuxue decimal(18,2),@count int

exec @count = sixproc @yuwen output,@shuxue output, @yingyu output,15

print @yuwen+@yingyu+@shuxue

print @count

14、SQL基础整理(存储过程)


推荐阅读
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
  • 本文内容为asp.net微信公众平台开发的目录汇总,包括数据库设计、多层架构框架搭建和入口实现、微信消息封装及反射赋值、关注事件、用户记录、回复文本消息、图文消息、服务搭建(接入)、自定义菜单等。同时提供了示例代码和相关的后台管理功能。内容涵盖了多个方面,适合综合运用。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
author-avatar
Amyb__ing舒
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有