DELIMITER $$
CREATE PROCEDURE update_salary()
BEGIN
UPDATE employee SET salary = salary*1.4 where e_desc = 'Senior Manager';
UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';
UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';
End $$
DELIMITER ;
I create stored procedure to My_sql to update tables values. It is working but, I want to change this to update only one of set of rows.
e.g. When I give 'Manager' update only that column.
How can i do this?
you can do this with an IF statement inside your stored procedure like so..
DELIMITER $$
CREATE PROCEDURE update_salary(IN employeeType VARCHAR(255))
BEGIN
IF (employeeType = "Seinor Manager") THEN
UPDATE employee SET salary = salary*1.4 where e_desc = 'Senior Manager';
ELSEIF (employeeType = "Manager") THEN
UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';
ELSE
UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';
END IF;
End $$
DELIMITER ;
or you could do it with a case statement too if that suites you better
DELIMITER $$
CREATE PROCEDURE update_salary(IN employeeType VARCHAR(255))
BEGIN
CASE employeeType
WHEN "Seinor Manager")
THEN UPDATE employee SET salary = salary*1.4 where e_desc = 'Senior Manager';
WHEN "Manager"
THEN UPDATE employee SET salary = salary*1.2 where e_desc = 'Manager';
ELSE
UPDATE employee SET salary = salary*1.1 where e_desc = 'Assistant manager';
END;
End $$
DELIMITER ;
Related
i am setting triggerafter_inser and i dont know how to resolve it
DELIMITER $$
USE `solocloud_dev_2`$$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW BEGIN
DECLARE var INT;
SET var=0;
SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id;
IF var > 0
THEN
UPDATE table_10_summary SET records = records+1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary (new.client_id,1);
END$$
DELIMITER$$
There are several problems with your trigger :
you need to SET var from the results of the query ; as it is now, you are not setting this value
the IF block must be terminated with END IF
The following codes compiles successfully in this DB Fiddle :
DELIMITER $$
DROP TRIGGER IF EXISTS table_10_summary_after_insert $$
CREATE TRIGGER table_10_summary_after_insert AFTER INSERT ON table_10
FOR EACH ROW
BEGIN
DECLARE var INT;
SET var = (SELECT COUNT(*) FROM table_10_summary WHERE client_id = new.client_id);
IF (var > 0) THEN
UPDATE table_10_summary SET records = records + 1 WHERE client_id = new.client_id;
ELSE
INSERT INTO table_10_summary VALUES(new.client_id,1);
END IF;
END$$
DELIMITER ;
I am new to MySQL trigger. I want to make a trigger that before the update is made from my emp_audit table will check first if the empno from my employees table already exists. If it exists then do the update otherwise do nothing.
I encounter the following error:
Error 1064
The code is:
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
empno = NEW.empno,
lastname = NEW.lastname,
changedat = NOW();
END IF;
END$$
DELIMITER;
The UPDATE syntax does not include the INTO keyword.
Please check if the below updated query works.
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
empno = NEW.empno,
lastname = NEW.lastname,
changedat = NOW()
WHERE empno = NEW.empno;
END IF;
END$$
DELIMITER;
I think, you can even remove the empno from you SET(update) list and try the below query,
DELIMITER $$
CREATE TRIGGER before_emp_audit_update
BEFORE UPDATE ON emp_audit
FOR EACH ROW BEGIN
IF (SELECT COUNT(*) FROM employees WHERE empno = NEW.empno) = 1
THEN
UPDATE INTO emp_audit
SET ACTION = 'update',
lastname = NEW.lastname,
changedat = NOW()
WHERE empno = NEW.empno;
END IF;
END$$
DELIMITER;
I have created a stored procedure in which I want to list employees according to the parameter.
The procedure has some parameters, one of them I want to use for the type(eg:branch, department etc) and the other is the id corresponding to that.
I want to create where conditions to variable.
How can I do that?
DELIMITER $$
CREATE PROCEDURE Emp_details(IN div_type VARCHAR(100), IN id INT, OUT emp_name VARCHAR(100))
BEGIN
DECLARE cond VARCHAR(100);
IF div_type = 'branch' THEN
SET cond = 'branch_id='+id;
ELSEIF div_type = 'department' THEN
SET cond = 'department_id='+id;
ELSEIF div_type = 'emp' THEN
SET cond = 'employee_master_id='+id;
ELSE
SET cond = '';
END IF;
SELECT
emp_fullname INTO emp_name
FROM armr_employee_master
WHERE +cond ;
END$$
Just turn the IF into a WHERE:
SELECT emp_fullname
INTO emp_name
FROM armr_employee_master
WHERE id = CASE div_type
WHEN 'branch' THEN branch_id
WHEN 'department' THEN department_id
WHEN 'emp' THEN employee_master_id
END
I wrote a trigger which get trigered after update statement of table1. If there is no row in table1 based on the conditions, then update table2.
CREATE TRIGGER trigger1
AFTER UPDATE ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF new.column1 = 1 THEN
SELECT COUNT(1) INTO #cnt FROM my_database.table1
WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2
SET isDeleted = 1
WHERE userSeqID = new.userID;
END IF
It gives me error i nline number 4, I couldn't understand the problem
Every IF statement has to ended with END IF clause. Try this statement -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER UPDATE
ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF NEW.column1 = 1 THEN
SELECT COUNT(1) INTO cnt FROM my_database.table1 WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2 SET isDeleted = 1 WHERE userSeqID = NEW.userID;
END IF;
END IF;
END$$
DELIMITER ;
The error on line 4 is because you're not using a delimiter.
Try this:
DELIMITER //
create trigger trigger1 after update on my_database.table1
for each row
begin
declare cnt int;
...
end if;
END;//
DELIMITER ;
You'll also need an END IF on the line after the select statement, and a terminal END; to match your begin, as I have in my example.
I want to update counter table when user table is modofied.
I have created a trigger where i will cal this procedure.
But this procudeure is not creating.
Please tell me what error in this procedure.
CREATE PROCEDURE barproc(IN id int,IN val int)
BEGIN
DECLARE #total_products int;
set #total_products=(SELECT COUNT(*)
FROM user where a=id and status='active')
if(#total_products>0)
update counter
set b=val
WHERE a = id and status='active';
END if;
Try
DELIMITER $$
CREATE PROCEDURE barproc(IN id INT, IN val INT)
BEGIN
IF (SELECT COUNT(*)
FROM `user`
WHERE a = id AND `status` = 'active') THEN
UPDATE counter
SET b = val
WHERE a = id AND `status` = 'active';
END IF;
END$$
DELIMITER ;