Click here to Skip to main content
15,886,045 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Great people,

It really awesome to see here people helping others out with their struggles. I have a little issue which doesn't seem to be working. I tried and googled to find a solution and this is how far I got.

What I want:
If a customer order a product X. The SQL trigger must check if there is enough of product X in stock and if it is not enough in stock it should give an error saying that there is not enough stock.

Example: Customer orders 5 bicycle but there is only 4 in stock: SQL saying: Stock is not sufficient.


SQL
alter PROCEDURE spNewProducts
 (	
	@describtion	VARCHAR(255),
	@stock		INTEGER,
	@distrubtur	VARCHAR(128)
)
AS
BEGIN
   Select * from products
   
      DECLARE @Productnr INT
      SELECT @Prodcutnr = MAX(productnr) + 1 FROM products
  
      INSERT INTO products(productnr, describtion, stock, distrubtur)
     VALUES (@productnr, @describtion, @stock, @distrubtur);

END)

GO
create table Order
(

Order_id		INTEGER,
Customer_id	INTEGER,
orderdate		DATE

constraint pk_Order
	primary key (Order_id)
);

GO

create table Order_row
(

Order_id		INTEGER,
Product_id	INTEGER,
quantity		INTEGER
)

go
create table ProductPrice
(

Productnr		INTEGER,
Price			DECIMAL(5,2),
begindate	        DATE,
Enddate		DATE,
)


DROP FUNCTION function() CASCADE ;
CREATE OR REPLACE FUNCTION function() 
RETURNS TRIGGER AS $trigger$
    BEGIN
      UPDATE product SET
      stock=COALESCE(stock,0)-Order_row.quantity
        WHERE Order_row.productnr=products.productnr;
        END;


CREATE TRIGGER trigger
    AFTER INSERT ON order
    FOR EACH ROW
    EXECUTE PROCEDURE function();


What am i doing wrong guys?
Thank you!
Posted
Updated 5-Oct-14 3:29am
v2
Comments
Kornfeld Eliyahu Peter 5-Oct-14 8:07am    
What is wrong?
[no name] 5-Oct-14 8:13am    
How would you expect us to know? We don't have access to your database or your monitor to see what it is that you are seeing.
George Jonsson 5-Oct-14 8:30am    
What is it you want to do? Start with describing your expected outcome.
Kutular 5-Oct-14 8:41am    
Indeed guys I am very sorry for being so unclear.

I want to build a inventory control on my db for my brothers shop. If my brother sells let's say: 10 cheese to a customer but there is only 8 in the stock. I want to SQL to give an error message saying that " There is not enough product in the stock".

So I want the database to check if the amount that is being ordered is in stock or not.

Thank you all for quick reply. Really awesome!
George Jonsson 5-Oct-14 9:22am    
Use the 'Improve question' button and add this info to your question.

1 solution

You can start with these pages for initial help.
An Introduction to Triggers -- Part I[^]
Triggers -- SQL Server[^]

But I think it is easier to do the logic inside the stored procedure instead, before you execute the INSERT statement.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900