0

Is this the correct syntax for a mysql 5.x stored procedure?

DELIMITER $$

CREATE PROCEDURE GetNearbyPhotogsByMapCenter(
  lat1 decimal (7,3),
  long1 decimal (7,3),
  range  numeric (15)
)
BEGIN
DECLARE  rangeFactor  decimal (7,6);
 SET  rangeFactor = 0.014457;
 select * from (
  SELECT B.cb_plug_lat, B.cb_plug_lng, B.cb_photostudio , B.city, B.State,
  B.country, B.website, B.cb_basesserved, B.phone,
  B.cb_isrc,B.cb_isavailableforsessions
   FROM  jos_comprofiler AS  B, jos_users as JU
  WHERE
  B.cb_plug_lat  BETWEEN  lat1-(range*rangeFactor)  AND
   lat1+(range*rangeFactor)
  AND  B.cb_plug_lng  BETWEEN  long1-(range*rangeFactor)  AND
   long1+(range*rangeFactor)
  AND  GetDistance(lat1,long1,B.cb_plug_lat,B.cb_plug_lng)  <= range
  AND B.approved = 1
  AND B.confirmed = 1
  AND B.user_id = JU.id
  ORDER BY B.cb_isrc desc) as D
  WHERE D.cb_isavailableforsessions = 'Yes' or  D.cb_isavailableforsessions is null;
END
$$

I am trying to load this stored procedure into my MySQL database using the SQL tab on my phpMyAdmin. I did set the delimeter in the bottom of that tab to $$ as per this post: How do I write an SP in phpMyAdmin (MySQL)?

So I am just curious why I keep getting this error:

#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 'lat1 decimal (7,3), long1 decimal (7,3), range numeric (15) BEGIN ' at line 2
Francisco
  • 10,918
  • 6
  • 34
  • 45
Codejoy
  • 3,722
  • 13
  • 59
  • 99

1 Answers1

0

Don't put procedure identifier into quotes. Also range is a MySQL reserved word (since 5.1), so you either need to put it in backticks (`) everytime you use it, or change it to a non-reserved word.

Below is the version that did register a procedure on my MySQL 5.2 server

CREATE PROCEDURE GetNearbyPhotogsByMapCenter(
  lat1 decimal (7,3),
  long1 decimal (7,3),
  `range`  numeric (15)
)
BEGIN
DECLARE  rangeFactor  decimal (7,6);
 SET  rangeFactor = 0.014457;
 select * from (
  SELECT B.cb_plug_lat, B.cb_plug_lng, B.cb_photostudio , B.city, B.State,
  B.country, B.website, B.cb_basesserved, B.phone,
  B.cb_isrc,B.cb_isavailableforsessions
   FROM  jos_comprofiler AS  B, jos_users as JU
  WHERE
  B.cb_plug_lat  BETWEEN  lat1-(`range`*rangeFactor)  AND
   lat1+(`range`*rangeFactor)
  AND  B.cb_plug_lng  BETWEEN  long1-(`range`*rangeFactor)  AND
   long1+(`range`*rangeFactor)
  AND  GetDistance(lat1,long1,B.cb_plug_lat,B.cb_plug_lng)  <= `range`
  AND B.approved = 1
  AND B.confirmed = 1
  AND B.user_id = JU.id
  ORDER BY B.cb_isrc desc) as D
  WHERE D.cb_isavailableforsessions = 'Yes' or  D.cb_isavailableforsessions is null;
END
$$
Mchl
  • 61,444
  • 9
  • 118
  • 120
  • actually should update the post...did that and still same error. – Codejoy Jan 20 '11 at 22:45
  • Now you probably get `...near 'range numeric(15)` error. RANGE is a MySQL reserved word. – Mchl Jan 20 '11 at 22:51
  • ahhhh sweet... how was this working before? I am guessing this is just a variable name and I can use anything for range? – Codejoy Jan 20 '11 at 22:57
  • 1
    `RANGE` was added to reserved words list in MySQL 5.1 (it is used in table partitioning definitions) http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html – Mchl Jan 20 '11 at 23:00
  • Okay, I swapped the procedures to load without the range in there. It does load them now..though when I test with this CALL GetNearbyPhotogsByMapCenter( 77.344 , 30.999 , 500 ); them i get an error: #1312 - PROCEDURE oplove_forum.GetNearbyPhotogsByMapCenter can't return a result set in the given context – Codejoy Jan 20 '11 at 23:13
  • That's quite another thing and not syntax related. I think it deserves a question of its own. – Mchl Jan 20 '11 at 23:39