What's wrong with this line when doing a declaration - mysql

I'm using HeidiSQL to connect to MySQL 5.7.14
I've researched how to declare a variable, but it's giving me this error. Even when on its on.
Does anyone know what is wrong with this?
Thanks
DECLARE total_sale INT;
Screen Shot Entire SQL

DECLARE is used in programs. As the documentation states:
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
You can simply set the value:
SET total_sale = 0;

Related

MySQL Stored Procedure - if then not functioning

I'm converting SQL Server stored procedures to MySQL and running into issues. I have a stored procedure with an IF THEN ELSE that, while not giving errors, is not returning any data either and I'm not seeing the problem to fix it. The queries by themselves are correct and return data but don't seem to work in the stored procedure. This is a simplified version of the real query just FYI.
The SQL for creating the stored procedure is:
DROP PROCEDURE IF EXISTS `sp_GetVolunteerAwardsList`;
DELIMITER //
CREATE PROCEDURE `sp_GetVolunteerList`( IN glAward_in int)
BEGIN
DECLARE glAward_In INT;
DECLARE awardType_In varchar(100);
DECLARE awardActive INT;
IF (glAward_In) = 0 THEN
SELECT * FROM tbl_volunteer
ELSEIF (glAward_In) = 1 THEN
SELECT * FROM tbl_volunteerpositions
END IF;
END
//
As always, any assistance would be most appreciated.
Check the glAward_In parameter or variable.
The SP is receiving the parameter glAward_in, i in lower case.
Then there is a DECLARE that declares a different variable glAward_In, i in upper case.
The if is done using the glAward_In in uppercase which is not set in any place of the SP. And the parameter in lower case is not used anywhere in the SP.
I think you have to remove the DECLARATION of the variable in upper case, and use the parameter in lowercase for the IF evaluation.

YA MySQL Error #1064 in CALL

I've created the following stored procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `ABON_PLATA`(DATE_BAL1 datetime)
BEGIN
DECLARE IMSI1 varchar(6);
DECLARE DATE1 datetime;
DECLARE ID_U1 integer;
DECLARE PAY1 double;
DECLARE PAYSUM double;
DECLARE OLD1 double;
DECLARE REASON1 varchar(16);
DECLARE FLAG integer DEFAULT 0;
DECLARE C1 CURSOR FOR SELECT ID_U FROM podkl_otkl_uslug WHERE IMSI1=IMSI;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET FLAG=1;
OPEN C1;
REPEAT
FETCH C1 INTO ID_U1;
IF FLAG=0 THEN
SELECT SUM(PAY) INTO PAY1 FROM uslugi WHERE ID_U1=ID_U AND DATE_PODKL<DATE_BAL1 AND DATE_OTKL IS NULL;
SET REASON1 = 'ABON PLATA';
SET DATE1 = DATE_BAL1;
SET PAYSUM = `NEW`-PAY1;
SET OLD1 = `NEW`;
END IF;
INSERT history (`DATE`, REASON, `NEW`, OLD, IMSI) VALUES (DATE1, REASON1, PAYSUM, OLD1, IMSI1);
UNTIL FLAG=1
END REPEAT;
CLOSE C1;
END
It is used to simulate subscribtion fee of mobile operator. As I am trying to call it, I get the notorious error:
CALL ABON_PLATA(2013-07-07 12:00:00);
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 '12:00:00)' at line 1
Considering it is an error at line 1, I get extremely confused. What super obvious am I missing?
Datetime literals must be in quotes.
CALL ABON_PLATA('2013-07-07 12:00:00');
The reason it reports an error at line 1 is that it's the CALL line that contains an error. It has nothing to do with your procedure code, because the error happens before it can even call your procedure.
Re your comment:
Thread stack overrun: 6892 bytes used of a 131072 byte stack, and 128000 bytes needed. Use 'mysqld --thread_stack=#' to specify a bigger stack.
This indicates that your MySQL has a configured value of thread_stack that is too small to run statements in Stored Procedures. This can happen, for example, if you use the my-small.cnf configuration file that is bundled with some releases of MySQL.
Increase the value of thread_stack in your configuration file. The default is 256K, and it's only the my-small.cnf that sets it lower. There has been a bug logged about this setting being too low.
Don't forget to restart your MySQL service after you make this change to the config file.

