[英]SQL, Updates,and Inserts
Hey so i'm supposed to write a procedure AddSaleDetail that will add a sale detail for a book purchased and will update the sale with that book information. 嘿,所以我应该写一个程序AddSaleDetail,它将为购买的书籍添加销售细节,并将使用该书籍信息更新销售。 The data that needs to be passed in is sale number, ISBN and quantity.
需要传递的数据是销售号,ISBN和数量。 And i have to RaiseErrors for the following things which i have already done.
我必须为我已经完成的以下事情进行RaiseErrors。 The ISBN and sale numbers are not valid The ISBN is already on that sale.
ISBN和销售号码无效ISBN已经在该销售中。
And if there aren't any errors i have to insert the Sale Detail record into SaleDetail table. 如果没有任何错误,我必须将Sale Detail记录插入SaleDetail表。 The selling price will be the Suggested Price for that ISBN.
售价将是该ISBN的建议价格。
Now everything i've got until the next two things that need to be done, this is where i'm unable to proceed. 现在我所做的一切,直到接下来需要完成的两件事,这是我无法继续的地方。
Update the book in the Title table to reduce the number in stock by the quantity Update the Sale record subtotal, total and GST fields in Sale table to include the sale amount of the book purchased. 更新标题表中的图书以减少库存中的数量按销售额表中的销售记录小计,总计和商品及服务税字段更新,以包括所购书籍的销售金额。
Here is what i have: 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)
Current 当前
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
How about this update: 这个更新怎么样:
UPDATE Title
SET NumberInStock = NumberInStock - @Quantity
WHERE ISBN = @ISBN
The NumberInStock is a column, not a @ parameter. NumberInStock是一个列,而不是@参数。
Also, you can also do something like this, but you will have to create @total and @GST variables: 此外,您也可以执行此类操作,但您必须创建@total和@GST变量:
UPDATE Sale
SET subtotal = @amount,
total = @total,
GST = @GST
WHERE sale.saleNumber = @salenumber
I think your first SELECT query is not very useful. 我认为你的第一个SELECT查询不是很有用。 Your procedure will print out everything.
您的程序将打印出所有内容。 But you already have the parameters you need, right?
但是你已经拥有了所需的参数,对吗? They are input parameters.
它们是输入参数。 I hope this helps?
我希望这有帮助?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.