SQLServer第七章:存储过程proc

我理解为把1个表或者多个表关联的数据,封装起来,调用改变数据,哪些表的数据也会改变。用起来方便。

存储过程优点:
  (1)执行速度快
  (2)效率高
  (3)安全性
  (4)模块化编程

分类:
  系统存储过程:sp_xxxxx
  扩展存储过程:xp_xxxxx
  用户自定义存储过程:

系统存储过程

--列出服务器上所有的数据库
exec sp_databases        --无参数的

--更改数据库名称
exec sp_renamedb 'stuDB','StudentDB'    --带参数的,把stuDB数据库名改为StudentDB。

--查看某个表的信息:参数可选
exec sp_help        
exec sp_help 'PCInfo'

使用数据库运行系统DOS命令:

 --打开xp_cmdshell命令 ,服务系统默认关闭状态所以也要打开。
exec sp_configure 'show advanced options', 1  
reconfigure
go
--开启xp_cmdshell服务
exec sp_configure 'xp_cmdshell', 1  --1不是打开,0表示关闭
reconfigure
go
--调用dos命令创建文件夹:不打开服务直接运行这一句会报错。为了防止黑客入侵,默认关闭状态。
exec xp_cmdshell 'md E:\zuxia'  --这里可以随意输入dos命令。
-- 禁用xp_cmdshell服务
exec sp_configure 'xp_cmdshell', 0
reconfigure
go

自定义存储过程:

程序员根据自己的需要来定义的

语法:
  create procedure(proc) proc_名称
    形参..........
  as
    相关代码............................
  go

调用:exec 过程名称 实数........也是传参

创建两个表做演示

create database NetBarDB    --创建数据库create database 数据库名
go    --批处理(批量处理)
use NetBarDB                --打开数据库
go
create table PCInfo             --创建表:create table 表名
(
    PCid int primary key identity,
    PCname char(10), --姓名
)
create table cardInfo             --创建表:create table 表名
(
    cardid int primary key identity,
    cardsex char(2), --性别
    cardage int        --年龄
)
insert into PCInfo values('张三'),('李四'),('王五')
insert into cardInfo values('',18),('',20),('',19)

无参

if exists(select * from sys.objects where name='proc_getstuinfo')
begin
    drop proc proc_getstuinfo --如果存在就删除。才能创建
end
go
create proc proc_getstuinfo
    --这里是无参
as
    select PCInfo.PCId as 学号, 
            PCInfo.PCname as 姓名,
            cardsex as 性别,
            cardage as 密码
    from PCInfo inner join cardInfo on PCInfo.PCId=cardInfo.cardid 
go
--调用存储过程:有参数就带参数,下面这个是无参
exec proc_getstuinfo

有参

if exists(select * from sys.objects where name='proc_PCU')
begin
    drop proc proc_PCU
end
go --go是批量处理,下面才不报错
create proc proc_PCU
    @PCname varchar(20),  --参数
    @sex varchar(2)  --第二个参数,以此类推,看自己需求
as
    select PCInfo.PCId as 学号, 
            PCInfo.PCname as 姓名,
            cardsex as 性别,
            cardage as 密码
    from PCInfo inner join cardInfo on PCInfo.PCId=cardInfo.cardid  where PCname=@PCname and cardsex=@sex
go
--调用:
exec proc_PCU '张三',''

案例

if exists(select * from sys.objects where name='proc_insertStuMarks')
begin
    drop proc proc_insertStuMarks
end
go
create proc proc_insertStuMarks
    @PCname varchar(20),
    @cardsex char(2),
    @cardage int,
    @num int output
as  
    --判断:用户存在的情况
    if exists(select * from PCInfo where PCname=@PCname)
    begin
        raiserror('用户已经存在!',16,20)  --raiserror('错误消息',错误级别,错误号)错误级别:0-18,错误号:1-127
        return
    end

    --判断:笔试成绩
    if(@cardage>100 or @cardage<0)
    begin
        raiserror('年龄只能在0-100之间!!',16,20)
        return
    end
    
    --插入
    insert into PCInfo values(@PCname)
    insert into cardInfo values(@cardsex,@cardage)
    set @num=@@rowcount --返回受影响行数
go
--调用:
declare @n int
exec proc_insertStuMarks  '赵六','',90,@n output --output用来指定参数是输出型参数
if(@n = 1)
begin
    print '添加成功'
end
else
begin
    print '添加失败'
end
go

select * from PCInfo inner join cardInfo on PCId=cardid

 

posted @ 2022-09-17 19:53  Akai_啊凯  阅读(346)  评论(0编辑  收藏  举报