创建一个将返回表的SQL函数

时间:2016-05-16 05:51:55

标签: sql postgresql

我试图创建一个返回表的Sql函数,但是当我尝试编译脚本时会抛出异常。我试图修复它超过一个小时。

错误例外:

ERROR:  syntax error at or near "$1"
LINE 1: ...name, oj.job_summary, oj.last_modified, d.num AS  $1 , oj.de...
                                                             ^

SQL函数查询:

CREATE FUNCTION public.export_job_description (
  organization_id BIGINT,
  status VARCHAR,
  active_p BOOLEAN,
  out org_job_code BIGINT,
  out org_job_title VARCHAR,
  out job_family_name VARCHAR,
  out job_summary VARCHAR,
  out last_modified DATE,
  out department_number INTEGER,
  out department_name VARCHAR,
  out org_salary_grade VARCHAR,
  out patient_care_p BOOLEAN,
  out equal_weights VARCHAR,
  out eval_required VARCHAR,
  out intro_eval_required_p BOOLEAN,
  out "ExperienceDescription" VARCHAR,
  out "ExperienceMinimum" VARCHAR,
  out "Experiencepreferred" VARCHAR,
  out "EducationDescription" VARCHAR,
  out "EducationMinimum" VARCHAR,
  out "Educationpreferred" VARCHAR,
  out "TrainingDescription" VARCHAR,
  out "TrainingMinimum" VARCHAR,
  out "Trainingpreferred" VARCHAR,
  out "SpecialSkillsDescription" VARCHAR,
  out "SpecialSkillsMinimum" VARCHAR,
  out "SpecialSkillspreferred" VARCHAR,
  out "LicensureDescription" VARCHAR,
  out "LicensureMinimum" VARCHAR,
  out licensureprepared VARCHAR,
  out supervisory VARCHAR,
  out label VARCHAR,
  out par_sitting INTEGER,
  out par_standing INTEGER,
  out par_walking INTEGER,
  out par_climbing INTEGER,
  out par_lifting_low INTEGER,
  out par_lifting_high INTEGER,
  out par_carrying_objects INTEGER,
  out par_push_pull INTEGER,
  out par_twisting INTEGER,
  out par_bending INTEGER,
  out par_reaching_forward INTEGER,
  out par_reaching_overhead INTEGER,
  out par_squat_kneel_crawl INTEGER,
  out par_wrist_position_deviation INTEGER,
  out par_pinching_fine_motor_activities INTEGER,
  out par_keyboard_use_repetitive_motion INTEGER,
  out par_taste_or_smell INTEGER,
  out par_talk_or_hear INTEGER,
  out sr_near_vision INTEGER,
  out sr_far_vision INTEGER,
  out sr_color_discrimination INTEGER,
  out sr_depth_perception INTEGER,
  out sr_hearing INTEGER,
  out er_bloodborne_pathogens INTEGER,
  out er_chemical INTEGER,
  out er_airborne_communicable_diseases INTEGER,
  out er_extreme_temperatures INTEGER,
  out er_radiation INTEGER,
  out er_uneven_surfaces_or_elevations INTEGER,
  out er_extreme_noise_levels INTEGER,
  out er_dust_particular_matter INTEGER,
  out er_usual_workday_hours INTEGER,
  out org_job_flsa_exempt_p BOOLEAN
)
RETURNS SETOF record AS
$body$
DECLARE
                p_organization_id alias for $1;
                p_status alias for $2;
                p_active_p alias for $3;
