MySQL CASE not working in Stored Procedure -


this works fine. if change @unit_type variable. correct unit type comes out in calculation.

set @unit_type = 'kilometers'; set @lat = 50; set @lon = 100; set @lat2 = 51; set @lon2 = 101;  select  (case @unit_type      when 'feet' 20000000     when 'miles' 3696.1     when 'meters' 6979100     when 'kilometers' 6979     end * acos( cos( radians(@lat) )      * cos( radians( @lat2 ) )      * cos( radians( @lon2 )      - radians(@lon) )      + sin( radians(@lat) )      * sin( radians( @lat2 ) )  ) ) distance; 

this however, not. when try run stored procedure, return in kilometers every time. , if delete case kilometers, result null.

create procedure `anothertest`(in `lat` bigint, in `lon` bigint, in `lat2` bigint, in `lon2` bigint, in `unit_type` text)         language sql         not deterministic         contains sql         sql security definer         comment ''     begin      select      (case @unit_type          when 'feet' 20000000         when 'miles' 3696.1         when 'meters' 6979100         when 'kilometers' 6979         end     * acos( cos( radians(@lat) )          * cos( radians( @lat2 ) )          * cos( radians( @lon2 )          - radians(@lon) )          + sin( radians(@lat) )          * sin( radians( @lat2 ) )      ) ) distance;     end 

i want understand why is, , how better written around issue.

anyone have ideas? in advance.

updated:

the solution remove @'s within select statement of procedure. not required access parameters.

create procedure `anothertest`(in `lat` bigint, in `lon` bigint, in `lat2` bigint, in `lon2` bigint, in `unit_type` text)         language sql         not deterministic         contains sql         sql security definer         comment ''     begin      select      (case unit_type          when 'feet' 20000000         when 'miles' 3696.1         when 'meters' 6979100         when 'kilometers' 6979         end     * acos( cos( radians(lat) )          * cos( radians( lat2 ) )          * cos( radians( lon2 )          - radians(lon) )          + sin( radians(lat) )          * sin( radians( lat2 ) )      ) ) distance;     end 

@unit_type user variable. don't see anywhere being set, in procedure. (it may set in session preceding call; have whatever value last assigned in session.)

we see parameter passed in procedure named unit_type. don't see reference in procedure.

one quick fix set @unit_type user variable, prior select, value parameter:

set @unit_type = unit_type; 

the same issue exists parameters well, there's no reference them either.


the other option fix reference procedure arguments in sql, replacing references user_variables. (looks quick edit there remove @ characters.)


there's significant difference between user-defined variables , procedure variables.

user-defined variables http://dev.mysql.com/doc/refman/5.5/en/user-variables.html


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -