Nick Price
asked on
syntax error with mySQL script with Trigger
I've got a script in a file , while I'm running in .net using the mySQL connector.
It was all working file, until I tried to add a trigger. Now I keep getting syntax error
It was all working file, until I tried to add a trigger. Now I keep getting syntax error
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
CREATE TABLE IF NOT EXISTS `basestation`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`imei` int(11) NOT NULL,
`type` int(11) NOT NULL DEFAULT 0,
`Install_Date` date NOT NULL,
PRIMARY KEY (`id`),
KEY `type` (`type`),
CONSTRAINT `basestation_ibfk_1` FOREIGN KEY (`type`) REFERENCES `basestation_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DELIMITER //
CREATE TRIGGER `inst_date` BEFORE INSERT ON `basestation`
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END;//
DELIMITER ;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
http://stackoverflow.com/questions/5814153/error-in-your-sql-syntax-creating-mysql-trigger
remove ; after end
DELIMITER //
CREATE TRIGGER `inst_date` BEFORE INSERT ON `basestation`
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END//
DELIMITER ;
DELIMITER //
CREATE TRIGGER `inst_date` BEFORE INSERT ON `basestation`
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END//
DELIMITER ;
ASKER
Hi,
Removed the ; after end. But still the same error
Removed the ; after end. But still the same error
- take out the single quote in inst_date and basestation:
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END;
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END;
- take out the ; after END too:
DELIMITER //
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END
//
DELIMITER;
DELIMITER //
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END
//
DELIMITER;
ASKER
nope, this not working
DELIMITER //
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END//
DELIMITER ;
- the syntax looks ok, but i suspect the single quote might be the reason. in your earlier code, did you copy paste the single quote or type it?
- if you run the create table section only without the trigger does it work?
DELIMITER //
CREATE TRIGGER 'inst_date' BEFORE INSERT ON 'basestation'
FOR EACH ROW BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END;//
DELIMITER;
- if you run the create table section only without the trigger does it work?
- change the begin to be on next line:
DELIMITER //
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW
BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END;//
DELIMITER;
ASKER
Yes, it works without the trigger.
ASKER
Putting the begin on a new line didn't work.
my current code below
my current code below
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET NAMES utf8 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
CREATE TABLE IF NOT EXISTS `basestation`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`imei` int(11) NOT NULL,
`type` int(11) NOT NULL DEFAULT 0,
`Install_Date` date NOT NULL,
PRIMARY KEY (`id`),
KEY `type` (`type`),
CONSTRAINT `basestation_ibfk_1` FOREIGN KEY (`type`) REFERENCES `basestation_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DELIMITER //
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW
BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END//
DELIMITER ;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
- test if u can run the trigger separately without the delimiter:
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW
BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END;
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW
BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END;
ASKER
ASKER
Has everyone been stumped?
not yet. taking a timeout watching priest movie just now ;)
- ok do this. take out the delimiter part from the script and run it again.
- ok do this. take out the delimiter part from the script and run it again.
ASKER
Hi,
Not sure what your asking me to do.
If i take out the trigger from the file (which contains all the scripts) it works.
If I put the trigger script into the SQL query part of PHPmyAdmin it works fine.
See the last screen shot I did.
I'm going home now, but will be in the office tomorrow (yes, working Saturday)
Cheers
Nick
Not sure what your asking me to do.
If i take out the trigger from the file (which contains all the scripts) it works.
If I put the trigger script into the SQL query part of PHPmyAdmin it works fine.
See the last screen shot I did.
I'm going home now, but will be in the office tomorrow (yes, working Saturday)
Cheers
Nick
- ok lets continue tomorrow.
- i suspect might it be the delimiter?:
- i suspect might it be the delimiter?:
CREATE TABLE IF NOT EXISTS `basestation`
(
`id` int(11) NOT NULL AUTO_INCREMENT,
`imei` int(11) NOT NULL,
`type` int(11) NOT NULL DEFAULT 0,
`Install_Date` date NOT NULL,
PRIMARY KEY (`id`),
KEY `type` (`type`),
CONSTRAINT `basestation_ibfk_1` FOREIGN KEY (`type`) REFERENCES `basestation_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
delimiter $$
CREATE TRIGGER inst_date BEFORE INSERT ON basestation
FOR EACH ROW
BEGIN
IF NEW.Install_Date = '0000-00-00 00:00:00' THEN
SET NEW.Install_Date = NOW();
END IF;
END$$
delimiter ;
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Blow me over, it worked without the delimiter.!!!!!!
Go figure...
Many thanks.
You got the points :)
Go figure...
Many thanks.
You got the points :)
- great! :) since you are running it on .net mySQL connector, it did understand the delimiter.
i mean it might did not understand the delimiter.