Sql基础代码第六章存储过程.docx

上传人:b****6 文档编号:4729170 上传时间:2022-12-08 格式:DOCX 页数:11 大小:336.05KB
下载 相关 举报
Sql基础代码第六章存储过程.docx_第1页
第1页 / 共11页
Sql基础代码第六章存储过程.docx_第2页
第2页 / 共11页
Sql基础代码第六章存储过程.docx_第3页
第3页 / 共11页
Sql基础代码第六章存储过程.docx_第4页
第4页 / 共11页
Sql基础代码第六章存储过程.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

Sql基础代码第六章存储过程.docx

《Sql基础代码第六章存储过程.docx》由会员分享,可在线阅读,更多相关《Sql基础代码第六章存储过程.docx(11页珍藏版)》请在冰豆网上搜索。

Sql基础代码第六章存储过程.docx

Sql基础代码第六章存储过程

Sql基础代码

目录

第一章建库建表2

1.1数据库的应用2

1.1.1创建数据库2

1.1.2查询数据库3

1.1.3删除数据库3

1.2表的运用4

1.2.1创建表4

1.2.2删除表5

1.3约束6

1.3.1五种约束6

1.3.2约束的使用6

第二章数据查询8

2.1建表和插入数据8

2.1.1建立学生信息表8

2.1.2插入信息9

2.2查询运用11

2.2.1数据查询11

2.2.2单表查询12

2.2.3连接查询13

2.2.4操作结果集查询18

2.2.5嵌套查询18

2.3数据库更新19

第三章函数19

3.1五种函数的运用19

3.1.1字符串函数20

3.1.2数学函数21

3.1.3日期函数22

3.1.4系统函数23

3.1.5聚合函数24

第四章变量25

4.1局部变量26

4.2全局变量27

第五章循环语句30

5.1统计平均成绩30

5.2提分31

5.3根据成绩显示等级35

第六章存储过程36

6.1存储过程的应用36

6.1.1系统存储过程36

6.1.2Sp_helptext的使用40

6.1.3创建存储过程41

6.2raiserror语句的应用45

第七章exists子查询46

7.1exists子查询的应用46

7.2求银行利息49

7.3变量查询51

7.4查询考试通过情况53

7.5循环提分55

7.6通过率57

第八章事物58

8.1使用事物解决银行转账59

第九章触发器60

9.1检测触发器62

9.2创建触发器62

9.3触发器delete运用64

9.4触发器update运用65

9.5触发器禁止修改运用66

 

第六章存储过程

6.1存储过程的应用

6.1.1系统存储过程

--列出服务器上的所有数据库--

sp_databases

--报告有关指定数据库或所有数据库的信息--

sp_helpdb

--更改数据库的名称--

usemaster

sp_renamedb'gongziguanli'/*更改前的名称*/,'工资管理'/*更改后的名称*/

--返回当前环境下可查询的对象的列表--

sp_tables

-----------------以下存储过程都以student数据库为例-----------------

usestudent

--回某个表列的信息--

sp_columnsstuinfo

--查看某个表的所有信息--

sp_helpstuinfo

--查看某个表的约束--

sp_helpconstraintstuinfo

--查看某个表的索引--

sp_helpindexstuinfo

--列出当前环境中的所有存储过程--

sp_stored_procedures

--添加或修改登录转户的密码--

sp_password'1234','2234'

--显示默认值、未加密的存储过程、用户定义的存储过程、触发器或视图的实际文本--

sp_helptext'sp_helptext'

--创建数据库bankDB,要求保存在D:

\bank

usemaster

go

execxp_cmdshell'mkdird:

\bank',no_output

ifexists(select*fromsysdatabaseswherename='bankDB')

dropdatabasebankDB

go

createdatabasebankDB

execxp_cmdshell'dird:

\bank\'--查看文件

6.1.2Sp_helptext的使用

--显示表的所有文本信息

sp_helptext'sysobjects'

--显示数据库的所有文本信息sp_helptext'sysdatabases'

sp_helptext'sp_helptext'

sp_helptext'sys.all_columns'

6.1.3创建存储过程

--用sp_executesql显示stuinfo表

usestudent

declare@sqlnvarchar(1000)

declare@tablenamenvarchar(20)

set@tablename='stuinfo'

set@sql='select*from'+@tablename+'where1=1'

print@sql

execsp_executesql@sql

--创建存储过程,查看本次考试平均分及未通过考试的学员名单

createprocproc_stu

as

declare@wavgfloat--笔试平均分

declare@lavgfloat--机试平均分

select@wavg=avg(wriexam),@lavg=avg(labexam)

fromstumarks

print'本次笔试的平均分为:

'+convert(varchar(10),@wavg)

print'本次机试的平均分为:

'+convert(varchar(10),@lavg)

if(@wavg>80and@lavg>80)--判断平均分等级

print'本班成绩优秀'

else

print'本班成绩较差'

print'没有通过考试的学员有:

'

print'======================================'

select*fromstumarkswherewriexam<60orlabexam<60--显示未通过的学员

go

--调用存储过程

execproc_stu

--用存储过程更改及格线

createprocproc_stu1

@wriexamint,--在存储过程前加变量要加逗号,并且变量前不加declare

@labexamint

as

print'没有通过考试的学员有:

'

print'======================================'

select*fromstumarkswherewriexam<@wriexamorlabexam<@labexam

--将笔试及格线和机试及格线设为70

execproc_stu1@wriexam=70,@labexam=70

 

--如果希望存储过程后,返回一个或多个值,这是就需要使用输出(output)参数了

--修改上列,返回未通过考试的学员人数

createprocproc_stu2

@nopasscountintoutput,

@wriexamint=60,

@labexamint=60

as

print'笔试及格分数线为:

'+convert(varchar(10),@wriexam)+

'机试及格分数线为:

'+convert(varchar(10),@labexam)

print'不及格学员有:

'

select@nopasscount=count(*)fromstumarks

wherewriexam<@wriexam

orlabexam<@labexam

selectsname,stuinfo.sno,wriexam,labexam

fromstuinfo

innerjoinstumarksonstuinfo.sno=stumarks.sno

wherewriexam<@wriexam

orlabexam<@labexam

go

execproc_stu260,60

--根据及格线以及及格人数来判断是否应该要上调分数

declare@countint

execproc_stu2@countoutput,65,60--加入output,结果不同

if(@count>3)

print'不及格人数'+convert(char(5),@count)+'分数线还应下调'

else

print'分数线合适,不用下调'

 

6.2raiserror语句的应用

--使用raiserror语句,如果有错误,统计中断退出

createprocproc_stu3

@notpasssumintoutput,--输出参数

@wriexamint=60,

@labexamint=60

as

if(not@wriexambetween0and100)

or(not@labexambetween0and100)

begin

raiserror('及格线错误,请指定1-100之间的分数,统计中断退出',16,1)

--引发系统错误,指定错误的严重级别,调用级别为(默认),并影响@@error系统变量的值

return--退出批处理,后续语句不再执行

end

go

--调用存储过程,测试raiserror语句

declare@sumint,@tint

execproc_stu3@sumoutput,604--笔试及格线误输入分

set@t=@@error

print'错误号:

'+convert(varchar(5),@t)

if@t<>0

return

print'---------------------------------'

if@sum>=3

print'未通过人数:

'+convert(varchar(5),@sum)+

'人,超过60%,及格分数线还应下调'

else

print'未通过人数:

'+convert(varchar(5),@sum)+

'人,自控制在60%一下,及格分数线适中'

Go

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 其它课程

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1