存储过程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基础整理(存储过程)的更多相关文章

  1. 必杀技———SQL基础整理系列(一)

    SQL(Structured Query Language)——结构化查询语言 SQL语言的组成部分 数据定义语言 (DDL:Data Definition Language) 负责数据结构定义与数据 ...

  2. 16、SQL基础整理(触发器.方便备份)

    触发器(方便备份) 本质上还是一个存储过程,只不过不是通过exec来调用执行,而是通过增删改数据库的操作来执行(可以操作视图) 全部禁用触发器 alter table teacher disable ...

  3. 11、SQL基础整理(变量)

    变量 定义变量:declare  @hello  varchar(20) 赋值:set  @hello = ‘你好’ select(结果框显示)/print(消息框显示) @hello *三行必须同时 ...

  4. 8、SQL基础整理(约束)

    约束 主键约束 防止在新增数据时出错,有约束性,起唯一标志的作用,在新增条目的时候防止不慎添加重复内容(不允许有null值) 1.  右键—设计—设置主键 2.在创建表格时设置 code int pr ...

  5. 5、SQL基础整理(字符串函数)

    字符串函数 ASCII 返回字符串首字母的ascii编码 select ASCII('name') select ASCII(name) from xuesheng select *from xues ...

  6. SQL基础分页存储过程(案例一)

    --分页 存储过程 案例 -- 所执行的存储过程 create proc pageForUsers @currPage int, --当前页数 @pageSize int, --每页多少条记录 @co ...

  7. 17、SQL基础整理(事务)

    事务 事务==流程控制 确保流程只能成功或者失败,若出现错误会自动回到原点 例: begin tran insert into student values('111','王五','男','1999- ...

  8. 15、SQL基础整理(视图)

    视图 即虚拟表 系统-右键-新建视图 编辑前200行 select *from studentscore 代码创建法: create view studentscore as select stude ...

  9. 13、SQL基础整理(流程控制begin……end)

    流程控制 begin ……end将一个语句块包含起来,中间可以写任何语句 格式: begin--开始 select *from student end--结束 if declare @bianlian ...

随机推荐

  1. kwicks插件学习

    /* Kwicks for jQuery (version 1.5.1) Copyright (c) 2008 Jeremy Martin http://www.jeremymartin.name/p ...

  2. jdk 安装

    安装JDK 选择安装目录 安装过程中会出现两次 安装提示 .第一次是安装 jdk ,第二次是安装 jre .建议两个都安装在同一个java文件夹中的不同文件夹中.(不能都安装在java文件夹的根目录下 ...

  3. asp.net 使用UrlRewritingNet.UrlRewriter组件URL重写,伪静态详解

    目录 URL重写的业务需求 ReWritingNet组件主要功能 配置IIS(IIS7/8环境下) 程序代码 重写规则 一,URL重写的业务需求 顾客可以直接用浏览器bookmark功能将页面连结储存 ...

  4. 转载 Javascript继承两种形式详解

    一直想对Javascript再次做一些总结,正好最近自己写了一个小型Js UI库,总结了一下Js的继承机制,在网上也看了一些前辈们博客里的总结,感觉分析不是特别全面.这里仅仅是把自己的学习体会拿出来分 ...

  5. python——使用readline库实现tab自动补全

    Input History readline tracks the input history automatically. There are two different sets of funct ...

  6. BZOJ2280 [Poi2011]Plot

    恩..这题真是sxbk 我们先二分答案,然后判断答案是否满足要求 判断方法是二分当前段的长度一直做到底,当然我们可以用倍增这样快一点,直接随机增量就可以了 然后就是卡常..... 然后就是卡精度QAQ ...

  7. loadrunner 参数化数据更新方式

    数据分配方式: Select next row[选择下一行]: 顺序(Sequential):按照参数化的数据顺序,一个一个的来取. 随机(Random):参数化中的数据,每次随机的从中抽取数据. 唯 ...

  8. PHP Memcached应用实现代码

    一.memcached 简介 在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东.这里简单介绍一下,memcached 是高 ...

  9. Understanding Virtual Memory

    Understanding Virtual Memory by Norm Murray and Neil Horman Introduction Definitions The Life of a P ...

  10. NIO基础

    通道和缓冲区 概述 通道 和 缓冲区 是 NIO 中的核心对象,几乎在每一个 I/O 操作中都要使用它们. 通道是对原 I/O 包中的流的模拟.到任何目的地(或来自任何地方)的所有数据都必须通过一个 ...