2013-04-04 103 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