2014-10-28 126 views
0

我想打印從查詢中獲得的所有表格。但是,我的代碼只顯示該函數的名稱。Postgres:如何在循環中打印值

# create or replace function remove_scene() 
# RETURNS void AS $$ 
# DECLARE 
# row record; 
# BEGIN 
# FOR row IN 
# select table_name from information_schema.tables WHERE table_schema = 'public' AND table_name ~ 'measurement[0-9]' 
# LOOP 
# RAISE NOTICE '** %', quote_ident(row.table_name); 
# END LOOP; 
# END; 
# $$ LANGUAGE plpgsql; 
CREATE FUNCTION 
# select remove_scene(); 
remove_scene 
-------------- 

(1 row) 

# 

回答

0

你沒有看到任何東西的原因是你的函數沒有返回任何東西。使用RAISE NOTICE「寫入」的文本將僅在客戶端顯示,如果a)客戶端支持(psql)和b)如果您的會話配置爲實際返回它們。爲了在psql可以看到你加薪,你需要使用:

set client_min_messages = NOTICE; 

您目前的設定可能是WARNING,並且因而不顯示NOTICE

但是:在RAISE NOTICE的循環中這樣做效率很低。這將是更好的改變你的功能分爲一組返回函數:

create or replace function remove_scene() 
    RETURNS table(table_name text) 
AS $$ 
    select table_name 
    from information_schema.tables 
    WHERE table_schema = 'public' 
    AND table_name ~ 'measurement[0-9]' 
$$ 
LANGUAGE sql; 

,然後用它是這樣的:

select * 
from remove_scene(); 

另一種辦法是把那個select語句到視圖。