Error creating MySQL Trigger - php

I'm trying to create this trigger but i'm getting
[Err] 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 '; CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW BEGIN ' at line 1
delimiter $$
DROP TRIGGER IF EXISTS ca_passwd_trigger ;
$$
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END;$$
delimiter ;

In your query you added the query terminator ; with the delimiter $$ in two places. The below query is having proper query terminators and delimiters.
DELIMITER $$
DROP TRIGGER IF EXISTS ca_passwd_trigger; -- removed the delimiter $$ after the terminator ;
CREATE TRIGGER ca_passwd_trigger BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF ((NEW.passwd <=> OLD.passwd) = 0) THEN
SET NEW.passwd_modified_at = NOW();
END IF;
END$$ -- removed the terminator ; before the delimiter $$
DELIMITER ;

Related

Loop in phpmyadmin don't work

I try to make loop in MySQL, but it always shows me an error.
Is it correct code or did I make a mistake?
DROP PROCEDURE IF EXISTS proc_loop_test;
CREATE PROCEDURE proc_loop_test()
BEGIN
DECLARE int_val INT DEFAULT 0;
test_loop : LOOP
IF (int_val = 10) THEN
LEAVE test_loop;
END IF;
SET int_val = int_val +1;
SELECT int_val;
END LOOP;
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
The problem is that the default delimiter is ; and stops the execution/parsing of the procedure code. Change the delimiter before you create the procedure and set it back to ; after the procedure code like this:
DROP PROCEDURE IF EXISTS proc_loop_test;
DELIMITER #
CREATE PROCEDURE proc_loop_test()
BEGIN
...
END #
DELIMITER ;

MySQL Workbench 6.2 create function

I'm trying to create a function in MySQL workbench, I have this SQL code:
DELIMITER $$
CREATE FUNCTION `regex_replace` (pattern VARCHAR(1000),replacement VARCHAR(1000),original VARCHAR(1000))
RETURNS VARCHAR(1000)
DETERMINISTIC
BEGIN
DECLARE temp VARCHAR(1000);
DECLARE ch VARCHAR(1);
DECLARE i INT;
SET i = 1;
SET temp = '';
IF original REGEXP pattern THEN
loop_label: LOOP
IF i>CHAR_LENGTH(original) THEN
LEAVE loop_label;
END IF;
SET ch = SUBSTRING(original,i,1);
IF NOT ch REGEXP pattern THEN
SET temp = CONCAT(temp,ch);
ELSE
SET temp = CONCAT(temp,replacement);
END IF;
SET i=i+1;
END LOOP;
ELSE
SET temp = original;
END IF;
RETURN temp;
END;
$$
DELIMITER ;
When I put this in phpMyAdmin, my function is created without any problems.
When I click on "Add Routine" in MySQL Workbench and then paste this code, I get this error:
Syntax error: 'DELIMITER' (identifier) is not valid input at this position
I'm no expert on MySQL functions, so I don't really know what to do with this. What is the problem here?
I really want to fix this, because MySQL Workbench now shows my function as _SYNTAX_ERROR instead of regex_replace.
When using the builtin procedure editor, MySQL Workbench adds a few extra commands:
USE `test`; // <----------
DROP procedure IF EXISTS `p2`; // <----------
DELIMITER $$
USE `test`$$ // <----------
CREATE PROCEDURE test.`p2` ()
LANGUAGE SQL
DETERMINISTIC
COMMENT 'Adds "nson" to first and last names in the record.'
BEGIN
SELECT 'Hello World';
END $$
DELIMITER ; // <----------
If you apply the above slashes to your procedure, it should help :)
For reference:
Stored Procedures Using MySQL Workbench

MYSQL Trigger Not Working. Not sure why

I am trying to create a trigger for my table named quailmiles. When the total miles hits a certain number I want the persons status (which is a attribute in that same table) to change. For some reason my code is not working.
The error currently reads:
#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 9.
delimiter $$
CREATE TRIGGER status_update
AFTER UPDATE ON quailmiles
FOR EACH ROW BEGIN
IF (total_miles > 2000) THEN
UPDATE quailmiles SET status = 'gold';
ELSE IF (total_miles > 4000) THEN
UPDATE quailmiles SET status = 'platinum';
END IF;
END $$
delimiter;
Your logic is backwards. You need to test against 4000 before 2000:
delimiter $$
CREATE TRIGGER status_update
BEFORE UPDATE ON qualmiles
FOR EACH ROW
BEGIN
IF (NEW.total_miles > 4000) THEN
SET NEW.status = 'platinum';
ELSEIF (NEW.total_miles > 2000) THEN
SET NEW.status = 'gold';
END IF;
END $$
delimiter;
I also changed the syntax to be a "before" update trigger. Also, I made the name "qualmiles". "Quailmiles" sounds funny.
I think you want ELSEIF rather than ELSE IF. (Remove the space.)
The error is being returned because that second IF isn't being ended with an END IF.
Also, you don't really want to issue an UPDATE against the same table. You'd rather this be handled in a BEFORE UPDATE trigger, and set the value of the column.
e.g.
CREATE TRIGGER status_update
BEFORE UPDATE ON quailmiles
FOR EACH ROW
BEGIN
IF (NEW.total_miles > 4000) THEN
SET NEW.status = 'platinum';
ELSIF (NEW.total_miles > 2000) THEN
SET NEW.status = 'gold';
END IF;
END$$

sql procedure import error

DELIMITER $$
drop procedure IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc`(OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END $$
DELIMITER ;
I am trying to import the above code using PHP but I'm getting this error:
Error performing query '':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 simpleproc$$ CREATE PROCEDURE `simplepro' at line 1
DELIMITER $$
DROP PROCEDURE IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc`(OUT param1 INT)
READS SQL DATA
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END $$
DELIMITER ;$$
The above syntax can only be executable at MySQL command line client.
The same won't work using server side scripting language.
Instead you can use the following as a string to construct the query and pass to mysqli->query
CREATE PROCEDURE simpleproc (OUT param1 INT )
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END;
Note:
If you have only a single statement to be processed and executed in a stored procedure,
you don't even need begin - end blocks.
This is correct:
CREATE PROCEDURE simpleproc (OUT param1 INT )
SELECT COUNT(*) INTO param1 FROM t;
You have an error in your procedure syntax, please use following
DELIMITER $$;
DROP PROCEDURE IF EXISTS `simpleproc`$$
CREATE PROCEDURE `simpleproc` (OUT param1 INT)
BEGIN
SELECT COUNT(*) INTO param1 FROM t;
END$$
DELIMITER ;$$
This will solve your problem

Trigger - AFTER INSERT - GIVING syntax error

CREATE TRIGGER giveaccess AFTER INSERT ON `befree_user`
FOR EACH ROW BEGIN
INSERT INTO user_access(user_id) VALUES (NEW.user_id);
END;
you need to define another delimiter than ;
delimiter |
CREATE TRIGGER giveaccess AFTER INSERT ON `befree_user`
FOR EACH ROW BEGIN
INSERT INTO user_access(user_id) VALUES (NEW.user_id);
END;
|
delimiter ;
If not the DB thinks that after the first ; the statement is done and will throw an error.

Categories