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
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.
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.
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.
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 ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With