触发器,存储过程,视图创建

触发器的语法:

?
1
2
3
4
5
6
create [ or replace ] tigger 触发器名 触发时间 触发事件
on 表名
[ for each row]
begin
  pl/sql语句
end

存储过程语法:

语法:

复制代码
 1 CREATE PROC [ EDURE ] procedure_name [ ; number ]
 2     [ { @parameter data_type }
 3         [ VARYING ] [ = default ] [ OUTPUT ]
 4     ] [ ,...n ]
 5 [ WITH
 6     { RECOMPILE | ENCRYPTION | RECOMPILE , ENCRYPTION } ]
 7 [ FOR REPLICATION ]
 8 AS 
 9 [ begin ]
10     T-SQL 语句
11 [ end ]
复制代码

无参数存储过程:

复制代码
 1 --创建名为 GetStuCou 的无参数存储过程
 2 create procedure GetStuCou
 3 as
 4 begin
 5     select * 
 6     from Student s
 7     left join Course c on s.C_S_Id=c.C_Id
 8 end
 9 
10 --执行名为 GetStuCou 的无参数存储过程
11 execute GetStuCou
复制代码

有返回值的存储过程:

复制代码
 1 --创建名为 GetStuCou_Re 的有返回值的存储过程
 2 create procedure GetStuCou_Re
 3 as
 4 begin
 5     insert into Course(C_Name) values('HTML5')
 6     return SCOPE_IDENTITY();        -- 返回为当前表插入数据最后生成的标识值。
 7 end
 8 
 9 --执行名为 GetStuCou 的有返回值的存储过程
10 execute GetStuCou_Re
复制代码

有输入参数的存储过程:

复制代码
 1 --创建名为 GetStuCou_In 的有输入参数的存储过程
 2 create procedure GetStuCou_In
 3 @StuNo    nvarchar(64)='001'        --设置默认值
 4 as
 5 begin
 6     select * from Student where S_StuNo=@StuNo
 7 end
 8 
 9 --执行名为 GetStuCou_In 的有输入参数的存储过程(不传参数,即使用默认值)
10 execute GetStuCou_In
11 
12 --执行名为 GetStuCou_In 的有输入参数的存储过程(传入参数)
13 execute GetStuCou_In '005'
复制代码

有输入、输出参数的存储过程:

复制代码
 1 --创建名为 GetStuCou_Out 的有输入参数和输出参数的存储过程
 2 create procedure GetStuCou_Out
 3 @StuNo    nvarchar(64),
 4 @Height nvarchar(32) output
 5 as
 6 begin
 7     if(@StuNo is not null and @StuNo <> '')
 8     begin
 9         select @Height=S_Height 
10         from Student 
11         where S_StuNo=@StuNo
12     end
13     else
14     begin
15         set @Height='185'
16     end
17 end
18 
19 --执行名为 GetStuCou_Out 的有输入参数和输出参数的存储过程
20 execute GetStuCou_Out '005',null
复制代码

有输入、输出参数和结果集的存储过程:

复制代码
 1 --创建名为 GetStuCou_DS 的有输入参数、输出参数和结果集的存储过程
 2 create procedure GetStuCou_DS
 3 @StuNo    nvarchar(64),
 4 @Height nvarchar(32) output
 5 as
 6 begin
 7     if(@StuNo is not null and @StuNo <> '')
 8     begin
 9         select @Height=S_Height 
10         from Student 
11         where S_StuNo=@StuNo
12     end
13     else
14     begin
15         set @Height='185'
16     end
17 
18     select s.S_Id,s.S_StuNo,s.S_Name,s.S_Sex,s.S_Height,s.S_BirthDate,c.C_Id,c.C_Name
19     from Student s
20     left join Course c on s.C_S_Id=c.C_Id
21     where S_StuNo=@StuNo
22 end
23 
24 --执行名为 GetStuCou_DS 的有输入参数、输出参数和结果集的存储过程
25 execute GetStuCou_DS '005',null
复制代码

返回多个结果集的存储过程:

复制代码
 1 --创建名为 GetStuCou_DSS 的返回多个结果集的存储过程
 2 create procedure GetStuCou_DSS
 3 @StuNo    nvarchar(64),
 4 @Height nvarchar(32)
 5 as
 6 begin
 7     if(@StuNo is not null and @StuNo <> '')
 8     begin
 9         select *
10         from Student 
11         where S_StuNo=@StuNo
12     end
13 
14     if(@Height is not null and @Height <> '')
15     begin
16         select * 
17         from Student
18         where S_Height=@Height
19     end
20 end
21 
22 --执行名为 GetStuCou_DSS 的返回多个结果集的存储过程
23 execute GetStuCou_DSS '005','185'
复制代码

