您的位置:首页 > 数据库

Day 8(11.15):存储过程(4)--处理错误信息

2015-11-15 16:52 281 查看
-- 4 ****************************************************

-- 存储过程中处理错误信息

-- 4.1 --------------------------------------------------

-- RETURN 无条件退出查询,可以返回数值,返回 0 表示成功

use Test

Go

create proc dbo.p_return

@fname nvarchar(50)

as

if exists (select * from pubs..employee where fname=@fname)

   begin

      select * from pubs..employee where fname=@fname

      return

   end

else

   begin

      return -12 -- 该名字不存在

   end

Go

declare @result nvarchar(50)

exec @result = dbo.p_return 'Maria'

select @result

declare @result nvarchar(50)

exec @result = dbo.p_return 'Maria'

select @result

-- 4.2 --------------------------------------------------

-- @@ERROR 返回最近捕获的语句错误号

/*

use Test

create table dbo.t_temperature

(

idx         int identity(1,1),

temp
4000
erature int,

create_time datetime default(getdate()),

constraint CK_t_temperature check(temperature between -18 and 45)

)

select * from t_temperature

*/

create proc dbo.p_Temperature

@temperature int

as

insert t_temperature (temperature) values (@temperature)

if @@error<>0

   begin

      return -1 --温度范围不正确

   end

else

   begin

      select * from t_temperature where idx=@@identity

   end

Go

declare @result int

exec @result=dbo.p_Temperature 32

select @result

select * from t_temperature

declare @result int

exec @result=dbo.p_Temperature -20

select @result

-- 4.3 --------------------------------------------------

-- RAISERROR 返回用户自定义错误信息,可记录错误日志

-- raiserror('error_message',severity_level,status) with log

alter proc dbo.p_Temperature

@temperature int

as

insert t_temperature (temperature) values (@temperature)

if @@error<>0

   begin

      raiserror('Procedure Test.dbo.p_Temperature Error',10,1)

      return -1 --温度范围不正确

   end

else

   begin

      select * from t_temperature where idx=@@identity

   end

Go

declare @result int

exec @result=dbo.p_Temperature -20

select @result

-- 以上仅返回错误信息,没有记录日志

alter proc dbo.p_Temperature

@temperature int

as

insert t_temperature (temperature) values (@temperature)

if @@error<>0

   begin

      raiserror('Procedure Test.dbo.p_Temperature Error',10,1) with log

      return -1 --温度范围不正确

   end

else

   begin

      select * from t_temperature where idx=@@identity

   end

Go

declare @result int

exec @result=dbo.p_Temperature -20

select @result

-- 4.4 --------------------------------------------------

-- SP_ADDMESSAGE 创建定制的错误信息

/*

use Test

create table dbo.t_AllInOne  -- 一卡通

(

user_id       int,

user_balance  int



create table dbo.t_Credit    -- 信用卡

(

user_id       int,

user_balance  int



insert t_AllInOne values (5005,4000)

insert t_Credit values (5005,-1500)

select * from t_AllInOne

select * from t_Credit

*/

exec sp_addmessage @msgnum=50001,@severity=16,@msgtext='Insufficient Funds',@with_log='true',@lang='english'

create proc dbo.p_Transfer

@user_id int,

@amount  int

as

if not exists (select user_id from t_AllInOne where user_id=@user_id)

   begin

      print 'Invalid User ID'

      return -1

   end

if not exists (select user_id from t_Credit where user_id=@user_id)

   begin

      print 'Invalid User ID'

      return -2

   end

if @amount>(select user_balance from t_AllInOne where user_id=@user_id)

   begin

      raiserror(50001,10,1)

      return -3

   end

update t_AllInOne set user_balance=user_balance-@amount where user_id=@user_id

update t_Credit set user_balance=user_balance+@amount where user_id=@user_id

Go

exec dbo.p_Transfer 5005,1500

select * from t_AllInOne

select * from t_Credit

-- 使用企业管理器 创建 message

-- 使用带参数的 message

exec sp_addmessage @msgnum=50002,@severity=16,@msgtext='Insufficient Funds for Customer ID: %d',@with_log='true',@lang='english'

alter proc dbo.p_Transfer

@user_id int,

@amount  int

as

if not exists (select user_id from t_AllInOne where user_id=@user_id)

   begin

      print 'Invalid User ID'

      return

   end

if not exists (select user_id from t_Credit where user_id=@user_id)

   begin

      print 'Invalid User ID'

      return

   end

if @amount>(select user_balance from t_AllInOne where user_id=@user_id)

   begin

      raiserror(50002,10,1,@user_id)

      return

   end

update t_AllInOne set user_balance=user_balance-@amount where user_id=@user_id

update t_Credit set user_balance=user_balance+@amount where user_id=@user_id

Go

exec dbo.p_Transfer 5005,1500

select * from t_AllInOne

select * from t_Credit

-- 练习 --------------------------------------------------

