我想减少数量字段并减少其数量

时间:2015-05-23 08:55:16

标签: sql sql-server stored-procedures

CREATE PROCEDURE dbo.IssueBook
    (
    @bookid nvarchar(50),
    @ano nvarchar(50),
    @mid int,
    @librarian varchar(10),
    @quantity int
    )
AS
    declare @cnt int
    declare @msg varchar(100)
    if  not exists( select * from books where bookid = @bookid and quantity = @quantity) 
       begin
         raiserror('Book is not available',16,1);      
         return;
       end;


    select @cnt = count(bookid) from issues where mid = @mid;
    if ( @cnt >= 2 )   
        begin
         raiserror('Maximum Limit Has Been Reached For Member!',16,1);
         return;
       end;

    begin tran
    begin try 
      update books set quantity =@quantity-1 where bookid= @bookid;
      insert into issues values (@bookid, @mid, getdate(), @librarian, @ano);
      commit tran
    end try
    begin catch
         rollback tran
         /* select  @msg = error_message() */
         raiserror( 'Unknown Error', 16,1);
    end catch

我想在sql表中更改quantity字段的值我该怎么做请帮助我我尝试了很多东西但是它们不起作用我会非常感谢你...

1 个答案:

答案 0 :(得分:1)

我认为问题出在这一部分:set quantity =@quantity-1。如果我理解正确,它应该是

set quantity = quantity-1 -- Decreease the book quantity by 1

set quantity = quantity - @quantity -- Decreease the book quantity by @quantity