2014-01-16 19 views
6

我有一個遞歸查詢,在函數內包封,它返回一個分層調查「結構」:定影誤差「功能array_length(BIGINT [])不存在」

CREATE OR REPLACE FUNCTION get_survey_results(IN v_survey_id integer DEFAULT 1, IN v_survey_session_id integer DEFAULT NULL) 
    RETURNS TABLE(sort_order bigint[], survey_question_id integer, question_text text, required_ind smallint, survey_session_id integer, custom_text text, option_value integer, survey_scale_id integer, survey_scale_type_id integer, parent_survey_question_id integer, parent_survey_scale_option_id integer) AS 
$BODY$ 
BEGIN 
    RETURN QUERY 
     WITH RECURSIVE Survey_Structure (PATH, survey_question_id, question_text, 
      required_ind, survey_session_id, custom_text, 
      option_value, survey_scale_id, survey_scale_type_id, 
      parent_survey_question_id, parent_survey_scale_option_id, CYCLE) 
     AS (
      SELECT ARRAY[row_number() OVER (ORDER BY Survey_Question.sort_order)], Survey_Question.survey_question_id, Survey_Question.question_text, 
       Survey_Question.required_ind, Survey_Response.survey_session_id, Survey_Response.custom_text, 
       Survey_Scale_Option.option_value, Survey_Question.survey_scale_id, Survey_Scale.survey_scale_type_id, 
       0 as parent_survey_question_id, 0 AS parent_survey_scale_option_id, FALSE 
      FROM ...etc... 

我可以傳遞一個survey_id到此功能(SELECT * FROM get_survey_results(1)),並返回該調查的所有問題和答案。

我想通過確定層次結構中的一個節點的 「級別」:

SELECT question_text, array_length(sort_order) AS level, 
    ...etc... 
FROM get_survey_results(1) 

我的查詢返回的錯誤

功能array_length(BIGINT [])不存在

PostgreSQL array documentation承諾arrar_length()將處理「anyarray」。

我在做什麼錯?是否有我需要安裝的可選軟件包?

回答

17

仔細檢查文件顯示array_length()需要兩個參數。

我不得不改變

SELECT question_text, array_length(sort_order) AS level, ...

SELECT question_text, array_length(sort_order, 1) AS level, ...

+2

有此刻加入郵件列表上的簡寫版的討論。隨意稱重。 –