Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.
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 
share|improve this question
    
The syntax is correct. Try to add DELIMITER commands. –  Devart Oct 9 '12 at 9:25

3 Answers 3

up vote 1 down vote accepted

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 ;
share|improve this answer
1  
now i used delimeter but still got 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 –  Viszman Oct 9 '12 at 10:15

You need to change your client's statement delimiter, so that it doesn't think each ; terminates the entire CREATE FUNCTION statement.

In the MySQL command line client, one can use the delimiter command:

delimiter ;;

CREATE FUNCTION ...  ;;

delimiter ;

Read the manual on Defining Stored Programs.

share|improve this answer

Make sure you set the delimiter to something other than semicolon before you declare your function:

DELIMITER |
CREATE FUNCTION bramkiStracone(idDruzyny INT)
RETURNS int(11)
DETERMINISTIC
BEGIN
DECLARE bramek INT;
...
END|
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.