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

存储过程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 @[email protected]

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 [email protected] and shuxue>80

select @yuwen = count(*) from fenshu where [email protected] and yuwen>80

select @yingyu = count(*) from fenshu where [email protected] and yingyu>80

set @zongfen = @[email protected][email protected]

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 [email protected] and shuxue>80

select @yuwen = count(*) from fenshu where cod[email protected] and yuwen>80

select @yingyu = count(*) from fenshu where [email protected] and yingyu>80

set @zongfen = @[email protected][email protected]

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 = @[email protected]

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 @[email protected][email protected]

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

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 @[email protected][email protected]

print @count

时间: 2024-08-30 11:51:30

14、SQL基础整理(存储过程)的相关文章

Oracle实践--PL/SQL基础之存储过程

PL/SQL基础之存储过程 存储过程:过程是用于完成特定任务的子程序(代码的集合) /* 子程序的优点:1.模块化,将程序分解为逻辑模块: 2.可重用性,可以被任意数目的程序调用: 3,可维护性,简化维护操作: 4.安全性:通过设置权限,使数据更安全 */ --存储过程1,打印HelloWorld create or replace procedure my_pro is --用于声明一些变量 v_string varchar2(20) :='helloworld!'; begin dbms_o

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

SQL(Structured Query Language)——结构化查询语言 SQL语言的组成部分 数据定义语言 (DDL:Data Definition Language) 负责数据结构定义与数据库对象定义的语言,由CREATE.ALTER.DROP三个语法所组成,操作的对象包括关系表.视图.索引等. 数据操纵语言(DML: Data Manipulation Language) 其语句包括动词SELECT(查询).INSERT(添加).UPDATE(修改).DELETE(删除)表中的行.

SQL基础整理

SQL 是用于访问和处理数据库的标准的计算机语言. 什么是 SQL? SQL 指结构化查询语言 SQL 使我们有能力访问数据库 SQL 是一种 ANSI 的标准计算机语言 编者注:ANSI,美国国家标准化组织  SQL 能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的记录 SQL 可更新数据库中的数据 SQL 可从数据库删除记录 SQL 可创建新数据库 SQL 可在数据库中创建新表 SQL 可在数据库中创建存储过程 SQL 可在数据库中创建视图 S

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

1 --分页 存储过程 案例 2 3 -- 所执行的存储过程 4 create proc pageForUsers 5 @currPage int, --当前页数 6 @pageSize int, --每页多少条记录 7 @count int output --总记录数 8 as 9 declare @firstIndex int 10 declare @lastIndex int 11 declare @sqlText varchar(200) 12 13 --统计总记录数 14 select

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

触发器(方便备份) 本质上还是一个存储过程,只不过不是通过exec来调用执行,而是通过增删改数据库的操作来执行(可以操作视图) 全部禁用触发器 alter table teacher disable trigger all 全部开启触发器 alter table teacher enable trigger all create trigger TR_student_delete--默认触发器名 on student --在哪个表上操作 instead of delete --为了对表做什么而建

11、SQL基础整理(变量)

变量 定义变量:declare  @hello  varchar(20) 赋值:set  @hello = ‘你好’ select(结果框显示)/print(消息框显示) @hello *三行必须同时运行 declare @hello varchar(20) set @hello = '销售部' select *from bumen where name = @hello 当放到select 和from中间时,可以当做赋值语句,不执行查询功能(赋值为查询到的最后一行数据) declare @he

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

字符串函数 ASCII 返回字符串首字母的ascii编码 select ASCII('name') select ASCII(name) from xuesheng select *from xuesheng where ASCII(name)>=200 CHAR --将ascii代码转换成对应的字符 select CHAR(13)--回车键 CHARINDEX 在一个表达式中搜索另一个表达式,并返回其起始位置(如果没找到,返回’0’) select CHARINDEX('efg','abcde

8、SQL基础整理(约束)

约束 主键约束 防止在新增数据时出错,有约束性,起唯一标志的作用,在新增条目的时候防止不慎添加重复内容(不允许有null值) 1.  右键—设计—设置主键 2.在创建表格时设置 code int primary key, 3.可以设置自增长的功能 code int primary key identity(1,1) 4.在自增长的环境下删掉其中一行,其他行不受影响 5.可以加快查询的速度,减慢新增和修改的速度 外键约束 设计—关系—添加—表和列规范—需要有联系的两个列 主键需要先设置,然后在主键

15、SQL基础整理(视图)

视图 即虚拟表 系统-右键-新建视图 编辑前200行 select *from studentscore 代码创建法: create view studentscore as select student.sno,sname,ssex,sbirthday,class,cno,degree from student join score on student.Sno=score.sno 删除视图: drop view studentscore 修改视图: alter view cts as sel