嘿所以我應該編寫一個程序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