2016-07-08 427 views
0

我試圖創建帶有級聯值結果的函數。POSTGRESQL:帶有Concat值的函數結果

見下圖:

CREATE OR REPLACE FUNCTION select_name() 
RETURNS TABLE(name text) AS 
$BODY$ 

BEGIN 
RETURN QUERY 

select 
cast(first_name as text) ||' ' || cast(middle_name as text) ||' ' || cast(last_name as text) as name 
from table_name; 

END; 
$BODY$ 
LANGUAGE plpgsql VOLATILE 
COST 100 
ROWS 1000; 

但是在查詢

select * from select_name(); 

它顯示錯誤:

ERROR: relation "select_name" does not exist 
LINE 8: select * from select_name 
       ^
********** Error ********** 

ERROR: relation "select_name" does not exist 
SQL state: 42P01 
Character: 159 

我卡在這裏。 請幫忙。

+1

您的函數適用於我......錯誤消息看起來像括號在查詢中被忽略。我對「LINE 8」和「Character:159」也很懷疑。也許你省略了一些可以澄清問題的細節。順便說一下:''firstname'|| NULL IS NULL'。 –

+2

我同意Laurenz:它似乎忘記了使用'()'並且只是從select_name運行select *。不相關的,但是:函數中的表達式可以簡化爲'select concat_ws('',first_name,middle_name,last_name)作爲name''來正確處理'null'值(Plus:你不需要PL/pgSQL ,一個普通的SQL函數就足夠了) –

+0

謝謝大家。 :)我再次審查,並做了一些調整,它的工作!我用這個:'作爲名字'將(名字首字母||''||中間名||''||姓氏作爲文本)強制轉換 – fLen

回答

0

我試着用下面的語句,它工作。^_^

select 
cast(first_name ||' ' || middle_name ||' ' || last_name as text) as name 
from table_name; 
0

試試這個,就像飛了一樣。

CREATE OR REPLACE FUNCTION select_name() 
RETURNS TABLE(name text) AS 
$$ 

BEGIN 
RETURN QUERY 

select 
select 
cast(first_name as text) ||' ' || cast(middle_name as text) ||' ' || cast(last_name as text) as name 
from table_name; 

     END; 
$$ LANGUAGE plpgsql;