title: How do I use a MySQL Stored Procedure with Variables?
body: <p>I found a stored procedure that calculates whether a point is in a polygon or outside of it. But, I'm having a little trouble adapting it to my needs.</p>
  
  <p>The stored procedure is called as follows:</p>
  
  <pre><code>SET @point = PointFromText('POINT(5 5)') ;
  SET @polygon = PolyFromText('POLYGON((0 0, 0 10, 10 10, 10 0, 0 0))');
  SELECT myWithin(@point, @polygon) AS result; 
  </code></pre>
  
  <p>I'd like to pull the the latitude and longitude from myPointsTable and set the @point to the result. Something like (pseudo-code):</p>
  
  <pre><code>    SET @point = PointFromText('POINT( SELECT latitude, longitude
      FROM `myPointsTable` 
      WHERE  color = 'red')') ;
  </code></pre>
  
  <p>And then have the stored procedure iterate through those points.</p>
  
  <p>Likewise, I'd like to pull the polygon from myPolygonTable. Something like (pseudo-code):</p>
  
  <pre><code>SET @polygon = PolyFromText('POLYGON((SELECT polygonPoints FROM 'myPolygonTable' WHERE town = 'Newport'))');
  </code></pre>
  
  <p>The stored procedure is: </p>
  
  <pre><code>DROP FUNCTION IF EXISTS myWithin;
  DELIMITER $$
  CREATE FUNCTION myWithin(p POINT, poly POLYGON) RETURNS INT(1) DETERMINISTIC
  BEGIN
  DECLARE n INT DEFAULT 0;
  DECLARE pX DECIMAL(9,6);
  DECLARE pY DECIMAL(9,6);
  DECLARE ls LINESTRING;
  DECLARE poly1 POINT;
  DECLARE poly1X DECIMAL(9,6);
  DECLARE poly1Y DECIMAL(9,6);
  DECLARE poly2 POINT;
  DECLARE poly2X DECIMAL(9,6);
  DECLARE poly2Y DECIMAL(9,6);
  DECLARE i INT DEFAULT 0;
  DECLARE result INT(1) DEFAULT 0;
  SET pX = X(p);
  SET pY = Y(p);
  SET ls = ExteriorRing(poly);
  SET poly2 = EndPoint(ls);
  SET poly2X = X(poly2);
  SET poly2Y = Y(poly2);
  SET n = NumPoints(ls);
  WHILE i&lt;n DO
  SET poly1 = PointN(ls, (i+1));
  SET poly1X = X(poly1);
  SET poly1Y = Y(poly1);
  IF ( ( ( ( poly1X &lt;= pX ) 
  &amp;&amp; ( pX &lt; poly2X ) ) || ( ( poly2X &lt;= pX ) 
  &amp;&amp; ( pX &lt; poly1X ) ) ) 
  &amp;&amp; ( pY &gt; ( poly2Y - poly1Y ) * ( pX - poly1X ) / ( poly2X - poly1X ) + poly1Y ) ) THEN
  SET result = !result;
  END IF;
  SET poly2X = poly1X;
  SET poly2Y = poly1Y;
  SET i = i + 1;
  END WHILE;
  RETURN result;
  End
  $$
  DELIMITER ;
  </code></pre>
  
  <p>How do I use a SELECT statement in setting the variables? Is it possible? Is there a better way?</p>
  
  <p>Thank you.</p>
  
  <p>-Laxmidi</p>
  
  <p>UPDATE:</p>
  
  <p>Hi,</p>
  
  <p>I've got part of the problem solved. I've figured out how to grab the latitude and longitudes.</p>
  
  <pre><code>DROP PROCEDURE IF EXISTS latlongGrabber;
  DELIMITER $$
  CREATE PROCEDURE latlongGrabber(IN offense_in VARCHAR(255))
  
  BEGIN
       DECLARE latitude_val VARCHAR(255);
       DECLARE longitude_val VARCHAR(255);
       DECLARE no_more_rows BOOLEAN;
       DECLARE latlongGrabber_cur CURSOR FOR
  
       SELECT latitude, longitude FROM myTable WHERE offense = offense_in;
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET no_more_rows = TRUE;
  
  OPEN latlongGrabber_cur;
  
  
  the_loop: LOOP
  
  FETCH latlongGrabber_cur
  INTO latitude_val, longitude_val;
  
  IF no_more_rows THEN CLOSE latlongGrabber_cur;
  LEAVE the_loop;
  END IF;
  SELECT latitude_val, longitude_val;
  END LOOP the_loop;
  END
  $$
  DELIMITER ;
  </code></pre>
  
  <p>Now, I have to figure out how to combine the two functions. </p>
  
  <p>-Laxmidi</p>