存储过程里面不仅可以进行查询,还可以进行各种增删改操作。其实存储就是由很多 T-SQL 语句组成的代码块。

 

存储过程中创建变量、赋值变量、创建表变量和临时表:

复制代码
 1 --创建名为 GetStuCou_Ext 的返回多个结果集的存储过程
 2 create procedure GetStuCou_Ext
 3 @StuNo    nvarchar(64),
 4 @Height nvarchar(32)
 5 as
 6 begin
 7     declare @Var nvarchar(10)    --定义变量
 8 
 9     set @Var='123'        --赋值变量
10 
11     --定义表变量
12     declare @StuTab table 
13     (
14         ID     int not null primary key,
15         StuNo    nvarchar(50) unique,
16         Name varchar(50),
17         Sex varchar(10),
18         Height varchar(10)
19     )
20     --表变量只能在定义的时候添加约束
21 
22     --定义临时表
23     create table #Tab
24     (
25         ID     int not null primary key,
26         StuNo    nvarchar(50),
27         Name varchar(50),
28         Sex varchar(10),
29         Height varchar(10)
30     )
31 
32     alter table #Tab add constraint S_UNIQUE unique(StuNo)
33 
34     --临时表可以在之后添加约束
35     
36     if(@StuNo is not null and @StuNo <> '')
37     begin
38         insert into @StuTab(ID,StuNo,Name,Sex,Height)    --把数据插入表变量
39         select S_Id,S_StuNo,S_Name,S_Sex,S_Height
40         from Student 
41         where S_StuNo=@StuNo
42 
43         insert into #Tab(ID,StuNo,Name,Sex,Height)    --把数据插入临时表
44         select S_Id,S_StuNo,S_Name,S_Sex,S_Height
45         from Student 
46         where S_StuNo=@StuNo
47     end
48 
49     if(@Height is not null and @Height <> '')
50     begin
51         insert into @StuTab(ID,StuNo,Name,Sex,Height)    --把数据插入表变量
52         select S_Id,S_StuNo,S_Name,S_Sex,S_Height
53         from Student
54         where S_Height=@Height
55 
56         insert into #Tab(ID,StuNo,Name,Sex,Height)    --把数据插入临时表
57         select S_Id,S_StuNo,S_Name,S_Sex,S_Height
58         from Student
59         where S_Height=@Height
60     end
61 
62     SELECT * FROM @StuTab
63     select * from #Tab
64 end
65 
66 --执行名为 GetStuCou_DSS 的返回多个结果集的存储过程
67 execute GetStuCou_Ext '005','185'
复制代码

存储过程动态执行 SQL 语句:

以上可以看出我们传入的参数(学号)是单个的,那么如果一次性传入多个学号呢(使用逗号隔开,即 '005,006,007' ),这就需要用到动态拼接并执行 sql 语句。

复制代码
 1 create proc GetStus
 2 @StuNo nvarchar(500)
 3 as
 4 begin
 5     declare @Sql nvarchar(3000)
 6 
 7     if(@StuNo is not null and @StuNo <> '')
 8     begin
 9         set @Sql=' select * from Student where S_StuNo in ('+@StuNo+') '
10     end
11 
12     exec (@Sql)    --执行动态 sql 
13 end
14 
15 exec GetStus '003,005,009'        --执行存储过程 GetStus
复制代码

现在还有一个问题,我想要执行动态 sql 的时候并返回变量值,比如我现在需要执行动态 sql 的时候返回课程 ID ,然后根据课程 ID 查询课程。

使用系统存储过程 sp_executesql 动态 sql 给变量赋值。

修改存储过程之后:

复制代码
 1 ALTER proc [dbo].[GetStus]
 2 @StuNo nvarchar(500)
 3 as
 4 begin
 5     declare @Sql nvarchar(3000)
 6     declare @C_Id int
 7     declare @Cou int
 8 
 9     if(@StuNo is not null and @StuNo <> '')
10     begin
11         set @Sql=' select @CId=C_S_Id,@cou=count(1) from Student where S_StuNo in ('+@StuNo+') group by C_S_Id '
12     end
13 
14     exec sp_executesql @Sql,N'@CId int output,@cou int output',@CId = @C_Id output,@cou = @Cou output;
15 
16     select @C_Id,@Cou    --查看返回变量的值
17 
18     select * from Course where C_Id=@C_Id 
19 end
20 
21 exec GetStus '005'        --执行存储过程 GetStus

猜你喜欢

转载自blog.csdn.net/sayesan/article/details/80221653
今日推荐