给定一个返回查询的PostgreSQL函数:
CREATE OR REPLACE FUNCTION word_frequency(_max_tokens int)
RETURNS TABLE (
txt text -- visible as OUT parameter inside and outside function
, cnt bigint
, ratio bigint) AS
$func$
BEGIN
RETURN QUERY
SELECT t.txt
, count(*) AS cnt -- column alias only visible inside
, (count(*) * 100) / _max_tokens -- I added brackets
FROM (
SELECT t.txt
FROM token t
WHERE t.chartype = 'ALPHABETIC'
LIMIT _max_tokens
) t
GROUP BY t.txt
ORDER BY cnt DESC; -- note the potential ambiguity
END
$func$ LANGUAGE plpgsql;
如何检索此功能的结构?我的意思是,我知道此函数将返回txt
,cnt
和ratio
列,但是如何创建返回这些列名称的查询?我试图在information_schema
架构上找到这些列名,但我不能。
这个假设查询的预期结果将是这样的:
3 results found:
---------------------------------
?column_name? | ?function_name?
---------------------------------
txt word_frequency
cnt word_frequency
ratio word_frequency
答案 0 :(得分:1)
此信息存储在SELECT unnest(p.proargnames) as column_name,
p.proname as function_name
FROM pg_proc p
JOIN pg_namespace n ON p.pronamespace = n.oid
WHERE n.nspname = 'public'
AND p.proname = 'word_frequency'
public function getGeneralSettings()
{
$generalsetting = DB::table('generalsettings')
->where('id', '=', 1)
->first();
return view('backend.generalsettings.add-general')->with('general', $generalsetting);
}
答案 1 :(得分:1)
基于 a_horse_with_no_name 的答案,我带来了这个最终版本:
SELECT
column_name,
function_name
FROM
(
SELECT
unnest(p.proargnames) as column_name,
unnest(p.proargmodes) as column_type,
p.proname as function_name
FROM pg_proc p
JOIN pg_namespace n ON p.pronamespace = n.oid
WHERE n.nspname = 'public'
AND p.proname = 'my_function'
) as temp_table
WHERE column_type = 't';
我只是省略了参数,只返回函数返回的列