I want to get the name in test table and split it form "to" word & insert that splitted string in abc table.
the trigger fired,but when insert data into test it generate
"#1172 - Result consisted of more than one row" error in phpMyAdmin.
please help me with this.
DROP TRIGGER IF EXISTS `split_after_insert`;
CREATE DEFINER=`root`#`localhost`
TRIGGER `split_after_insert` AFTER INSERT ON `test`
FOR EACH ROW
BEGIN
DECLARE vName varchar(50);
-- Find name of person performing the INSERT into table
SELECT SUBSTR(name,(POSITION("to" IN name)+4),20) FROM test INTO vName;
-- Insert record into abc table
INSERT INTO abc(a) VALUES (vName);
END
DROP TRIGGER IF EXISTS `j`;
CREATE DEFINER=`root`#`localhost`
TRIGGER `j` AFTER INSERT ON `test`
FOR EACH ROW
BEGIN
DECLARE vName varchar(100);
DECLARE vtemp varchar(100);
select new.name into vtemp;
-- Find name of person performing the INSERT into table
SELECT SUBSTR(vtemp,(POSITION("to" IN vtemp)+3),20) INTO vName;
-- Insert record into abc table
INSERT INTO `abc`(`a`) VALUES (vName );
END
Related
I am trying to write an MYSQL statement that will copy the patient_id where they have a value of "Dose: Second" and paste it to my table vaccinatedpatients.
Also, I wanted to implement this within a trigger to automatically update itself after each new record on the table.
Any help with this will be great!
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW BEGIN
SELECT #variable = patient_id from patient where vaccine_Dose = 'Dose: Second';
INSERT INTO vaccinatedpatients VALUES(variable);
END
You can use an IF cLause, to determines if the patient gets the second dose
DELIMITER &&
CREATE DEFINER=`root`#`localhost` TRIGGER `patient_AFTER_INSERT` AFTER INSERT ON `patient` FOR EACH ROW
BEGIN
IF NEW.vaccine_Dose = 'Dose: Second' THEN
INSERT INTO vaccinatedpatients VALUES (NEW.patinet_id);
END IF;
END&&
DELIMITER ;
The DELIMITER may not be needed.
I want to insert into one table selecting values from another table and some with paramters.
CREATE PROCEDURE `insertuser`(
IN `param1` VARCHAR(50)
)
BEGIN
insert into users(firstname,lastname,email)
select name,nickname,#param1 from members
where id=3;
END
I fill param1 but the query insert null to email
what I need to do?
# symbol is used to define Session variables (accessible to the Session everywhere). You don't need to use # here, as you are using the input parameter value from the Stored procedure.
Try the following instead:
DELIMITER $$
CREATE PROCEDURE `insertuser`(IN `param1` VARCHAR(50))
BEGIN
INSERT INTO users(firstname,lastname,email)
SELECT name,nickname,param1 -- remove # from #param1 here
FROM members
WHERE id=3;
END$$
DELIMITER ;
That's my table:
create table if not exists Ditte(nome varchar(30), luogo varchar(30), ZIP int);
That's my procedure:
delimiter //
create procedure deleteDitta(zip int)
begin
DECLARE newZIP int;
SET newZIP = zip;
DELETE from Ditte where ZIP = newZIP;
end;
//
delimiter ;
This is what I added in my table:
insert ignore into Ditte values ("Ditta1", "city1", 6828);
insert ignore into Ditte values ("Ditta2", "city2", 12345);
When I call my procedure deleteDitta and I put "12345" as parameter (like this: call deleteDitta(12345);), the procedure should deletes only the second row in table "Ditte", but it deletes all the contents of the table.
How can I fix it?
This seems to be MySQL getting confused about column names and variable names. Changing your procedure to this fixes the problem:
create procedure deleteDitta(dzip int)
begin
DELETE from Ditte where ZIP = dzip;
end;
Demo on dbfiddle
I have create a trigger where the first column and second column would be concatenated into 3rd column.
USE `customer_1`;
DELIMITER $$
CREATE DEFINER=`root`#`%` TRIGGER `Metric1_anpr_vega_BINS` BEFORE INSERT ON `Metric1_anpr_vega` FOR EACH ROW
BEGIN
SET NEW.image_id = CONCAT(NEW.camera_id,'-', NEW.id);
END
Id camera_id image_id
4567 236 236-0
( here i can't get id of the row when triggered it is '0' instead of 4567)
what trigger would get the ID value in image_Id after insert statment exceuted.
If i use after insert I get this error - Updating of NEW row is not allowed in after trigger
USE `customer_1`;
DELIMITER $$
CREATE DEFINER=`root`#`%` TRIGGER `Metric1_anpr_vega_BINS` AFTER INSERT ON `Metric1_anpr_vega` FOR EACH ROW
BEGIN
SET NEW.image_id = CONCAT(NEW.camera_id,'-', NEW.id);
END
SQLFiddle
I need to loop throught my cursor and insert new values in another table.
I have two tables:
CREATE TABLE peoples
(
id int auto_increment primary key,
name varchar(20),
enabled varchar(1)
)
/
INSERT INTO peoples
(name, enabled)
VALUES
('john', 'F'),
('jane', 'T')
/
CREATE TABLE test
(
id int,
logins int
)
/
I need to select all peoples enabled = T and insert a new entry in my second table 'test'. I created a stored procedure:
CREATE PROCEDURE sp_a()
BEGIN
DECLARE p int;
DECLARE done INT DEFAULT FALSE;
DECLARE peoples_cur CURSOR FOR select p.id from peoples p where p.enabled='T';
OPEN peoples_cur;
REPEAT
FETCH peoples_cur INTO p;
IF NOT done THEN
INSERT INTO test
(id, logins)
VALUES
(p, '999');
END IF;
UNTIL done END REPEAT;
CLOSE peoples_cur;
END;
/
But i got the error:
No data - zero rows fetched, selected, or processed: CALL sp_a()
SQLFiddle
Simple way:
DROP PROCEDURE IF EXISTS sp_a;
DELIMITER $$
CREATE PROCEDURE `sp_a`()
BEGIN
INSERT INTO test (logins)
SELECT COUNT(id) AS l FROM peoples WHERE enabled = 'T';
END$$
CALL sp_a();