Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql syntax error in creating function

Tags:

mysql

DROP FUNCTION IF EXISTS bramkiStracone;

CREATE FUNCTION bramkiStracone(idDruzyny INT)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE bramek INT;
DECLARE tmp1 INT;
DECLARE tmp2 INT;

SELECT DISTINCT COALESCE(SUM( b.bramki ), 0) INTO tmp1
FROM LigaBramki b
INNER JOIN LigaZawodnicy z ON b.idZawodnika = z.idZawodnika
INNER JOIN LigaDruzyny d ON z.idDruzyny = d.idDruzyny
INNER JOIN LigaMecze m ON b.idMeczu = m.idMeczu
WHERE m.idDruzyny2 = idDruzyny
AND z.idDruzyny != idDruzyny
AND d.idDruzyny != idDruzyny
AND m.rozegrany = '1';

SELECT DISTINCT COALESCE(SUM( b.bramki ), 0) INTO tmp2
FROM LigaBramki b
INNER JOIN LigaZawodnicy z ON b.idZawodnika = z.idZawodnika
INNER JOIN LigaDruzyny d ON z.idDruzyny = d.idDruzyny
INNER JOIN LigaMecze m ON b.idMeczu = m.idMeczu
WHERE m.idDruzyny1 = idDruzyny
AND z.idDruzyny != idDruzyny
AND d.idDruzyny != idDruzyny
AND m.rozegrany = '1';

SET bramek = tmp1 + tmp2;

RETURN bramek;
END

I have error near DECLARE bramek INT; but don't know what am i missing,
i tried to make it work by removing semi-colons but that didn't work either.

after i added delimiter i still have 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 'DELIMITER |
CREATE FUNCTION bramkiStracone(idDruzyny INT)
RETURNS int(11)
DET' at line 1 
like image 1000
Viszman Avatar asked Oct 09 '12 09:10

Viszman


People also ask

What is syntax error in MySQL?

The MySQL 1064 error is a syntax error. This means the reason there's a problem is because MySQL doesn't understand what you're asking it to do. However, there are many different situations that can lead to this type of miscommunication between you and your database.

How to CREATE functions in MySQL with example?

The syntax to create a function in MySQL is: CREATE FUNCTION function_name [ (parameter datatype [, parameter datatype]) ] RETURNS return_datatype BEGIN declaration_section executable_section END; function_name. The name to assign to this function in MySQL.

Can we CREATE FUNCTION in MySQL?

The CREATE FUNCTION statement is also used in MySQL to support loadable functions. See Section 13.7. 4.1, “CREATE FUNCTION Statement for Loadable Functions”. A loadable function can be regarded as an external stored function.


1 Answers

dont forget to change the default DELIMITER so the query won't terminate at DECLARE bramek INT;

DROP FUNCTION IF EXISTS bramkiStracone;

DELIMITER $$

CREATE FUNCTION bramkiStracone(idDruzyny INT)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE bramek INT;
DECLARE tmp1 INT;
DECLARE tmp2 INT;

SELECT DISTINCT COALESCE(SUM( b.bramki ), 0) INTO tmp1
FROM LigaBramki b
INNER JOIN LigaZawodnicy z ON b.idZawodnika = z.idZawodnika
INNER JOIN LigaDruzyny d ON z.idDruzyny = d.idDruzyny
INNER JOIN LigaMecze m ON b.idMeczu = m.idMeczu
WHERE m.idDruzyny2 = idDruzyny
AND z.idDruzyny != idDruzyny
AND d.idDruzyny != idDruzyny
AND m.rozegrany = '1';

SELECT DISTINCT COALESCE(SUM( b.bramki ), 0) INTO tmp2
FROM LigaBramki b
INNER JOIN LigaZawodnicy z ON b.idZawodnika = z.idZawodnika
INNER JOIN LigaDruzyny d ON z.idDruzyny = d.idDruzyny
INNER JOIN LigaMecze m ON b.idMeczu = m.idMeczu
WHERE m.idDruzyny1 = idDruzyny
AND z.idDruzyny != idDruzyny
AND d.idDruzyny != idDruzyny
AND m.rozegrany = '1';

SET bramek = tmp1 + tmp2;

RETURN bramek;
END $$

DELIMITER ;
like image 125
John Woo Avatar answered Oct 13 '22 23:10

John Woo