Reputation: 43
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. The data that needs to be passed in is sale number, ISBN and quantity. And i have to RaiseErrors for the following things which i have already done. The ISBN and sale numbers are not valid The ISBN is already on that sale.
And if there aren't any errors i have to insert the Sale Detail record into SaleDetail table. The selling price will be the Suggested Price for that 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
Upvotes: 1
Views: 236
Reputation: 86
How about this update:
UPDATE Title
SET NumberInStock = NumberInStock - @Quantity
WHERE ISBN = @ISBN
The NumberInStock is a column, not a @ parameter.
Also, you can also do something like this, but you will have to create @total and @GST variables:
UPDATE Sale
SET subtotal = @amount,
total = @total,
GST = @GST
WHERE sale.saleNumber = @salenumber
I think your first SELECT query is not very useful. Your procedure will print out everything. But you already have the parameters you need, right? They are input parameters. I hope this helps?
Upvotes: 3