Syntax translate

this is my code :
CREATE TRIGGER Zmiana_kategorii
ON Hotele
AFTER UPDATE
AS
BEGIN
DECLARE #stara smallint, #nowa smallint
IF COL_LENGTH('deleted', 'IloscGwiazdek')
BEGIN
SET #stara=(SELECT IloscGwiazdek FROM deleted)
SET #nowa=(SELECT IloscGwiazdek FROM inserted)
IF(#stara<#nowa)
BEGIN
print 'Powiadom następujących klientów o zmianie klasy hotelu'
declare #data date
SET #data=(CONVERT (date, GETDATE()))
SELECT KlientID FROM Rezerwacje Where #data<DataPrzyjazdu
END
END
END
could someone tell me what is wrong in syntax? I am 1st time using MYSQL i have no clue whats is wrong with this...
this is error : ON Hotele AFTER UPDATE AS BEGIN DECLARE #stara smallint, #nowa smallint ' at line 2" this is error
At least one problem is that you don't include the THEN after your IF conditions. For example:
IF (#stara<#nowa) THEN
BEGIN
-- ...
END
Another problem is that you have the order of the CREATE TRIGGER elements wrong. It should be:
CREATE TRIGGER Zmiana_kategorii
AFTER UPDATE
ON Hotele
Note that the AFTER UPDATE goes before the ON. This seems to solve your specific problem.
Finally, MySQL doesn't have a PRINT command and all variables have to be declared at the beginning of a BEGIN block, before any other statements.
Please read this entire page and understand what's going on: http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
If you understand the syntax, then you can debug it yourself. These types of simple problems shouldn't require community assistance (though sometimes an extra pair of eyes can catch an obvious error).

EXCEPTION WHEN OTHERS equivalent in MySQL

I am trying to port an Oracle trigger to MySQL. There's an EXCEPTION WHEN OTHERS statement in the trigger and while I have found equivalent statements for everything else, I cannot find one for this. The trigger is something like:
IF (yada yada)
THEN
BEGIN
SELECT a
INTO generic_variable1
FROM table
WHERE condition;
SET generic_variable2 = value1;
EXCEPTION WHEN OTHERS --this part needs to be replaced by valid MySQL syntax
SET generic_variable2 = value2;
END;
END IF;
How do I convert that part into MySQL?
You should understand that MySQL has a very limited stored proc / trigger language compared to Oracle. While porting to MySQL, many Oracle users say over and over again, "I can't believe MySQL can't do X!!!!"
The closest thing to EXCEPTION WHEN OTHERS may be DECLARE CONTINUE HANDLER.
Example (not tested):
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET #generic_variable2 = 1;
But you'd declare that before the block of code that might throw the exception, not after.
See http://dev.mysql.com/doc/refman/5.5/en/declare-handler.html for full docs.

Batch Insert SQL query in MySQL

I just keep getting the error 1064. I searched how to do while loops then declare local variables etc. and I don't see what I'm doing wrong. I tried to do it without the ";" and I tired setting the delimiter as "|" to be able to use ";" as a separator between lines (I read something somwhere that kind of said it could be the way to do it?..)
I'm trying to do that query on PhpMyAdmin and my MySql version is 5.1.36
I'm not going to explain what I'm trying as I believe it is easy to understand by simply reading my query below.
BEGIN
DECLARE v1 INT DEFAULT 0;
DECLARE v2 VARCHAR(10);
WHILE v1 < 20 DO
SET v2 = CONCAT('Test ', CAST(v1 AS CHAR(2)));
INSERT INTO news(title,date, message) VALUES(v2, NOW(), v2);
SET v1 = v1 + 1;
END WHILE;
END;
MySql only allows compound statements using the BEGIN...END tag inside stored programs.
From the Docs:
BEGIN ... END syntax is used for
writing compound statements, which can
appear within stored programs
The first thing i can see is a small syntax error within the CAST parameters.
You have:
CAST(v1 ASCHAR(2))
You need:
CAST(v1 AS CHAR(2))
See http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html#function_cast
Change
DECLAREv2 VARCHAR(10);
to
DECLARE v2 VARCHAR(10);