BEGIN
  SELECT
    oj.org_job_code,
    oj.org_job_title,
    oj.job_family_name,
    oj.job_summary,
    oj.last_modified,
    d.num AS department_number,
    oj.department_name,
    oj.org_salary_grade,
    oj.patient_care_p,
    oj.equal_weights,
    oj.eval_required,
    oj.intro_eval_required_p,
    tdes.ExperienceDescription,
    tdes.ExperienceMinimum,
    tdes.Experiencepreferred,
    tdes.EducationDescription,
    tdes.EducationMinimum,
    tdes.Educationpreferred,
    tdes.TrainingDescription,
    tdes.TrainingMinimum,
    tdes.Trainingpreferred,
    tdes.SpecialSkillsDescription,
    tdes.SpecialSkillsMinimum,
    tdes.SpecialSkillspreferred,
    tdes.LicensureDescription,
    lmp.LicensureMinimum,
    lmp.licensureprepared,
    oj.supervisory,
    fd.label,
    ei.par_sitting,
    ei.par_standing,
    ei.par_walking,
    ei.par_climbing,
    ei.par_lifting_low,
    ei.par_lifting_high,
    ei.par_carrying_objects,
    ei.par_push_pull,
    ei.par_twisting,
    ei.par_bending,
    ei.par_reaching_forward,
    ei.par_reaching_overhead,
    ei.par_squat_kneel_crawl,
    ei.par_wrist_position_deviation,
    ei.par_pinching_fine_motor_activities,
    ei.par_keyboard_use_repetitive_motion,
    ei.par_taste_or_smell,
    ei.par_talk_or_hear,
    ei.sr_near_vision,
    ei.sr_far_vision,
    ei.sr_color_discrimination,
    ei.sr_depth_perception,
    ei.sr_hearing,
    ei.er_bloodborne_pathogens,
    ei.er_chemical,
    ei.er_airborne_communicable_diseases,
    ei.er_extreme_temperatures,
    ei.er_radiation,
    ei.er_uneven_surfaces_or_elevations,
    ei.er_extreme_noise_levels,
    ei.er_dust_particular_matter,
    ei.er_usual_workday_hours,
    oj.org_job_flsa_exempt_p
  FROM org_jobs AS oj
  LEFT OUTER JOIN cp_departments AS d
    ON oj.department_id = d.department_id
  LEFT OUTER JOIN cp_lov_job_families AS f
    ON oj.job_family_id = f.lov_job_family_id
  LEFT OUTER JOIN cp_org_func_demands AS fd
    ON oj.cp_org_func_demands_id = fd.cp_org_func_demands_id
  LEFT OUTER JOIN (SELECT
    des.org_job_id,
    MAX(CASE
      WHEN t.description_name = 'Experience' THEN des.description
      ELSE NULL
    END) AS ExperienceDescription,
    MAX(CASE
      WHEN t.description_name = 'Experience' THEN des.minimum
      ELSE NULL
    END) AS ExperienceMinimum,
    MAX(CASE
      WHEN t.description_name = 'Experience' THEN des.preferred
      ELSE NULL
    END) AS Experiencepreferred,
    MAX(CASE
      WHEN t.description_name = 'Education' THEN des.description
      ELSE NULL
    END) AS EducationDescription,
    MAX(CASE
      WHEN t.description_name = 'Education' THEN des.minimum
      ELSE NULL
    END) AS EducationMinimum,
    MAX(CASE
      WHEN t.description_name = 'Education' THEN des.preferred
      ELSE NULL
    END) AS Educationpreferred,
    MAX(CASE
      WHEN t.description_name = 'Training' THEN des.description
      ELSE NULL
    END) AS TrainingDescription,
    MAX(CASE
      WHEN t.description_name = 'Training' THEN des.minimum
      ELSE NULL
    END) AS TrainingMinimum,
    MAX(CASE
      WHEN t.description_name = 'Training' THEN des.preferred
      ELSE NULL
    END) AS Trainingpreferred,
    MAX(CASE
      WHEN t.description_name = 'Special Skills' THEN des.description
      ELSE NULL
    END) AS SpecialSkillsDescription,
    MAX(CASE
      WHEN t.description_name = 'Special Skills' THEN des.minimum
      ELSE NULL
    END) AS SpecialSkillsMinimum,
    MAX(CASE
      WHEN t.description_name = 'Special Skills' THEN des.preferred
      ELSE NULL
    END) AS SpecialSkillspreferred,
    MAX(CASE
      WHEN t.description_name = 'Licensure' THEN des.description
      ELSE NULL
    END) AS LicensureDescription
  FROM cp_description_types AS t
  JOIN cp_descriptions AS des
    ON t.description_type_id = des.description_type_id
  WHERE des.active_p = p_active_p /*Param*/
  AND des.organization_id = p_organization_id /*Param*/
  GROUP BY des.org_job_id) AS tdes
    ON oj.org_job_id = tdes.org_job_id
  LEFT OUTER JOIN (SELECT
    ljm.org_job_id,
    MAX(CASE
      WHEN ljm.licensure_type = 'minimum' THEN lic.licensure_name
      ELSE NULL
    END) AS LicensureMinimum,
    MAX(CASE
      WHEN ljm.licensure_type = 'preferred' THEN lic.licensure_name
      ELSE NULL
    END) AS LicensurePrepared
  FROM licensure_job_map AS ljm
  JOIN cp_lov_licensures AS lic
    ON ljm.lov_licensure_id = lic.lov_licensure_id
  WHERE lic.active_p = p_active_p /*Param*/
  AND lic.organization_id = p_organization_id /*Param*/
  GROUP BY ljm.org_job_id) AS lmp
    ON oj.org_job_id = lmp.org_job_id
  LEFT OUTER JOIN (SELECT
    wi.org_job_id,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Sitting' THEN wi.level_required
      ELSE NULL
    END) AS par_sitting,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Standing' THEN wi.level_required
      ELSE NULL
    END) AS par_standing,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Walking' THEN wi.level_required
      ELSE NULL
    END) AS par_walking,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Climbing' THEN wi.level_required
      ELSE NULL
    END) AS par_climbing,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Lifting (floor to waist level)' THEN wi.level_required
      ELSE NULL
    END) AS par_lifting_low,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Lifting (waist level and above)' THEN wi.level_required
      ELSE NULL
    END) AS par_lifting_high,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Carrying objects' THEN wi.level_required
      ELSE NULL
    END) AS par_carrying_objects,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Push/pull' THEN wi.level_required
      ELSE NULL
    END) AS par_push_pull,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Twisting' THEN wi.level_required
      ELSE NULL
    END) AS par_twisting,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Bending' THEN wi.level_required
      ELSE NULL
    END) AS par_bending,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Reaching forward' THEN wi.level_required
      ELSE NULL
    END) AS par_reaching_forward,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Reaching overhead' THEN wi.level_required
      ELSE NULL
    END) AS par_reaching_overhead,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Squat/kneel/crawl' THEN wi.level_required
      ELSE NULL
    END) AS par_squat_kneel_crawl,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Wrist position deviation' THEN wi.level_required
      ELSE NULL
    END) AS par_wrist_position_deviation,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Pinching/fine motor activities' THEN wi.level_required
      ELSE NULL
    END) AS par_pinching_fine_motor_activities,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Keyboard use/repetitive motion' THEN wi.level_required
      ELSE NULL
    END) AS par_keyboard_use_repetitive_motion,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Taste or smell' THEN wi.level_required
      ELSE NULL
    END) AS par_taste_or_smell,
    MAX(CASE
      WHEN wt.item_name = 'Physical Activity Requirements' AND
        wi.title = 'Talk or hear' THEN wi.level_required
      ELSE NULL
    END) AS par_talk_or_hear,
    MAX(CASE
      WHEN wt.item_name = 'Sensory Requirements' AND
        wi.title = 'Near Vision' THEN wi.level_required
      ELSE NULL
    END) AS sr_near_vision,
    MAX(CASE
      WHEN wt.item_name = 'Sensory Requirements' AND
        wi.title = 'Far Vision' THEN wi.level_required
      ELSE NULL
    END) AS sr_far_vision,
    MAX(CASE
      WHEN wt.item_name = 'Sensory Requirements' AND
        wi.title = 'Color Discrimination' THEN wi.level_required
      ELSE NULL
    END) AS sr_color_discrimination,
    MAX(CASE
      WHEN wt.item_name = 'Sensory Requirements' AND
        wi.title = 'Depth perception' THEN wi.level_required
      ELSE NULL
    END) AS sr_depth_perception,
    MAX(CASE
      WHEN wt.item_name = 'Sensory Requirements' AND
        wi.title = 'Hearing' THEN wi.level_required
      ELSE NULL
    END) AS sr_hearing,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Bloodborne pathogens' THEN wi.level_required
      ELSE NULL
    END) AS er_bloodborne_pathogens,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Chemical' THEN wi.level_required
      ELSE NULL
    END) AS er_chemical,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Airborne communicable diseases' THEN wi.level_required
      ELSE NULL
    END) AS er_airborne_communicable_diseases,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Extreme temperatures' THEN wi.level_required
      ELSE NULL
    END) AS er_extreme_temperatures,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Radiation' THEN wi.level_required
      ELSE NULL
    END) AS er_radiation,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Uneven surfaces or elevations' THEN wi.level_required
      ELSE NULL
    END) AS er_uneven_surfaces_or_elevations,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Extreme noise levels' THEN wi.level_required
      ELSE NULL
    END) AS er_extreme_noise_levels,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Dust/particular matter' THEN wi.level_required
      ELSE NULL
    END) AS er_dust_particular_matter,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Other (List)' THEN wi.weight2
      ELSE NULL
    END) AS er_other,
    MAX(CASE
      WHEN wt.item_name = 'Environment Requirements' AND
        wi.title = 'Usual workday hours' THEN wi.level_required
      ELSE NULL
    END) AS er_usual_workday_hours
  FROM cp_work_environment_items AS wi
  JOIN cp_work_environment_item_types AS wt
    ON wi.work_item_type_id = wt.work_item_type_id
    AND wi.organization_id = p_organization_id /*Param*/
    AND wi.active_p = p_active_p /*Param*/
  GROUP BY wi.org_job_id) AS ei
    ON oj.org_job_id = ei.org_job_id
  WHERE oj.organization_id = p_organization_id /*Param*/
  AND oj.status IN (p_status) /*Param*/
  AND oj.active_p = p_active_p /*Param*/
  AND oj.base_job_p = TRUE
  ORDER BY oj.org_job_code;
END;
$body$
LANGUAGE 'plpgsql'
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER;

1 个答案:

答案 0 :(得分:1)

看起来编译器与department_number混淆。

它使用了两次:作为输出参数out department_number INTEGER和查询列d.num AS department_number的别名。

尝试将department_number更改为其中一个地方的其他地方,它应该有效。

BTW:您使用的是什么版本的PostgreSQL?