Search code examples
mysqlif-statementconditional-statementsphpmyadmininsert

Insert value through procedure in a table by mysql


I want to insert values into a table by using a procedure. I have 2 tables sales and products; in the product table I have columns id, product_name,product_price, product_quantity and in sales table I have p_id and p_qty.

I want to use a procedure when a user add values in a sales table; this procedure should take 2 arguments for p_id and p_qty.

If p_qty is less than 0 or greater than product_quantity, then showstock is not available

enter image description here


Solution

  • here is my query

    DELIMITER //
    CREATE PROCEDURE find_qty_(
        product_id_proc INT,
        product_qty_proc INT
    )
    BEGIN
        DECLARE
            remaining_stock INT; 
            SELECT product_quantity INTO remaining_stock FROM products
            WHERE id = product_id_proc; 
            IF product_qty_proc>=0 OR product_qty_proc>remaining_stock THEN 
                BEGIN SELECT "stock not available";
                END;
            ELSE 
                INSERT into sales(p_id,p_qty)VALUES(product_id_proc,product_qty_proc);
            END IF; 
        END;;