views:

38

answers:

2

I have never done one before and am trying to work through an online guide but I can't seem to find a good example of one with parameters.

CREATE PROCEDURE `calcdistance` (
    IN ulat varchar, 
    IN ulon varchar, 
    IN clat varchar, 
    IN clon varchar)
BEGIN
Select DEGREES(ACOS(SIN(RADIANS(ulat))
    * SIN(RADIANS(clat))
    + COS(RADIANS(ulat))
    * COS(RADIANS(clat))
    * COS(RADIANS(ulon - ulon))))) * 69.09 AS distance
END

the error MySQL admin gives me when I try this is:

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 '  IN ulat varchar, 
    IN ulon varchar, 
    IN clat varchar, 
    IN cl' at line 2 (error 1064)
A: 

In your query, varchar should be varchar(255). And I think that a stored procedure should end in ; by default, unless you use DELIMITER.

Wouter de Bie
+2  A: 

you can try

DELIMITER $$
DROP PROCEDURE IF EXISTS calcdistance$$
CREATE PROCEDURE `calcdistance` (IN ulat VARCHAR(50), IN ulon VARCHAR(50), IN clat VARCHAR(50), IN clon VARCHAR(50))
    BEGIN
    SELECT 
    DEGREES(ACOS(SIN(RADIANS(ulat)) 
    * SIN(RADIANS(clat))
    + COS(RADIANS(ulat))
    * COS(RADIANS(clat))
    * COS(RADIANS(ulon - ulon))))
    * 69.09 AS distance;


     END$$

DELIMITER ;
Sandy
that is still giving me errors. 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 $$DROP PROCEDURE IF EXISTS calcdistance$$CREATE PROCEDURE `calcdista' at line 1 (error 1064)
Kyle
I am assuming you are using PhpMyAdmin, and hence giving error, try using command line or sqlYog. Its working fine for me in sqlYog
Sandy
I am using MySQL GUI Administrator, i can try phpmyadmin though.
Kyle
just tested on PhpMyAdmin, working fine for me. Which version of MySql are you using ?
Sandy
yeah, i get this error in phpmyadmin:
Kyle
#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 'DELIMITER' at line 1
Kyle
running mysql 5.1
Kyle
@Kyle: The stored procedure successfully created for me on MySQL 5.1.49-community, via MySQL Workbench. How are you trying to deploy the stored procedure?
OMG Ponies
I am using phpmyadmin. I have never done a stored procedure before so maybe i am inputting it in the wrong place. Within phpmyadmin i click my database then go to the 'sql' tab and run the stored procedure there. I guess now that i think about it that doesnt insert it or anything. Where should i go or what do you recommend i do?
Kyle