POSTGRESQL - 返回选择查询时出错

时间:2016-12-22 05:21:49

标签: postgresql

这是我的函数,它在执行此函数时返回错误:

create or replace function country(varchar) 

returns customer(customer_id integer,
                 first_name varchar,                       
                  last_name varchar) as $$


 select customer_id, customer.first_name, customer.last_name 

 from customer 

     inner join address on customer.address_id = address.address_id 
     inner join city on address.city_id = city.city_id 
     inner join country on city.country_id = country.country_id 

where country = '$1';

$$  
language sql;

返回查询时出错。

1 个答案:

答案 0 :(得分:1)

使用此解决方案的创建类型:

create type ct as (customer_id integer, first_name varchar, last_name varchar)

现在创建功能

create or replace function country(varchar) 

returns ct as $$

select customer_id, first_name, last_name from customer 
     inner join address on customer.address_id = address.address_id 
     inner join city on address.city_id = city.city_id 
     inner join country on city.country_id = country.country_id    
where country = $1;

$$ language sql;

或使用此

create or replace function country(varchar) 

returns table(customer_id integer,
                 first_name varchar,                       
                  last_name varchar) as $$ 

 select customer_id, customer.first_name, customer.last_name 

 from customer 

     inner join address on customer.address_id = address.address_id 
     inner join city on address.city_id = city.city_id 
     inner join country on city.country_id = country.country_id 

where country = $1;

$$language sql;