------------存储过程--------------
--usp_存储过程
--无参数的一个查询存储过程语法
/*
create proc usp_存储过程名
as
查询步骤
*/
go
create proc usp_spFenYe1
as
select * from dbo.Fn_FenYe1(1,2);
go
-----------
exec usp_spFenYe1;
------------------
go
alter proc usp_ZZ1--修改存储过程,create 为创建
as
begin
begin transaction
declare @myError int ;
set @myError=0;
update bank set balance=balance+900 where cId='001';
set @myError+=@@ERROR;
update bank set balance=balance-900 where cId='002';
if(@myError>0)
begin
rollback transaction
end
else
begin
commit transaction
end
end
--执行存储过程
go
exec usp_ZZ1;
go
select * from bank;
-------------------视图,存储过程,内联表值函数
--视图里面只能有一个结果集的查询
go
create view vw_Test1
as
select 1 as n,2 as m,3 as k
union
select 4,5,6;
go
select * from vw_Test1;
---------------------------
go
create proc usp_Test
as
begin
if(OBJECT_ID('TestTbl')) is not null
drop table TestTbl;
create table TestTbl
(
id int
)
end;
go
exec usp_Test;
select * from TestTbl
----------------带有参数的存储过程
--转账的例子
--转账时,输入谁转给谁,同时转多少
/*
create proc usp_ZZ2
@参数名 类型名,
@参数名 类型名,
@参数名 类型名
as
begin --事务
*/
go
create proc usp_ZZ2
@from char(4),
@to char(4),
@money money
as
begin transaction
declare @myError int;
set @myError=0;
update bank set balance=balance-@money where cId=@from;
set @myError=@@ERROR;
update bank set balance=balance +@money where cId=@to;
if(@myError>0)
begin
rollback transaction;
end
else
begin
commit transaction;
end
go
exec usp_ZZ2'001','002',100;exec usp_ZZ2 @from='001',@to='002',@money=100;
select * from bank;
------------有的存储过程不需要参数,有时又需要参数
--一般不需要参数的时候,是有一个默认值的参数
/*
语法
create proc usp_TestDefault
@testVal int =默认值
as
select
*/
--调用一个存储过程,如输入参数,就将参数打印出来,否则打印默认值
go
create proc usp_TestDefault
@str nvarchar(50)='默认值'
as
select @str;
exec usp_TestDefault;
exec usp_TestDefault '我是传进来的参数'
---一般情况,使用存储过程返回数据,都是使用output参数
select datediff (second ,'2012-01-01 0: 0:0',getdate())
go
create proc usp_ZZ4
@from char(4),
@to char(4),
@money money,
@state int output
-- 这个state表示需要在存储过程中赋值,外面使用的参数
as
begin
begin transaction
declare @last money;
set @last = (select balance from bank where cId=@from);
if(@last - 10 >= @money)
-- 我为了保证一致性,要求当转账后,即减去钱以后,余额还要大于10
-- @last - @money > 10
begin
update bank set balance=balance - @money where cid=@from;
update bank set balance=balance + @money where cid=@to;
commit;
set @state = 1;--成功
end
else
begin
rollback;
set @state = 0;--失败
end
end
go
--使用
declare @mystate int;
exec usp_ZZ4 '001','002',100,@mystate output;
select @mystate;
--usp_存储过程
--无参数的一个查询存储过程语法
/*
create proc usp_存储过程名
as
查询步骤
*/
go
create proc usp_spFenYe1
as
select * from dbo.Fn_FenYe1(1,2);
go
-----------
exec usp_spFenYe1;
------------------
go
alter proc usp_ZZ1--修改存储过程,create 为创建
as
begin
begin transaction
declare @myError int ;
set @myError=0;
update bank set balance=balance+900 where cId='001';
set @myError+=@@ERROR;
update bank set balance=balance-900 where cId='002';
if(@myError>0)
begin
rollback transaction
end
else
begin
commit transaction
end
end
--执行存储过程
go
exec usp_ZZ1;
go
select * from bank;
-------------------视图,存储过程,内联表值函数
--视图里面只能有一个结果集的查询
go
create view vw_Test1
as
select 1 as n,2 as m,3 as k
union
select 4,5,6;
go
select * from vw_Test1;
---------------------------
go
create proc usp_Test
as
begin
if(OBJECT_ID('TestTbl')) is not null
drop table TestTbl;
create table TestTbl
(
id int
)
end;
go
exec usp_Test;
select * from TestTbl
----------------带有参数的存储过程
--转账的例子
--转账时,输入谁转给谁,同时转多少
/*
create proc usp_ZZ2
@参数名 类型名,
@参数名 类型名,
@参数名 类型名
as
begin --事务
*/
go
create proc usp_ZZ2
@from char(4),
@to char(4),
@money money
as
begin transaction
declare @myError int;
set @myError=0;
update bank set balance=balance-@money where cId=@from;
set @myError=@@ERROR;
update bank set balance=balance +@money where cId=@to;
if(@myError>0)
begin
rollback transaction;
end
else
begin
commit transaction;
end
go
exec usp_ZZ2'001','002',100;exec usp_ZZ2 @from='001',@to='002',@money=100;
select * from bank;
------------有的存储过程不需要参数,有时又需要参数
--一般不需要参数的时候,是有一个默认值的参数
/*
语法
create proc usp_TestDefault
@testVal int =默认值
as
select
*/
--调用一个存储过程,如输入参数,就将参数打印出来,否则打印默认值
go
create proc usp_TestDefault
@str nvarchar(50)='默认值'
as
select @str;
exec usp_TestDefault;
exec usp_TestDefault '我是传进来的参数'
---一般情况,使用存储过程返回数据,都是使用output参数
select datediff (second ,'2012-01-01 0: 0:0',getdate())
go
create proc usp_ZZ4
@from char(4),
@to char(4),
@money money,
@state int output
-- 这个state表示需要在存储过程中赋值,外面使用的参数
as
begin
begin transaction
declare @last money;
set @last = (select balance from bank where cId=@from);
if(@last - 10 >= @money)
-- 我为了保证一致性,要求当转账后,即减去钱以后,余额还要大于10
-- @last - @money > 10
begin
update bank set balance=balance - @money where cid=@from;
update bank set balance=balance + @money where cid=@to;
commit;
set @state = 1;--成功
end
else
begin
rollback;
set @state = 0;--失败
end
end
go
--使用
declare @mystate int;
exec usp_ZZ4 '001','002',100,@mystate output;
select @mystate;