嘿,所以我应该编写一个 AddSaleDetail 程序,它将为所购买的书籍添加销售详细信息,并使用该书籍信息更新销售。需要传入的数据是销售编号、ISBN 和数量。对于我已经完成的以下事情,我必须 RaiseErrors。ISBN 和销售编号无效 ISBN 已在该销售中。
如果没有任何错误,我必须将销售细节记录插入 SaleDetail 表。售价将是该 ISBN 的建议价格。
现在我所拥有的一切,直到接下来需要完成的两件事,这是我无法继续的地方。
更新 Title 表中的图书以减少库存数量 更新 Sale 表中的 Sale 记录小计、总计和 GST 字段以包括所购买图书的销售金额。
这是我所拥有的: 原始
Create Procedure AddSaleDetail
(
@salenumber int,
@ISBN char(10),
@Quantity int,
@NumberInStock smallint
)
AS
SELECT sale.saleNumber, title.ISBN, saledetail.quantity,NumberInStock
FROM sale INNER JOIN
saledetail ON sale.saleNumber = saledetail.saleNumber INNER JOIN
title ON saledetail.ISBN = title.ISBN
IF @ISBN is null or @salenumber is null
BEGIN
RAISERROR ('Please enter valid ISBN and Sale Number',16,1)
END
Else
BEGIN
declare @sellingprice money
select @sellingprice= suggestedprice from title where ISBN=@ISBN
declare @amount money = @quantity * @sellingprice
If exists (select * from saledetail where ISBN=@ISBN)
BEGIN
RAISERROR ('ISBN already exists',16,1)
END
ELSE
if not exists (select * from saledetail where saleNumber=@salenumber)
BEGIN
RAISERROR ('Sale Number Does not exist',16,1)
END
ELSE
BEGIN
INSERT INTO saledetail(ISBN,saleNumber, sellingprice)
values (@ISBN,@salenumber,@sellingprice )
END
END
Else
BEGIN
Update title(NumberInStock =@NumberInStock - @Quantity where ISBN=@ISBN)
当前的
Create Procedure AddSaleDetail
(
@salenumber int,
@ISBN char(10),
@Quantity int,
@NumberInStock smallint
)
AS
SELECT sale.saleNumber, title.ISBN, saledetail.quantity,NumberInStock
FROM sale INNER JOIN
saledetail ON sale.saleNumber = saledetail.saleNumber INNER JOIN
title ON saledetail.ISBN = title.ISBN
IF @ISBN is null or @salenumber is null
BEGIN
RAISERROR ('Please enter valid ISBN and Sale Number',16,1)
END
Else
BEGIN
declare @sellingprice money
select @sellingprice= suggestedprice from title where ISBN=@ISBN
declare @amount money = @quantity * @sellingprice
If exists (select * from saledetail where ISBN=@ISBN)
BEGIN
RAISERROR ('ISBN already exists',16,1)
END
ELSE
if not exists (select * from saledetail where saleNumber=@salenumber)
BEGIN
RAISERROR ('Sale Number Does not exist',16,1)
END
ELSE
Begin Transaction
BEGIN
INSERT INTO saledetail(ISBN,saleNumber, sellingprice)
values (@ISBN,@salenumber,@sellingprice )
if @@Error<>0
Begin
Raiserror ('insert failed',16,1)
Rollback Transaction
END
Else
Begin
UPDATE Title
SET NumberInStock = NumberInStock - @Quantity
WHERE ISBN = @ISBN
if @@Error<>0
Begin
Raiserror('Update failed',16,1)
Rollback Transaction
End
Else
begin
Commit Transaction
END
END
END
END