为什么我的存储函数给了我一个"在上下文中无效" SQLSTATE = 42887?

时间:2017-03-16 13:34:40

标签: sql stored-procedures db2 stored-functions

enter image description here我制作了一个自定义存储函数,它返回指定半径内的终端(变量iTerminal)的所有邮政编码(使用变量iBucket 1-5)。我希望它返回1列表/列表,但它一直给我SQLSTATE = 42887。 SQL查询作为一个独立的部分运行良好,但显然我在存储函数中做错了。

  CREATE OR REPLACE FUNCTION magnum_service_bucket(iTerminal VARCHAR(10),iBucket_zone INTEGER)
  RETURNS TABLE(zone_id VARCHAR(10))

  LANGUAGE SQL
  SPECIFIC fn_magnum_service_bucket
  READS SQL DATA
  NO EXTERNAL ACTION
  NOT DETERMINISTIC
  BEGIN ATOMIC
  RETURN
  select zone_id
  from( 
     Select 
        zone_id, 
        distance, 
        case when distance between 0 and 20 then 1 
           when distance between 21 and 50 then 2 
           when distance between 51 and 100 then 3 
           when distance between 101 and 150 then 4 
           else 5 
        end as bucket  
     From(
        select 
           zone_id,
           city,
           province,
           terminal_zone, 
           DIST_BETWEEN_ZONES((select postal_code from zone where zone_id = iTerminal ),zone_id) as distance 
        from zone 
        where service_type = 'DIRECT' 
        and zone_id in (select zone_id from terminal_multi where terminal_zone = iTerminal)
     )
  ) where bucket = iBucket_zone;
  END

0 个答案:

没有答案