数据库(存储过程)

---------------存储过程
--创建存储过程
create proc firstproc
as
select *From score --中间放的代码都可以执行出来
go
--执行存储过程
exec firstproc
--定义变量接收存储过程默认的返回值,可判定执行成功或失败
declare @fanhuizhi int
exec @fanhuizhi = firstproc
select @fanhuizhi

----------修改存储过程
alter proc firstproc
as
select score.code,chiese,math,english,name from score,student where score.stucode=student.code

go
exec firstproc

-----------查询多个表
create proc secondproc
as
begin
select *from score
select *from student
select *from teacher
end
go
exec secondproc

----------加语句的存储过程
create proc threeproc
as
begin
declare @count int
select @count=count(*) from score where stucode in (
select code from student where mateacher=(select code from teacher where name=‘张三丰‘)
) and math>=80
if @count>3
print ‘达标‘
else
print ‘不达标‘
end
go
exec threeproc

----------------带参数的存储过程
alter proc fourproc
@hello varchar(50),
@ercan varchar(50)
as
begin
print @[email protected]
end
go
exec fourproc ‘hello world!‘,‘这是第二个参数‘

--------带语句带参数的存储过程
alter proc fourproc
@name varchar(20)
as
begin
-- declare @jscode int,@kecheng varchar(20)
declare @count int,@kecheng varchar(20)
select COUNT(*) from teacher where [email protected]
--select @jscode=code,@kecheng=course from teacher where [email protected]

declare @count int

if @kecheng=‘语文‘
begin
select @count=count(*) from score where stucode in (
select code from student where chteacher=(select code from teacher where [email protected])
) and chiese>=80
end
if @kecheng=‘数学‘
begin
select @count=count(*) from score where stucode in (
select code from student where mateacher=(select code from teacher where [email protected])
) and math>=80
end
if @kecheng=‘英语‘
begin
select @count=count(*) from score where stucode in (
select code from student where enteacher=(select code from teacher where [email protected])
) and english>=80
end

if @count>=3
begin
print ‘达标‘
end
else
begin
print ‘不达标‘
end

end
go
exec fourproc ‘莫言‘

---输入学生的学号,判断学生结业与否 三门课优秀证书结业两门课不及格结页一门不结业
alter proc xuesheng
@xuehao int
as
begin

declare @yuwen int,@shuxue int,@yingyu int,@buhui int
select @yuwen=COUNT(*) from score where [email protected] and chiese>60
select @shuxue=COUNT(*) from score where [email protected] and math>=60
select @yingyu=COUNT(*) from score where [email protected] and english>=60
end
set @[email protected][email protected]+ @yingyu
if @buhui=3
print ‘优秀‘
end

go

exec xuesheng

-------------使用return返回值的储存过程
create proc jisuan

@sum int=10
as
begin
set @[email protected]+10
return @sum
end
go

--定义变量接收执行储存过程返回的值
declare @shu int
exec @shu=jisuan default
print @shu
---------------

--
create proc jiecheng
@al int
as
begin
declare @i int=0, @sum int=0
while @i<[email protected]
begin
set @[email protected][email protected]
set @[email protected]+1
end
return @sum
end
go
declare @sum int
exec @sum=jiecheng 5
print @sum

--------带返回值,返回参数,输入参数的储存过程
select *from score--输入学号返回三门课的成绩
create proc sixproc
@code int,
@yuwen decimal(18,2) output--输出参数
@shuxue decimal(18,2)output
@yingyu decimal(18,2) output

as
begin
declare @count int
select @count=COUNT(*)from student where [email protected]
select @yuwen =chiere,@shuxue=math,@yingyu=english from score where [email protected] *from score where [email protected]
return @count
end
go

--定义变量接收存储过程带出来的输出参数的值
declare @yuwen decimal(18,2),@yingyu decimal(18,2),@shuxue decimal(18,2)
exec sixproc 1,@yuwen output,@shuxue output,@yingyu output
print @[email protected][email protected]
print @count

时间: 2024-08-07 00:17:01

数据库(存储过程)的相关文章

索引、视图、SQL优化以及数据库存储过程

一.索引 索引是查询优化最有效和最常用的技术 索引是一个单独的.物理的数据库结构,它是指向表中某一列或若干列上的指针列表. mysql中,一个表的物理存储由两部分组成,一部分用于存放表的数据,另一部分存放索引,当进行数据搜索时,mysql会首先搜索索引,从中找到所需数据的起始位置的指针,再直接通过指针查找目标数据. 1.创建索引: CREATE INDEX 索引名 on 表名(要添加索引的列名) 可以给一个表中的多个列添加索引 通过在查询sql语句前加一句Explain可以分析索引效率, 有这样

