【问题标题】:Correct Syntax for Mysql 5 stored procedure?Mysql 5 存储过程的正确语法?
【发布时间】:2011-06-12 19:31:04
【问题描述】:

这是 mysql 5.x 存储过程的正确语法吗?

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
$$

我正在尝试使用 phpMyAdmin 上的 SQL 选项卡将此存储过程加载到我的 MySQL 数据库中。根据这篇文章,我确实将该选项卡底部的分隔符设置为 $$: How do I write an SP in phpMyAdmin (MySQL)?

所以我只是好奇为什么我不断收到这个错误:

#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

【问题讨论】:

    标签: mysql phpmyadmin


    【解决方案1】:

    不要将过程标识符放在引号中。 range 也是 MySQL 的保留字(从 5.1 开始),所以每次使用时都需要将其放在反引号 (`) 中,或者将其更改为非保留字。

    以下是在我的 MySQL 5.2 服务器上注册过程的版本

    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
    $$
    

    【讨论】:

    • 实际上应该更新帖子...这样做了,仍然是同样的错误。
    • 现在您可能会收到...near 'range numeric(15) 错误。 RANGE 是 MySQL 的保留字。
    • ahhhh 甜...以前是如何工作的?我猜这只是一个变量名,我可以使用任何范围?
    • RANGE 被添加到 MySQL 5.1 的保留字列表中(用于表分区定义)dev.mysql.com/doc/refman/5.1/en/reserved-words.html
    • 好的,我交换了程序以加载没有范围。它现在确实加载了它们..虽然当我用这个 CALL GetNearbyPhotogsByMapCenter( 77.344 , 30.999 , 500 ); 进行测试时他们我得到一个错误:#1312 - PROCEDURE oplove_forum.GetNearbyPhotogsByMapCenter 无法返回给定上下文中的结果集
    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 2015-12-28
    • 2012-03-11
    • 1970-01-01
    • 2020-06-23
    • 2016-10-28
    • 2020-03-08
    • 2012-04-12
    相关资源
    最近更新 更多