Unable to execute Trigger in mysql - mysql

I have tables like CallLogs and Activities.Here,My requirement is for each insertion in CallLogs i need an entry in Activities.for that i am writing a trigger and procedure
I am calling procedure from my trigger
Procedure
DELIMITER $$
CREATE PROCEDURE `activity_insert` (IN text TEXT,IN type varchar(250),IN subtype varchar(250),IN date timestamp, IN url TEXT,IN target_id INT(16))
BEGIN
Insert into Activities(text,type,subtype,timestamp,url,Target_ID)values(in_text,in_type,in_subtype,in_date,in_url,in_target_id);
END $$
DELIMITER ;
It is executed successfully
Trigger
DROP TRIGGER IF EXISTS `call_logs_insert`
DELIMITER $$
CREATE TRIGGER `call_logs_insert` AFTER INSERT ON `CallLogs`
FOR EACH ROW
BEGIN
CALL activity_insert('message','Calls',NEW.'type',NEW.'date','null',NEW.'Target_ID');
END;
$$
for some of the columns i am giving static data. and for some of columns i am using new table data
When i am trying to execute this trigger i am getting a following error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''type',NEW.'date','null',NEW.'Target_ID'); END; $$' at line 4
Can any one plz tell me to execute this trigger what are the changes i need to do

Remove single quote from table columns
DROP TRIGGER IF EXISTS `call_logs_insert`
DELIMITER $$
CREATE TRIGGER `call_logs_insert` AFTER INSERT ON `CallLogs`
FOR EACH ROW
BEGIN
CALL activity_insert('message','Calls',NEW.type,NEW.date,'null',NEW.Target_ID);
END;
$$

Related

How to create a stored proceedure in mysql?

Just trying to make a procedure that copies one table into a new one but keep getting all sorts of errors..
eg:
SQL Error [1064] [42000]: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS ' at line 2
DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS SELECT * FROM raw_api_data
END$$
DELIMITER ;`
thanks
You have to end all commands with a semicolon or else mysql will search for more code
DELIMITER $$
CREATE PROCEDURE spClean_T()
BEGIN
CREATE TABLE staging_table AS SELECT * FROM raw_api_data;
END$$
DELIMITER ;
But if you don't have more commands
Make a simple without DELIMITER
CREATE PROCEDURE spClean_T()
CREATE TABLE staging_table AS SELECT * FROM raw_api_data;

how to insert rows via loop in MySQL shell (no PHP)

I would like to execute a loop in phpmyadmin which inserts rows in a table. So far I have:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows()
With this, I get an error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DELIMITER$$
DROP PROCEDURE IF EXISTS insert_my_rows()
CREATE PROCEDURE ins' at line 1
Syntax for the DROP PROCEDURE statement is incorrect!
Change
DROP PROCEDURE IF EXISTS insert_my_rows()
to
DROP PROCEDURE IF EXISTS insert_my_rows;
You need to end the statement with the proper delimiter. Change END to END$$.
You must change the delimiter only while you make blocks of statements, so during the procedure definition. The DROP PROCEDURE and CALL statements needs delimiters too.
DROP PROCEDURE IF EXISTS insert_my_rows;
DELIMITER $$
CREATE PROCEDURE insert_my_rows()
BEGIN
DECLARE i INT DEFAULT 376;
WHILE i<405 DO
INSERT INTO wp_term_relationships(object_id,term_taxonomy_id,term_order) VALUES (i,16,0);
SET i=i+1;
END WHILE;
END $$
DELIMITER ;
CALL insert_my_rows();
DROP PROCEDURE IF EXISTS insert_my_rows;

SQL trigger giving error

I'm trying to call a stored procedure to create multiple rows using some of the cell values of the new row inserted in the table resource but I'm getting an error. Here is my query:
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END;
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
What am I doing wrong?
delimiter |
CREATE TRIGGER `details` AFTER INSERT ON `resource`
FOR EACH ROW BEGIN
CALL addcopies(NEW.copies, NEW.id, NEW.location);
END
|
delimiter ;
If you don't set another delimiter than ;, then the statement will end at the first ; and your trigger definition will be incomplete. You need to tell MySQL that the statement should end at the delimiter you defined. After that you can set the delimiter back with delimiter ;

MySQL trigger syntax error near CREATE TRIGGER

I'm trying to create trigger in Mysql, and get the error:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TRIGGER `foo`.`test` AFTER INSERT ON `foo`.`test` FOR EACH ROW BE' at line 2
in this trigger:
DELIMITER $$
SET #name2:='w';
CREATE TRIGGER `foo`.`test`
AFTER INSERT
ON `foo`.`test`
FOR EACH ROW BEGIN
SELECT name1 INTO name2;
END;
$$
DELIMITER ;
What is wrong with line 2?
try this:
DELIMITER $$
CREATE TRIGGER `foo`.`test` AFTER INSERT
ON `foo`.`test`
FOR EACH ROW BEGIN
SET #name2:='w';
SELECT name1 INTO #name2;
END$$
DELIMITER ;

mysql trigger syntax error

I need the right syntax for mysql trigger I'm using version 5.1
and the syntax error alwys apper when I wrote sql statment
DELIMITER $$
CREATE TRIGGER blood_year
AFTER INSERT ON donor
FOR EACH ROW
BEGIN
INSERT INTO blood_donation (donation_year)VALUES
(YEAR(NOW()));
END$$
any idea??
Try adding another "DELIMITER" statement at the end:
DELIMITER $$
CREATE TRIGGER blood_year
AFTER INSERT ON donor
FOR EACH ROW
BEGIN
INSERT INTO blood_donation (donation_year)VALUES (YEAR(NOW()));
END$$
DELIMITER ;