如何在PostgreSQL中传递类型数组作为参数?

时间:2018-11-01 06:16:12

标签: sql postgresql

试图将数组类型的对象作为参数传递并出现错误。

在我的案例中,如何将2个对象EmployeeType作为数组传递?

 1. SELECT employeeTester(row ( row (687678655453,'Yan','Burkov','Yan.Burkov1992@gmail.com','@_+Yan987pl'),row (897689768768,'Ludmila','Burkov','Ludmila.Burkov1967@gmail.com','+_@Ludmia87vb')));
  
    

错误:函数employeetester(record)不存在

  
 2. `SELECT employeeTester( row (687678655453,'Yan','Burkov','Yan.Burkov1992@gmail.com','@_+Yan987pl'),row (897689768768,'Ludmila','Burkov','Ludmila.Burkov1967@gmail.com','+_@Ludmia87vb'));`
  
    

错误:函数employeetester(记录,记录)不存在

  
 3. `SELECT employeeTester(ARRAY [row(687678655453,'Yan','Burkov','Yan.Burkov1992@gmail.com','@_+Yan987pl'), row (897689768768,'Ludmila','Burkov','Ludmila.Burkov1967@gmail.com','+_@Ludmia87vb')]);`
  
    

错误:函数employeetester(record [])不存在

  
 CREATE OR REPLACE FUNCTION employeeTester(employees EmployeeType[]) RETURNS text [] AS $$

    DECLARE counter SMALLINT = 0; emp EmployeeType; errors text [];


    BEGIN

        FOREACH emp IN ARRAY employees LOOP

                RAISE NOTICE 'Employee data %',emp;

        END LOOP;

        RETURN errors;

    END;


$$ LANGUAGE plpgsql;

使用一个类型化的对象:

CREATE OR REPLACE FUNCTION employeeTester(employees EmployeeType) RETURNS text [] AS $$

    errors text [];


    BEGIN

        RAISE NOTICE 'Employee id %',employees.emp_id;
        RAISE NOTICE 'Employee FirstName %',employees.firstname;
        RAISE NOTICE 'Employee LastName %',employees.lastname;
        RAISE NOTICE 'Employee Email %',employees.email;
        RAISE NOTICE 'Employee Password %',employees.e_password;

        RETURN errors;

    END;


 $$ LANGUAGE plpgsql;

结果:

[2018-11-01 08:13:08] [00000] Employee id 687678655453
[2018-11-01 08:13:08] [00000] Employee FirstName Yan
[2018-11-01 08:13:08] [00000] Employee LastName Burkov
[2018-11-01 08:13:08] [00000] Employee Email Yan.Burkov1992@gmail.com
[2018-11-01 08:13:08] [00000] Employee Password @_+Yan987pl

1 个答案:

答案 0 :(得分:0)

如果您要传递记录数组,则应这样调用。

select empTester(ARRAY[row(687678655453,'Yan','Burkov','Yan.Burkov1992@gmail.com','@_+Yan987pl')
 ,row(687678653,'sen','Burkov','Sen.Burkov1992@gmail.com','@_+Sen987pl')]);