我想如果條件得到滿足,運行SQL查詢,但我得到了以下錯誤:PostgreSQL的函數執行查詢
錯誤:單獨的$鏈是未完成或接近«$ FUNC $
我的SQL查詢是:
CREATE OR REPLACE FUNCTION myfunc()
RETURNS TABLE(dateticket date, timeticket time, userid integer, my_all bigint) AS
$func$
BEGIN
IF (SELECT COUNT(DISTINCT(dateticket)) from tickets) = 1 THEN
RETURN QUERY EXECUTE 'select t.*
from (select distinct on (userid) dateticket, timeticket, userid,
count(*) over (partition by userid) as my_all
from tickets t
order by userid, dateticket, timeticket) t
order by my_all, dateticket, timeticket';
ELSE
RETURN QUERY EXECUTE 'select t.*
from (select distinct on (userid) dateticket, timeticket, userid,
count(*) over (partition by userid) as my_all
from tickets t
order by userid, dateticket, timeticket) t
order by my_all DESC, dateticket DESC, timeticket DESC';
END IF;
END;
$$ LANGUAGE plpgsql;
您使用$ func $啓動函數,但嘗試使用$$結束。這些必須與 –