简体   繁体   English

如何定义将用作函数内的 IN 参数的参数?

[英]How to define a parameter that will be used as an IN argument inside a function?

I want to make a function that receives two parameters.我想做一个接收两个参数的函数。 The first one represents the salaries from a group of employees, and second one, the codes from a group of departments.第一个代表一组员工的工资,第二个代表一组部门的代码。 Both, P_IN_SALARIES and P_IN_DEPARTMENTS_CODES parameters, will be used as arguments in an IN function of a query, just as demonstrated in the code below: P_IN_SALARIES 和 P_IN_DEPARTMENTS_CODES 参数都将用作查询的 IN 函数中的参数,如下面的代码所示:

CREATE OR REPLACE FUNCTION public.get_employees_id(P_IN_SALARIES WHICH_TYPE_COMES_HERE, P_IN_DEPARTMENTS_CODES WHICH_TYPE_COMES_HERE)
 RETURNS text
 LANGUAGE plpgsql
AS $function$
declare
    v_employees_ids text;
begin

    select STRING_AGG(employee.id || '', ',') into v_employees_ids
    from employee 
    inner join departament on department.id = employee.departament_id
    where employee.salary in (P_IN_SALARIES)
    and department.code in (P_IN_DEPARTMENTS_CODES);

    RETURN v_employees_ids;
END;
$function$
  • What is the type of a IN parameter in a SQL statement? SQL 语句中的 IN 参数的类型是什么?
  • Is there a generic one that I might use in order to allow a kind of portability on an occasional database exchange (eg to Oracle)?是否有一个通用的我可以使用以便在偶尔的数据库交换(例如到 Oracle)中允许某种可移植性?
  • How to call this function in a hibernate query?如何在休眠查询中调用此函数?

In Oracle, you can use a collection data type:在 Oracle 中,您可以使用集合数据类型:

CREATE TABLE number_list IS TABLE OF NUMBER;

Then you can use the MEMBER OF operator rather than IN :然后您可以使用MEMBER OF运算符而不是IN

CREATE FUNCTION public.get_employees_id(
  P_IN_SALARIES          IN number_list,
  P_IN_DEPARTMENTS_CODES IN number_list
) RETURNS VARCHAR2
IS
    v_employees_ids VARCHAR2(4000);
BEGIN
    SELECT LISTAGG( id, ',' ) WITHIN GROUP ( ORDER BY id )
    INTO   v_employees_ids
    FROM   employee e 
           inner join departament d
           on ( d.id = e.departament_id )
    WHERE  e.salary MEMBER OF P_IN_SALARIES
    AND    d.department.code MEMBER OF P_IN_DEPARTMENTS_CODES;

    RETURN v_employees_ids;
END;
/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM