I have a .bat file which executes the SQL file as follows.
The aim is that if records are found in a table, nothing will be done, whereas if the table is empty, some records will be inserted.
BEGIN
DECLARE rowCount INT;
SELECT count(*) FROM `martin1` INTO rowCount;
IF rowCount <= 5 THEN
END IF;
END;
But when I execute it, there is an error. I tried to delete the DECLARE, but even for (IF SELECT COUNT(*)...>0) there is still an error.
The error is,
ERROR 1064 (42000) at line 1: 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 'DECLARE rowCount INT' at line 2
How can I resolve this?
try this way
BEGIN
DECLARE rowCount INT;
SELECT count(*) INTO rowCount FROM `martin1`
IF rowCount <= 5 THEN
END IF;
END;
And have a look at this
Related
Does anyone see where the mistake can be? been dealing with this one all afternoon. Workbench isn't very useful in pointing out the mistakes.
It appears the next mistake in my code
CALL pCityMean('Spain') Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 2.
DELIMITER $$
DROP PROCEDURE IF EXISTS pCityMean $$
CREATE PROCEDURE pCityMean (IN vPais VARCHAR(30))
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE vID INT;
DECLARE cur1 CURSOR FOR SELECT distinct id_city FROM world_temp_stats.temp_by_city where id_country=(SELECT id_country FROM world_temp_stats.country where Name=vPais);
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
bucle1: LOOP
FETCH cur1 INTO vID;
IF done = 1 THEN
LEAVE bucle1;
END IF;
SET #nomCiutat = (SELECT name FROM world_temp_stats.city WHERE id = vID);
SET #tablaCiutat = CONCAT ("SELECT year(dt), count(AverageTemperature), ROUND(avg(AverageTemperature), 2) INTO OUTFILE '",UCASE(vPais),"_",UCASE(#nomCiutat),"_temp_analisis.csv' FIELDS TERMINATED BY ';'
LINES TERMINATED BY '\n' FROM world_temp_stats.temp_by_city where id_country=(SELECT id_country FROM world_temp_stats.country where Name='",vPais,"' and id_city=",vID," group by year(dt)");
PREPARE execucio FROM #tablaCiutat;
EXECUTE execucio;
DEALLOCATE PREPARE execucio;
END LOOP bucle1;
CLOSE cur1;
END $$
DELIMITER ;
CALL pCityMean('Spain');
I can eyeball your query and I see that you forgot a closing parenthesis.
SET #tablaCiutat = CONCAT ("SELECT ...
where id_country=(
SELECT id_country FROM world_temp_stats.country
where Name='",vPais,"' and id_city=",vID," group by year(dt)
");
I think you got mixed up because year(dt) ends in a right-paren, but you need one more to close the subquery. Something like the following:
SET #tablaCiutat = CONCAT ("SELECT ...
where id_country=(
SELECT id_country FROM world_temp_stats.country
where Name='",vPais,"' and id_city=",vID," group by year(dt)
)
");
You should also use query parameters for dynamic values in your prepared query, instead of string-concatenation.
But if it were me, I would change so many things about this code. Why use INTO OUTFILE, and why use a stored procedure at all? In my experience, MySQL stored procedures are more difficult than writing the code in virtually any client programming language. I hardly ever use MySQL stored procedures.
I am trying to execute trigger statement in MySql 5.1.41 but i am getting an error which is beyond my comprehension. Any help will be appreciated.
Following is the trigger code:
create TRIGGER populate_modality_trigger AFTER INSERT on series
FOR EACH ROW BEGIN
DECLARE x int;
select count(*) into x from lu_modality_alias l WHERE l.modality=NEW.modality;
IF (x =0 OR x is NULL) THEN
INSERT INTO lu_modality_alias
set modality = NEW.modality,
modality_alias = NEW.modality
;
END if;
end;
When I run above code, I get following error:
MySQL said: Documentation #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 interesting thing is that when i execute the same code from Naviacat query editor, it runs successfully and trigger is created.
But the same fails from phpMyAdmin sql.
You need to change the delimiter character so the MySql parser doesn't end the CREATE statement parsing at the first semi-colon
delimiter //
create TRIGGER populate_modality_trigger AFTER INSERT on series
FOR EACH ROW BEGIN
DECLARE x int;
select count(*) into x from lu_modality_alias l WHERE l.modality=NEW.modality;
IF (x =0 OR x is NULL) THEN
INSERT INTO lu_modality_alias
set modality = NEW.modality,
modality_alias = NEW.modality
;
END if;
end //
delimiter ;
I am using MySQL v5.0.92 and I'm trying to import some data, but I get an error on declare my variables.
BEGIN
DECLARE flag INT;
DECLARE id INT;
while flag=0 begin
SET id=(SELECT top 1 user_id
FROM ac_user_info WHERE user_id>#id order by user_id)
INSERT INTO cuddleew_database1.cewp_usermeta(user_id,meta_key,meta_value)
SELECT id,'first_name',first_name
FROM cuddleew_backup.ac_user_info WHERE user_id=#id
INSERT INTO cuddleew_database1.cewp_usermeta(user_id,meta_key,meta_value)
SELECT id,'last_name',last_name
FROM cuddleew_backup.ac_user_info WHERE user_id=#id
SET flag=(select case #id when(SELECT MAX(user_id)
FROM cuddleew_bakup.ac_user_info) then 1
else 0
END CASE)
END WHILE
END
In MySQL console i get this:
BEGIN DECLARE flag INT;
#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 'DECLARE flag INT' at line 2.
Can you help me with this and tell me what's wrong. Thanks in advance.
You have to change your delimiter first, so the ; doesn't tell MySQL that this command is over.
DELIMITER $$
[your code here]
END $$
DELIMITER ;
Some names and stuff changed to protect my work.
DELIMITER //
CREATE PROCEDURE glt(IN howMany INT)
BEGIN
DECLARE f VARCHAR(32);
DECLARE done INT DEFAULT 0;
DECLARE curs CURSOR FOR SELECT DISTINCT id FROM tpd;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN curs;
DROP TABLE IF EXISTS lt;
CREATE TEMPORARY TABLE lt LIKE tpd;
REPEAT
FETCH curs INTO f;
IF NOT done THEN
INSERT INTO lt SELECT * FROM tpd WHERE id = f ORDER BY TIME DESC LIMIT howMany;
END IF;
UNTIL done END REPEAT;
CLOSE curs;
END
The above code gives the following error on a linux machine, but not a mac machine despite both being case-sensitive filesystems and having the same MySQL version:
ERROR 1064 (42000) at line 172: 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 'howMany;
END IF;
UNTIL done END REPEAT;
CLOSE curs;
END' at line 16
Bye
Try to use Prepare Statement for INSERT query.
Look at http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
It seems it will look like:
PREPARE stmt FROM 'INSERT INTO lt SELECT * FROM tpd WHERE id = f ORDER BY TIME DESC LIMIT ?';
EXECUTE stmt USING howMany;
You cannot use a parameter in the limit clause here.
You can only do that when using PDO. In this context that's a syntax error.
I want to execute the following code in MySql but I am getting the error
CREATE TRIGGER CINEMAHALLNO_GENERATE_TRIGGER
BEFORE INSERT ON CINEMA_HALLS
FOR EACH ROW
BEGIN
DECLARE cnt DOUBLE;
DECLARE next1 DOUBLE;
SELECT COUNT(HALLNO) INTO cnt from CINEMA_HALLS;
IF cnt > 0 AND cnt < 20 THEN
SELECT MAX(HALLNO) INTO NEXT1 FROM CINEMA_HALLS;
set next1=next1+1;
SET :new.HALLNO=next;
ELSEIF cnt = 0 THEN
SET :new.HALLNO=1;
end if;
end;
Output Error:
SQL query:
CREATE TRIGGER CINEMAHALLNO_GENERATE_TRIGGER
BEFORE INSERT ON CINEMA_HALLS
FOR EACH ROW
BEGIN
DECLARE cnt DOUBLE
MySQL said: Documentation
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 5
Declare must be placed after the Begin.
Please check the following link