嘿所以我应该编写一个程序AddSaleDetail,它将为购买的图书添加销售详细信息,并将用该图书信息更新销售。需要传递的数据是销售编号,ISBN和数量。我必须为我已经完成的以下事情引发错误。 ISBN和销售号码无效 ISBN已在销售中。SQL,更新和插入
如果没有任何错误,我必须将销售明细记录插入到SaleDetail表中。售价将是该ISBN的建议价格。
现在我所得到的一切,直到接下来的两件事情需要完成,这是我无法继续下去的地方。
更新标题表中的图书以减少库存数量 更新Sale表中的销售记录小计,总计和GST字段以包含所购图书的销售金额。
这里是我有: ORIGINAL
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 [email protected]
declare @amount money = @quantity * @sellingprice
If exists (select * from saledetail where [email protected])
BEGIN
RAISERROR ('ISBN already exists',16,1)
END
ELSE
if not exists (select * from saledetail where [email protected])
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 [email protected] - @Quantity where [email protected])
当前
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 [email protected]
declare @amount money = @quantity * @sellingprice
If exists (select * from saledetail where [email protected])
BEGIN
RAISERROR ('ISBN already exists',16,1)
END
ELSE
if not exists (select * from saledetail where [email protected])
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
我会建议您的存储过程中有业务逻辑。由于各种原因,但主要是可维护性。 – 2013-04-04 20:38:31
这是一个更实用的教育目的,而不是实际的商业目的,你会有任何意见,我可以如何着手? – Shahze123 2013-04-04 20:40:04
相反,我会建议你*将业务逻辑放在存储过程中,逻辑是面向数据的。数据库不是价值观的哑巴商店,它们很聪明,应该像对待女性一样对待。 – Sorpigal 2013-04-04 20:42:13