mysql高级 视图 与 存储器

视图:(视图可以看做是一张很多表联合查询到的数据集合成的一张数据表)

create view vw_StuInfo   --  创建视图 ,视图中的语句 都是 select组合成的查询语句
as
select * from test1;     

select * from vw_StuInfo  --使用视图 (把其当做一张表看待)

存储过程:

与视图的差别是:视图只写 select语句   存储过程可以写任何东西 增删改查、变量、参数 等等;

存储过程的创建:

create procedure 存储过程的名字
-- @变量 类型 【= 默认值】 【output】 []代表是可选的 把它当做函数看就很好理解了!
@name varchar = '默认值' output
as
begin
...语句;
end;

--老师sql代码 几种写法

go
create proc usp_转账2
@from char(4)
, @to char(4)
, @money money
as
begin
begin transaction
begin try
update bank set balance=balance - @money where cid=@from
update bank set balance=balance + @money where cid=@to
commit transaction
end try
begin catch
rollback transaction
end catch
end;
go
-- 调用带有参数的存储过程
exec usp_转账2 '0001', '0002', 200.00

exec usp_转账2 @to='0001', @from='0002', @money=1000

select * from bank;

go
create proc usp_转账3
@from char(4)
, @to char(4)
, @money money
, @isSuccess int output
as
begin
begin transaction
begin try
update bank set balance=balance - @money where cid=@from
update bank set balance=balance + @money where cid=@to
commit transaction
set @isSuccess = 1;
end try
begin catch
rollback transaction
set @isSuccess = 0;
end catch
end;
go

-- 需要一个变量
declare @res int
-- exec usp_转账3 '0001', '0002', 500, @res output;
exec usp_转账3 @isSuccess=@res output, @to='0001', @from='0002', @money=500

select @res;

select * from bank;

坚持
原文地址:https://www.cnblogs.com/gaoSJ/p/13675864.html