SQL数据库&mdash;&mdash;存储过程

SQL数据库--存储过程 语法格式: use 数据库名 在存储过程第一行就要首先声明所在数据库 go create(alter) proc 存储过程名 形参(@-), - as begin 执行体 (return) end go 注意:建立一个存储过程后,修改的话应该把create 改为alter. 执行体内的语法: 1.定义变量:declare 变量名(@-) 数据类型 2.赋值语法:set/select 变量名=表达式 3.if语句格式: if- begin - end else begin

数据库—存储过程。

存储过程: 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它.存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程. 存储过程的建立: 选中存储过程,右击--新建存储过程,则出现下面的代码. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ===========

MySQL数据库存储过程动态表建立(PREPARE)

PREPARE statement_name FROM sql_text /*定义*/ EXECUTE statement_name [USING variable [,variable...]] /*执行预处理语句*/ DEALLOCATE PREPARE statement_name /*删除定义*/ 这是我项目当中用到的,用作参考使用: DELIMITER $$ DROP PROCEDURE IF EXISTS `gpsdata`.`sp_test`$$ CREATE DEFINER=`r

让你提前认识软件开发(28):数据库存储过程中的重要表信息的保存及相关建议

第2部分 数据库SQL语言 数据库存储过程中的重要表信息的保存及相关建议 1. 存储过程中的重要表信息的保存 在很多存储过程中,会涉及到对表数据的更新.插入或删除等,为了防止修改之后的表数据出现问题,同时方便追踪问题,一般会为一些重要的表建立一个对应的debug表.这个debug表中的字段要包括原表的所有字段,同时要增加操作时间.操作码和操作描述等字段信息. 例如,在某项目中,包括了如下一个重要的表tb_XXX: create table tb_XXX (      AAA           

数据库存储过程相关知识

(一) SET ANSI_NULLS {ON | OFF} 指定在对空值使用等于 (=) 和不等于 (<>) 比较运算符时,这些运算符的 SQL-92 遵从行为. 注释 SQL-92 标准要求对空值的等于 (=) 或不等于 (<>) 比较取值为 FALSE.当 SET ANSI_NULLS 为 ON 时,即使column_name 中存在空值,使用 WHERE column_name = NULL 的 SELECT 语句仍返回零行.即使column_name 中存在非空值,使用 W

JDBC对MySQL数据库存储过程的调用

一.MySQL数据库存储过程: 1.什么是存储过程 存储过程(英文:Stored Procedure)是在大型数据库系统中,为了完毕特定功能而编写的一组的SQL语句集.存储过程经编译存储在数据库中.用户通过指定存储过程的名字并给出參数(假设该存储过程带有參数)来运行它. 2.与一般SQL语句相比.使用存储过程有哪些长处.有哪些缺点 长处: 1).降低了脚本的运行环节,缩短了获取数据的时间.存储过程仅仅在创建的时进行编译,在调用使用的时候直接运行.不需再次编译:而一般SQL语句每次运行前都须要编译

一点实例明白mysql数据库存储过程

mysql存储过程: 封装sql: create procedure p1() begin select * from t_news; end $ //mysql存储过程 简单实例 显示存储过程信息:  \G (横向表格采用纵向表格输出) delimiter $  改变执行符号,直到mysql碰到$ 开始执行语句命令 set  names     解决mysql乱码问题  但mysql重启后又还原到以前字符集状态 call 存储过程名字 ()  调用存储过程 参数: create procedu

使用shell脚本调用mysql数据库存储过程,并设置定时任务

本来是要mysql数据库中创建事件任务来,定时执行存储过程,做数据传输的...后来由于种种原因,就使用crontab来定时执行,调用存储过程. 实现这个数据传输分为两步: 第一步:编写shell脚本调用mysql数据库存储过程,如下: #!/bin/bash#0 1 * * * sh /home/drmTrans3/rj_proc.shhost1=127.0.0.1user=systempasswd=linuxport=3306mysql -h${host1} -u${user} -p${pas

备份数据库存储过程

原文:备份数据库存储过程 由于使用SSMS创建的维护计划中,完整备份的话,会打断别的备份的顺序链,而由于管理原因,往往需要有几套备份计划,所以经过时间,本人编写了一个存储过程实现我的想法.不管你用不用,反正我用了. 首先,创建一个表,用于记录备份信息.因为用DMV和系统视图的话往往要编写很多东西,对于一般人很难记住那么多表.所以使用一个表来记录更加方便.以前之前已经创建了一个库 AuditDB,用于监控数据库的DDL操作,所以这里没有再编写建库脚本.只是在这个库里面创建一个表. USE Audi