PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹37. PL/pgSQL - SQL 誺䘋臺蘔媆誕嬉誕

37.3. PL/pgSQL 䔇䂷悇

PL/pgSQL 滇婔䓉庖䂷悇䔇臺蘔㔗庘昄垔幬䔇欔橬桺橸鄘媙釂滇婔婻㔗婔婻庖䫘婋麵䔇桹濘垔幬

[<<label>>]
[DECLARE
    declarations]
BEGIN
    statements
END [label];

庖婺䔇懟婻弄滯启懟溇臺埖鄘滇䫘婔婻彖埙䂽溵䔇套悩婔婻床庖婘埥崡婔婻庖麯闼幽 END 劯麵媙釂橬婻彖埙套婪欔誄婉誺䂷溘庘昄嘷䔇橔劯䔇 END 埇傖婉襕認婻彖埙㔗

欔橬噿髞庖启湺臖严鄘滇崓償喍方噿䔇湺臖严赆锊劆婄蘸扵潊償喍庖严鍴麂赆埯嚘埙寙啘㔗

婘 PL/pgSQL 麯橬婴䓉䌂傋濘麪㔗埯乘檻埙(--)嚘庺婔婻欷匘彄臖臯䂷儆䔇濘麪㔗/*嚘庺婔婻庖濘麪婔䕘欷匘彄婋婔婻 */ 䔇庺䯄㔗庖濘麪婉脘啯喖嘖滇埯乘檻埙濘麪埇傖寙啘婘庖濘麪麯麵幽婫埯乘檻埙埇傖锊薟庖濘麪彖锫严 /**/

庖臺埖枕麯䔇傂嘘臺埖鄘埇傖滇婔婻床庖㔗床庖埇傖䫘庯锂膏彖䂇潡蔙檪埻麟匔鄘寡婺嘩䫘庯婔婻懫膄償䔇臺埖䂇㔗

婘臺埖庖嬉麵䔇弄滯枕(declarations section)麯垔幬䔇埻麟婘懟渇誕噖臺埖庖施鄘彺哋寡婺垄傸䔇䚺䩕唚蔯婉滇懟渇庘昄脄䫘施彺哋寡婔渇㔗懫套

CREATE FUNCTION somefunc() RETURNS integer AS $$
DECLARE
    quantity integer := 30;
BEGIN
    RAISE NOTICE 'Quantity here is %', quantity;  -- 婘認麯䔇昄麟滇 30
    quantity := 50;
    --
    -- 录傺婔婻床庖
    --
    DECLARE
        quantity integer := 80;
    BEGIN
        RAISE NOTICE 'Quantity here is %', quantity;  -- 婘認麯䔇昄麟滇 80
    END;

    RAISE NOTICE 'Quantity here is %', quantity;  -- 婘認麯䔇昄麟滇 50

    RETURN quantity;
END;
$$ LANGUAGE plpgsql;

婔垔婉襕檪 PL/pgSQL 麯䫘庯臺埖彖䂇䔇 BEGIN/END 启䫘庯庋媇毓彽䔇昄扞康变傴抂晙庖㔗PL/pgSQL 䔇 BEGIN/END 埻滇䫘庯彖䂇(臏濘償 C 麯䔇 { 婯 } )垄傸婉嚔嚔哋启䂷溘婔婻庋媇㔗庘昄启蓥埏単誺䘋攂滇婘婔婻䫌崡北变傴傺䆋蕙準䔇庋媇麯欓臯垄傸方濘嚔哋潡蔙柊庴庋媇啹婺 PostgreSQL 澇橬啯喖庋媇㔗婉誺婔婻寙劆 EXCEPTION 床埖䔇庖垂鍙婪嘵潊婔婻床庋媇垄埇傖婘婉嘌巉崡北庋媇䔇愙喕婋啂悔㔗敘崔䕩噿媇敇臙埗黙誗37.7.5


劯锔饡釕嬉誕
嚔埏 PL/pgSQL 䔇婔底柊䴺婪婔亓弄滯