2013-04-04 97 views
1

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

我會建議您的存儲過程中有業務邏輯。由於各種原因,但主要是可維護性。 – 2013-04-04 20:38:31

+0

這是一個更實用的教育目的,而不是實際的商業目的,你會有任何意見,我可以如何着手? – Shahze123 2013-04-04 20:40:04

+3

相反,我會建議你*將業務邏輯放在存儲過程中,邏輯是面向數據的。數據庫不是價值觀的啞巴商店,它們很聰明,應該像對待女性一樣對待。 – Sorpigal 2013-04-04 20:42:13

回答

3

這個怎麼樣更新時間:

UPDATE Title 
SET NumberInStock = NumberInStock - @Quantity 
WHERE ISBN = @ISBN 

的NumberInStoc k是一列,而不是@參數。

此外,你也可以做這樣的事情,但你必須創建@total和@GST變量:

UPDATE Sale 
SET subtotal = @amount, 
total = @total, 
GST = @GST 
WHERE sale.saleNumber = @salenumber 

我想你的第一個SELECT查詢是不是非常有用。你的程序將打印出一切。但是你已經有了你需要的參數,對嗎?它們是輸入參數。我希望這有幫助?

+0

所以我得到了一些錯誤,仍然像上面我的這條線其他 – Shahze123 2013-04-04 20:48:25

+0

你還在用「括號更新標題(NumberInStock = ...」)嗎?但是括號是用於插入語句。像「更新標題SET NumberInStock = ...」 – 2013-04-04 20:54:40

+0

也許你的INSERT INTO有問題,我想你可能會插入一個已經存在的salenumber,如果已經存在相同的鍵值,這可能會產生衝突,它是獨一無二的,表格是銷售表中的主鍵,還有其他什麼是saledetail中的主鍵?如果它不是PK的IDENTITY字段,則可能需要向INSERT INTO添加更多列。這些表中的列是什麼? – 2013-04-04 21:04:35