A
A
Alexiuscrow2015-04-04 21:11:15
MySQL
Alexiuscrow, 2015-04-04 21:11:15

Create a stored procedure. What could be the problem?

There is a simple request. You need to create a stored procedure from this query.
Request:

SELECT
  id, name, category, ROUND((
    1000 * 6371 * ACOS (
      COS ( RADIANS(51.42689) )
      * COS( RADIANS( latitude ) )
      * COS( RADIANS( longitude ) - RADIANS(31.42689) )
      + SIN ( RADIANS(51.522256) )
      * SIN( RADIANS( latitude ) )
    )
  ), 3) AS distance, address, latitude, longitude, 
  (SELECT name FROM localities WHERE id = locality_id) AS locality_name, locality_id
FROM shops
HAVING distance < 150
ORDER BY distance;

Creating a stored procedure from the above query:
DROP PROCEDURE IF EXISTS `get_nearest_shops`;
DELIMITER //
CREATE PROCEDURE get_nearest_shops(
  IN p_lat DOUBLE, 
  IN p_lng DOUBLE, 
    IN p_radius DOUBLE,
    OUT p_id INT(10),
    OUT p_name VARCHAR(45),
    OUT p_category ENUM('undefined','auto','children_prod','food','game','book',
                        'electronics','beuty&health','fashin','footwear','clothing',
                        'sports','homewere','pet_prod','services','gift&flowers'),
    OUT p_distance DOUBLE, 
    OUT p_latitude DOUBLE,
    OUT p_longitude DOUBLE,
    OUT p_locality VARCHAR(45),
    OUT p_locality_id INT(10))
BEGIN 
  DECLARE sign_after_point INT(10) DEFAULT 3;
  SELECT
    id, name, category, ROUND((
    1000 * 6371 * ACOS (
      COS ( RADIANS(p_lat) )
      * COS( RADIANS( latitude ) )
      * COS( RADIANS( longitude ) - RADIANS(p_lng) )
      + SIN ( RADIANS(p_lat) )
      * SIN( RADIANS( latitude ) )
      )
    ), sign_after_point) AS distance, latitude, longitude, 
        (SELECT name FROM localities WHERE id = locality_id) AS locality, 
        locality_id
  INTO
    p_id, p_name, p_category, p_distance, p_latitude, p_longitude, 
        p_locality, p_locality_id
    FROM shops
  HAVING distance < p_radius
  ORDER BY distance;
END;//

As a result, the stored procedure is created without a single error, but, unlike a simple query, it does not display information, despite the fact that the incoming data is the same.
What could be wrong?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artyom Karetnikov, 2015-04-06
@Alexiuscrow

I'm shy to ask, but if you need the query to return several lines - why are you shoving its result into out parameters? No need to do this, just leave select without any parameters and it will return what you need.

V
Vapaamies, 2015-04-04
@vapaamies

Isn't more than one row returned in a query? In another DBMS from Oracle, this leads to a clearly visible error, where it says: "The query returned more than one row." In into , this is not possible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question