sql 存储过程的简单实现

--存储过程
/*
1:什么是存储过程
类比c中的函数
处理管理业务和业务规则 (t-sql 程序块)
(ddl 变量 逻辑控制 )
是否有返回参数
*/

--建立存储过程 -相当于c中的定义函数
--create view 名称 as T-sql
--查处乐乐的个人信息(简单的实例)

use stu
go
create procedure pro_stuInfo
as
select * from stuInfo where stuName='乐乐'

--执行存储过程 --c中调用函数
exec pro_stuInfo


--自定义
--系统存储过程
exec sp_databases --得到所有数据库的信息
exec sp_helpdb 'stu' --指定数据库的信息
exec sp_renamedb 'bank','yinh'  --修改数据库的名称
exec sp_columns 'stuInfo'
exec sp_password '新密码','旧密码','用户名'
exec sp_helptext '存储过程的名称'
--扩展的存储过程
--xp_cmdshell 默认是关闭
/*
1:视图
2: t-sql语句
*/
exec sp_configure 'show advanced options',1
go
reconfigure
go
exec sp_configure 'xp_cmdshell',1
go
reconfigure

exec xp_cmdshell 'ipconfig'
--使用xp_cmdshell
use master
go
exec xp_cmdshell 'mkdir E:\banks',no_output
drop database shuju
create database shuju
create table bankyh
(
id int
)

exec xp_cmdshell 'dir E:\banks'

--存储过程
--请创建存储过程,查看本次考试平均分以及未通过考试的学员名单
if exists(select * from sysObjects where name='pro_stuInfo' )
drop proc pro_stuInfo
go --必须是查询批次中的第一个语句
create proc pro_stuInfo
as
select avg(writtenExam) from score
select stuName,stuInfo.stuNo,writtenExam,labExam from stuInfo
inner join score on stuInfo.stuNo=score.stuNo where writtenExam<60 or labExam<60

--执行
exec pro_stuInfo

--带参数的存储过程
--输入参数
--输出参数

--每次笔试和机试的及格线可能随时变化(不再是分),
--这导致考试的评判结果也相应变化。
if exists(select * from sysObjects where name='pro_stuInfo_score' )
drop proc pro_stuInfo_score
go
create proc pro_stuInfo_score
@writtenpass int,
@labpass int
as
select stuName,stuInfo.stuNo,writtenExam,labExam from stuInfo
inner join score on stuInfo.stuNo=score.stuNo where writtenExam <@writtenpass or labExam<@labpass


--执行
exec pro_stuInfo_score 60,60 --带输入参数
--带默认值参数
if exists(select * from sysObjects where name='pro_stuInfo_score' )
drop proc pro_stuInfo_score
go
create proc pro_stuInfo_score
@writtenpass int=60,
@labpass int=60
as
select stuName,stuInfo.stuNo,writtenExam,labExam from stuInfo
inner join score on stuInfo.stuNo=score.stuNo where writtenExam <@writtenpass or labExam<@labpass
--执行
exec pro_stuInfo_score
exec pro_stuInfo_score @labpass=50 --对机试指定
exec pro_stuInfo_score 90 --对笔试成绩
exec pro_stuInfo_score @writtenpass=90 ,@labpass=50

--带输出参数

--笔试和机试的及格线,统计出不及格人数
if exists(select * from sysObjects where name='pro_stuInfo_score' )
drop proc pro_stuInfo_score
go
create proc pro_stuInfo_score
@ispass int output, --输出参数
@writtenpass int=60, --输入
@labpass int=60
as
select @ispass =count(*) from score where writtenExam <@writtenpass or labExam<@labpass
select * from score where writtenExam <90 or labExam<90
--执行
declare @sum int
exec pro_stuInfo_score @sum output,90,90

print @sum

--(学习时候的总结)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值