How execute procedure in trigget INSERT AFTER mysql? - mysql

I found a lot questions here, for example:
Execute procedure in a trigger
But can not post own comment here for specify some moments.
I have trigger INSERT AFTER, in phpmyadmin I wrote:
...
BEGIN
insert_log(1, :new.idArticle, :new.ArticleName, :new.ArticleTime, :new.ArticleCategory, :new.ArticleToUserID);
END
...
I get error:
MySQL: #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 '(1, :new.idArticle, :new.ArticleName,
:new.ArticleTime, :new.ArticleCategory, :n' at line 2
Can you advise me what is wrong?

Try this:
CREATE TRIGGER your_trigger
AFTER INSERT
ON table_name FOR EACH ROW
BEGIN
insert_log(1, NEW.idArticle, NEW.ArticleName, NEW.ArticleTime, NEW.ArticleCategory, NEW.ArticleToUserID);
END;
Also note, MySQL's documentation page titled, "Trigger Syntax and Examples" states:
Within the trigger body, the OLD and NEW keywords enable you to access columns
in the rows affected by a trigger. OLD and NEW are MySQL extensions to triggers;
they are not case sensitive.
Please let me know if you have any questions!

Related

simple mysql trigger, cannot find the syntax error. appreciate for it

I am using phpmyadmin and got a syntax error from my trigger part. I followed examples online but didn't see anything wrong. Does anyone see the syntax error? Thank you in advance.
Here is my code:
CREATE TRIGGER INSERT_T BEFORE INSERT ON dataT
for each row begin
IF(NEW.id IS NOT NULL)
SET NEW.id = 9999;
END IF
end;
mysql reports that:
#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 'SET NEW.id = 9999' at line 4
.
Missing THEN keyword.
MySQL Reference Manual:
IF Syntax https://dev.mysql.com/doc/refman/5.7/en/if.html
And for your next questions. Yes, you need a semicolon following the END IF
And you need to specify a delimiter other than the default semicolon. e.g.
DELIMITER $$
CREATE TRIGGER ...
END$$
DELIMITER ;

MySQL error after updating on

