2016-12-22 80 views
1

這裏是我的功能,執行此功能時,它會返回錯誤:POSTGRESQL - 錯誤的返回select查詢

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; 

錯誤返回查詢發生。

+0

我認爲你有一個錯字,你應該使用'哪裏國家= $ 1',_沒有單引號。 –

+0

你有什麼錯誤? – Mureinik

+0

錯誤:在「整數」處或附近的語法錯誤 LINE 2:返回customer(customer_id integer,謝謝Tim我刪除了單引號,但這個錯誤表明我說有語法錯誤,並指出整數可以幫助我進出口新的Postgres的 – kimdasuncion12

回答

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; 
+0

謝謝薩達姆我用你的編輯功能,但現在它返回<不願透露姓名的門戶網站1>。 – kimdasuncion12

+0

dvdrental =#選擇國家('阿爾及利亞'); 國家 -------------------- <無名門戶6> (1行) 這就是我得到的結果 – kimdasuncion12

+0

你在使用光標嗎?你正在使用1或2(我寫上面) –