-- 1 创建存储过程,作用为用户登录,输入参数为用户名和用户密码。

--   如果帐号不存在,返回值为 -1;如果帐号存在,密码错误,返回值为 -2;如果通过验证,返回值为 0。

/*

-- 以下为用户帐号表

use Test

create table t_user(name varchar(20),password varchar(20))

insert t_user values ('Tom','123456')

insert t_user values ('Leo','12121')

select * from t_user

*/

-- 2 更改上面的存储过程,使其在用户验证失败时,记录错误日志。

--   如果帐号不存在,记录 'Invalid User : @name' ;如果帐号存在,密码错误,记录 'Invalid Password For User : @name'

--   要求创建2个错误消息

-- 3 库存表中,stock 是库存数量, reorder_level 是再订货线,即 stock 低于 reorder_level 时需要再订货

--   编写存储过程,用于生成订单,订单记录在 t_order 中。输入参数为产品ID,订购数量;输出参数为实际取货数量,订单总价。

--   订货成功后,若库存低于再订货线,记录系统日志,信息级别为‘信息’

--   当库存低于订购数量时,取货数量为库存数量,记录系统日志,信息级别为‘警告’

/*

use Test

create table dbo.t_Inventory

(

product_id    int,

unit_price    int,

stock         int,

reorder_level int

)

insert dbo.t_Inventory values (17,199,50,20)

insert dbo.t_Inventory values (18,215,45,20)

insert dbo.t_Inventory values (21,179,60,25)

insert dbo.t_Inventory values (26,395,35,10)

insert dbo.t_Inventory values (35,485,15,5)

create table dbo.t_order(idx int identity(1,1),p_id int,u_price int,o_qty int,r_qty int,total int)

select * from t_Inventory

select * from t_order

*/

-- 答案 --------------------------------------------------

-- 1

create proc dbo.p_Login

@name     varchar(20),

@password varchar(20)

as

if not exists (select * from t_user where name=@name)

   begin

      return -1

   end

if not exists (select * from t_user where name=@name and password=@password)

   begin

      return -2

   end 

else

   begin

      return 0

   end

Go

declare @result int

exec @result=p_Login 'Tommy','1234567'

select @result

-- 或者

alter proc dbo.p_Login

@name     varchar(20),

@password varchar(20)

as

declare @password_r varchar(20)

select @password_r=password from t_user where name=@name

if @@rowcount=0

   return -1

if @password<>@password_r

   return -2

Go

declare @result int

exec @result=p_Login 'Tommy','1234567'

select @result

-- 2

exec sp_addmessage @msgnum=50008,@severity=16,@msgtext='Invalid User : %s',@with_log='true',@lang='english'

exec sp_addmessage @msgnum=50009,@severity=16,@msgtext='Invalid Password For User : %s',@with_log='true',@lang='english'

Go

alter proc dbo.p_Login

@name     varchar(20),

@password varchar(20)

as

declare @password_r varchar(20)

select @password_r=password from t_user where name=@name

if @@rowcount=0

   begin

      raiserror(50008,10,1,@name)

      return -1

   end

if @password<>@password_r

   begin

      raiserror(50009,10,1,@name)

      return -2

   end

Go

declare @result int

exec @result=p_Login 'Tommy','1234567'

select @result

-- 3 

alter proc dbo.p_Order

@product_id int,

@order_sum  int,

@get_sum    int output,

@total      int output

as

declare @u_price int

if not exists (select * from t_Inventory where product_id=@product_id)

   begin

      select '产品号不正确'

      return -1

   end

select @u_price=unit_price from t_Inventory where product_id=@product_id

if @order_sum > (select stock from t_Inventory where product_id=@product_id)

   begin

      select @get_sum=(select stock from t_Inventory where product_id=@product_id)

      select @total=@get_sum*(select unit_price from t_Inventory where product_id=@product_id)

      insert t_order (p_id,u_price,o_qty,r_qty,total) values (@product_id,@u_price,@order_sum,@get_sum,@total)

      update t_Inventory set stock=0 where product_id=@product_id

      raiserror('Insufficient Inventory for Product ID: %d',12,1,@product_id) with log

      return -2

   end

update t_Inventory set stock=stock-@order_sum where product_id=@product_id

select @get_sum=@order_sum

select @total=@get_sum*(select unit_price from t_Inventory where product_id=@product_id)

insert t_order (p_id,u_price,o_qty,r_qty,total) values (@product_id,@u_price,@order_sum,@get_sum,@total)

if (select stock from t_Inventory where product_id=@product_id) < (select reorder_level from t_Inventory where product_id=@product_id)

   begin

      raiserror('Product ID: %d Under Reorder Level',10,1,@product_id) with log

      return -3

   end

Go

select * from t_Inventory

select * from t_order

declare @get_sum int

declare @total   int

declare @result  int

exec @result=dbo.p_Order 21,20,@get_sum output,@total output

select @get_sum as get_sum,@total as total

select @result
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql server 数据库