Here i am trying to insert into payment_table after updating the review_table only after the status in review_table is updated to 2 .But there is error after running this trigger i.e,
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the
Trigger is:
CREATE TRIGGER payment_details_trig AFTER UPDATE ON review_table
FOR EACH ROW
BEGIN
IF NEW.status=2
INSERT INTO payment_table(paper_id, payment_mode, payment_refNo, payment_image,status)
VALUES (0,0,0,0,0);
END IF;
END;
Nothing sticks out except for the missing THEN. You might need delimiter statements:
DELIMITER $$
CREATE TRIGGER payment_details_trig AFTER UPDATE ON review_table
FOR EACH ROW
BEGIN
IF NEW.status = 2 THEN
INSERT INTO payment_table(paper_id, payment_mode, payment_refNo, payment_image,status)
VALUES (0, 0, 0, 0, 0);
END IF;
END;
$$
DELIMITER ;
The issue 1064 in SQL comes in following four cases:
1) Use of reserved words:
Every version of MySQL has its own list of reserved words. These are words that are used for specific purposes or perform specific functions within the MySQL engine. If you attempt to use one of these reserved words, you will receive the 1064 error.To fix the issue, you will want to surround the word with backticks “`”.
2) Missing Data:
Sometimes data in the database is missing. This can cause issues when this data is required for a query.
3) Mistyped or missing Commands:
One of the most common causes for the 1064 error is when a SQL statement uses a mistyped or missing command.
4) Deprecated Commands:
Some commands that were deprecated (slated for removal but still allowed for a period of time) eventually go obsolete. This means that the command is no longer valid in the SQL statement.
In your case third option is the answer is the reason of your error. You have missed writing THEN after IF.

MySQL CREATE TRIGGER and DELIMITER ISSUES

I am trying to make a database that does some inventory checking, and I am working on some triggers to update everything as required. In this case, that I want to do is to take the current availability (disponibilidad) of an ingredient from the ingredients table, add to it how much is taken or added, and then save that value into the table stock_disponibilidad, as you can see from the trigger code I am trying to use.
DELIMITER $$
CREATE TRIGGER `update_disponibilidad_variacion` BEFORE INSERT ON `stock_disponibilidad`
FOR EACH ROW BEGIN
DECLARE old_disp DOUBLE DEFAULT 0;
SELECT disponibilidad INTO old_disp FROM ingredientes WHERE id = NEW.ingredientes_id;
NEW.disponibilidad = old_disp + NEW.variacion;
END$$
DELIMITER ;
However, whenever I execute the query to create the trigger, I get the 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 '.disponibilidad = old_disp + NEW.variacion; END' at line 5
I've done everything I read about this issue, still without result. What am I doing wrong?

The MySQL "DELIMITER" keyword isn't working

Ok so, I've been ripping my hairs ou on this one, why doesn't this work?
DELIMITER |
CREATE PROCEDURE Decrypt_pw()
READS SQL DATA
BEGIN
SELECT 'Hey Select';
END|
It's so basic and I'm pretty sure I'm using the correct syntax, what am I missing?
Error:
21:14:07 [DELIMITER - 0 row(s), 0.000 secs] [Error Code: 1064, SQL State: 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 Decrypt_pw()
READS SQL DATA
BEGIN
SELECT 'He' at line 1
21:14:07 [END| - 0 row(s), 0.000 secs] [Error Code: 1064, SQL State: 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 'END|' at line 1
I'm using DbVisualizer, latest version, could this problem be with the software itself?
Perhaps I should explain myself better, the passwords are encrypted in my database (no need to worry), and this allows me to decrypt them, this is for a personal project I'm working on.
I was trying to develop a script that would allow me to run it and set up the necessary databases, tables, etc for that to work, and I require some SPs which must also be created, I'm trying to create an SP through a mysqli_query, is that even possible?
Basically it's for a "setup script" of a php application.
UPDATE: Seems that this is supposed to work, however I can't use objects due to the guys at HostGator -.- not allowing for objects in PHP.
I Have pretty much given up on mysqli since it's just not going to work I'm trying with shell_exec, I'm creating the procedure but when I check the ddl it's empty, it's creating empty procedures but at least it's doing something...
it is probaly a software version problem... i tried your code and it works just fine for me...
try this
DELIMITER //
CREATE PROCEDURE Decrypt_pw()
READS SQL DATA
BEGIN
SELECT 'Hey Select';
END //
DELIMITER ;
At least as of 9.1, DBVisualizer doesn't support the DELIMITER keyword. Here's the way they do it: link.
Definitely Not an elegant work-around ... but it works.
All the usual caveats about not shelling out, yada yada yada.
// here's the core stored procedure code
$stored = <<<EOT
CREATE PROCEDURE Decrypt_pw()
READS SQL DATA
BEGIN
SELECT * FROM whatever;
END #
EOT;
// first, shell out to change the delimiter using mysql command-line
shell_exec('mysql -u user -ppassword -e "DELIMITER #");
// assuming $pdo is a valid PDO connection
// send the command to create the stored procedure:
$pdo->exec($stored);
// now shell out again to change the delimiter back
shell_exec('mysql -u user -ppassword -e "DELIMITER ;");
Try putting space between 'DELIMITER' and '|'.
It worked for me.
DELIMITER | --here
CREATE TRIGGER my_trigger BEFORE INSERT
ON employee
FOR EACH ROW BEGIN
INSERT INTO trigger_test VALUES('added new employee');
END |
DELIMITER;

MySQL stored proc not getting created

So I have this stored proc that will not get created when I run the file.
DELIMITER //
DROP PROCEDURE IF EXISTS msd.test_proc//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM
msd.zipcode_lookup;
END//
DELIMITER ;
When I run this I get an error code 1064 at line 1 when I execute in RazorSQL. Here is the complete error message:
ERROR: 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 '//
CREATE PROCEDURE msd.test_proc()
BEGIN
SELECT
'Hello proc'
FROM ' at line 1
Error Code:1064
I've tried other variations and still get errors. I am sure this is something basic I am missing. I appreciate any help.
Thanks.
As stated on the RazorSQL website:
The DELIMITER statement is not part of the MySQL language. It is a command supported by certain MySQL tools. This command tells those MySQL programs to scan for a certain character that indicates the end of a query or statement.
RazorSQL does not support using the DELIMITER command. The SQL statement delimiter value used by RazorSQL can be changed using the preferences window. The default values is the semi-colon.