SQL,更新和插入

时间:2013-04-04 20:35:55

标签: sql updates

嘿所以我应该写一个程序AddSaleDetail,它将为购买的书籍添加销售细节,并将使用该书籍信息更新销售。需要传递的数据是销售号,ISBN和数量。我必须为我已经完成的以下事情进行RaiseErrors。 ISBN和销售号码无效 ISBN已经在销售中。

如果没有任何错误,我必须将Sale Detail记录插入SaleDetail表。售价将是该ISBN的建议价格。

现在我需要做的一切,直到接下来需要完成的两件事情,这是我无法继续的地方。

更新标题表中的图书,以按数量减少库存数量 更新销售表中的销售记录小计,总计和商品及服务税字段,以包括所购书籍的销售金额。

这是我的拥有: 的 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 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

1 个答案:

答案 0 :(得分:3)

此更新如何:

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

NumberInStock是一个列,而不是@参数。

此外,你也可以这样做,但你必须创建@total和@GST变量:

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

我认为你的第一个SELECT查询不是很有用。您的程序将打印出所有内容。但是你已经拥有了所需的参数,对吗?它们是输入参数。我希望这有帮助吗?