This is my credit leave table.
creditleaveid empid creditleavemonth creditedleaves
1 21 March 1
I am using scheduler to insert next month to credit/balance leave in this table is
DELIMITER $$
CREATE EVENT creditleavemonth
ON SCHEDULE EVERY '1' MONTH
STARTS '2017-02-01 00:00:00'
DO
BEGIN
CALL spAddCreditLeaves;
END$$
DELIMITER ;
stored procedure spAddCreditLeaves is
DELIMITER $$
USE `attendance`$$
DROP PROCEDURE IF EXISTS `spAddCreditLeaves`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `spAddCreditLeaves`()
BEGIN
INSERT INTO creditleaves(empid,creditleavemonth)SELECT empid,MONTHNAME(CURDATE())FROM empdetails;
END$$
DELIMITER ;
Now I want to update a record of inserted month by checking previous month's record for every employee. How to do that?
I have got my answer. It is working awesomely. stored procedure-
USE `attendance`$$
DROP PROCEDURE IF EXISTS `spAddCreditLeaves`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `spAddCreditLeaves`()
BEGIN
IF MONTHNAME(CURDATE())='January'
THEN
INSERT INTO creditleaves(empid,creditleavemonth,creditmonthyear,creditedleaves) SELECT empid,MONTHNAME(CURDATE()),YEAR(CURDATE()),creditedleaves+1 FROM creditleaves WHERE empid IN (SELECT empid FROM empdetails GROUP BY empid) AND creditedleaves IN(SELECT creditedleaves FROM creditleaves WHERE creditleavemonth=MONTHNAME((CURDATE()-INTERVAL 1 MONTH)) AND creditmonthyear=YEAR((CURDATE()-INTERVAL 1 YEAR)));
ELSE
INSERT INTO creditleaves(empid,creditleavemonth,creditmonthyear,creditedleaves) SELECT empid,MONTHNAME(CURDATE()),YEAR(CURDATE()),creditedleaves+1 FROM creditleaves WHERE empid IN (SELECT empid FROM empdetails GROUP BY empid) AND creditedleaves IN(SELECT creditedleaves FROM creditleaves WHERE creditleavemonth=MONTHNAME((CURDATE()-INTERVAL 1 MONTH)) AND creditmonthyear=YEAR(CURDATE()));
END IF;
END$$
DELIMITER ;
Related
I'm trying to do a room capacity validation for a specific date using a before trigger.
This is the current trigger I'm using:
delimiter $$
create trigger my_insert_trigger before insert on my_table
for each row
begin
if (select count(*) from my_table where room_type = new.room_type) > 3 then
signal sqlstate '45000';
end if;
end;
$$
delimiter ;
I currently have two columns in the same table which are date and room, this is my desired output
However this would not be the case because the trigger will still limit 'Single' three times regardless of date.
do a room capacity validation for a specific date
Just add the date to the control query:
delimiter $$
create trigger my_insert_trigger before insert on my_table
for each row
begin
if (
select count(*)
from my_table t
where t.room_type = new.room_type and t.date = new.date
) >= 3 then
signal sqlstate '45000';
end if;
end;
$$
delimiter ;
Note: because it has condition > 3, your existing code would allow 4 records per room. I changed that to >= 3 so only 3 records are allowed per room and date, which seems to be what you are looking for.
I need to write a MySql Event to select some values from a table under some conditions and put those values in a second table. By the select statement. I get multiple rows, so I need to store data in the second table as a batch. How can I achieve this? I wrote an event to select one row. But what I need to do is select multiple rows and store as a batch.
The event I wrote is as below.
DELIMITER $$
CREATE EVENT salary_add
ON SCHEDULE EVERY 24 HOUR
DO
BEGIN
DECLARE month_end DATETIME;
DECLARE today DATETIME;
DECLARE reg_id VARCHAR(6);
DECLARE sal INT(8);
SET month_end = LAST_DAY(DATE(NOW()));
SET today = DATE(NOW());
IF month_end=today THEN
SELECT register_id,salary INTO reg_id,sal FROM employees
WHERE status ='1' LIMIT 1;
INSERT INTO tbl_salary (register_id,amount,salary_date,status) VALUES (reg_id,sal,today,'0');
END IF;
END $$
DELIMITER ;
You can insert selected rows into the target table at once. For example:
DELIMITER $$
CREATE EVENT salary_add
ON SCHEDULE EVERY 24 HOUR
DO
BEGIN
DECLARE month_end DATETIME;
SET month_end = LAST_DAY(CURDATE());
IF month_end=CURDATE() THEN
INSERT INTO tbl_salary (register_id, amount, salary_date, status)
SELECT register_id,
salary,
CURDATE(),
'0'
FROM employees
WHERE status ='1'
END IF;
END $$
DELIMITER ;
Directions
Write a proceedure called GetAllCustomerLevelsthat returns the customerNumber, customerName, and cardtype for all customers.
Hint: Use CASE in your select statement. See: Case in Select Statement at stackoverflow or MySQL CASE operator from w3resource. This could also be done using a nested IF. See: Inline IF and CASE statements in MySQL from timmurphy.
Below is an example proceedure that returns the customerNumber and customerName for all customers:
DROP PROCEDURE IF EXISTS GetAllCustomerLevels;
DELIMITER $$
CREATE PROCEDURE GetAllCustomerLevels()
BEGIN
select customerNumber, customerName
from customers;
END$$
DELIMITER ;
Save the sql that created the GetAllCustomerLevels procedure as GetAllCustomerLevels.sql
One of my Attempts
DROP PROCEDURE IF EXISTS GetAllCustomerLevels2;
DELIMITER $$
CREATE PROCEDURE GetAllCustomerLevels2()
BEGIN
DECLARE creditlimit double;
call GetCustomerLevel(#customerNumber,#cardType);
select customerNumber, customerName, #cardType
from customers
end;
SET #cardType =
(CASE
WHEN (creditlimit > 50000) THEN 'PLATINUM'
WHEN (creditlimit <= 50000 AND creditlimit >= 10000) THEN 'GOLD'
WHEN (creditlimit < 10000) THEN 'SILVER'
ELSE 'No card'
END
);
END$$
DELIMITER ;
My result returns customerName and customerNumber but #cardType stays empty.
I have a problem with my trigger before insert.
I try to control the row number of my table 'user_search' for one 'id_user' and if this row number > 4, the oldest row should be deleted.
My actual trigger is this :
DELIMITER $$
CREATE TRIGGER before_insert_user_search
BEFORE INSERT
ON user_search FOR EACH ROW
BEGIN
DECLARE sDate DATETIME;
SELECT MIN(date_search) FROM user_search WHERE id_user = NEW.id_user INTO sDate;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = NEW.id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE date_search = sDate;
END;
END IF;
END$$
DELIMITER ;
However, it does not work. Do someone have a solution to help me please.
Thanks.
You can not delete same table row during using INSERT trigger because it holds the lock of table. So try to write a Stored Procedure to insert new record.
Stored Procedure should be something like bellow:
CREATE PROCEDURE insert_user_search(new_id_user INT, ......other variable to inert)
BEGIN
DECLARE #minId INT;
//INSERT using parameter
SET #minId = SELECT MIN(id) FROM user_search WHERE id_user = new_id_user;
IF ((SELECT COUNT(id) FROM user_search WHERE id_user = new_id_user) > 4) THEN
BEGIN
DELETE FROM user_search WHERE id_user = #minId;
END;
END IF;
END;
I have a problem with a MySQL Trigger.
I have 3 tables Customers, Products and Sales.
In Sales I reference customer and product and I want to update the some counts on Products and Customers after a new sale is inserted.
The following trigger fails to update both tables... I cannot figure out what I am doing wrong.
DELIMITER $
CREATE TRIGGER OnSalesInsert AFTER INSERT ON Sales
FOR EACH ROW BEGIN
UPDATE Products SET Products.sold=Products.sold+NEW.amount WHERE Products.id=NEW.product_id;
UPDATE Customers SET Customers.amount=Customers.amount+NEW.amount WHERE Customers.id=NEW.customer_id;
END $
DELIMITER ;
Try this:
DELIMITER $$
CREATE
/*!50017 DEFINER = 'root'#'%' */
TRIGGER `OnSalesInsert` BEFORE INSERT ON `Sales`
FOR EACH ROW BEGIN
UPDATE Products
SET sold = sold + new.amount
WHERE id = new.product_id;
UPDATE Customers
SET amount = amount + new.amount
WHERE id = new.customer_id;
END;
$$
DELIMITER ;
DELIMITER $$
create trigger UpdateAvail after insert on product_details
for each row
Begin
Declare a1 INT;
Declare d1 VARCHAR(1);
Declare d2 VARCHAR(100);
Select count(0) INTO a1 from prod_available where P_Id=new.P_Id;
Select P_Name,P_Brand INTO d1,d2 from product where P_Id=new.P_Id;
IF a1>0 THEN
Update prod_available set P_quantity=P_quantity+new.quantity where P_Id=new.P_Id;
ELSE
insert into prod_available (P_Id,P_Name,P_Brand,P_quantity) values (new.P_Id,d1,d2,new.quantity);
END IF;
END;
$$
DELIMITER ;