PL/pgSQL Blocks

发布时间:2026/9/15 17:41:15
PL/pgSQL Blocks Summary: in this tutorial, you’ll learn about PL/pgSQL blocks, how to create and execute some blocks.Overview of a PL/pgSQL BlockPL/pgSQL is a blocked structure programming language. PL/pgSQL organizes code into blocks.Here’s the syntax of a block:[ label ] [ DECLARE declarations ] BEGIN statements END [ label ];Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)A block has two main sections:Declaration:The declaration section is optional. It is where you declare variables, constants, and cursors.Body:The body section is required. It is where you put the logic of the block, such as SQL statements.A block may include an optional label appearing at the beginning and end. The label is necessary only when you use statements like theEXITstatement or to qualify the variable names declared in the block.PL/pgSQL Block ExampleThe following example shows how to create a block that displays a messageHello, World:DO $$ BEGIN RAISE NOTICE %, Hello, World; END; $$;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)OutputNOTICE: Hello, WorldCode language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)How it works:TheDOstatement executes the PL/pgSQL block.We use the PL/pgSQL block as a dollar-quoted string constant, which starts with$$and ends with$$. This helps avoid escaping quotations and special characters in the PL/pgSQL code.A block starts with theBEGINkeyword and ends with the keywordEND. Note thatBEGINdoesn’t mean start a transaction.TheRAISE NOTICEstatement displays a notice when you execute the block:RAISE NOTICE %, Hello, World;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)TheRAISE NOTICEstatement starts with a placeholder%, followed by a comma, and the value for that placeholder. The block does not include a declaration section in this example.Executing the Block in psqlFirst, connect to PostgreSQL using the psql tool:psql -U postgresCode language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)The psql tool will prompt you to enter the password for the postgres user:Password for user postgres:Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)You must enter a valid password and press the Enter (or Return) key.Second, copy the PL/pgSQL code above and paste it into the psql tool:postgres# DO postgres-# $$ postgres$# BEGIN postgres$# RAISE NOTICE %, Hello, World; postgres$# END; postgres$# $$; NOTICE: Hello, World DOCode language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)OutputNOTICE: Hello, World DOCode language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)Executing the PL/pgSQL Block Using pgAdmin 4First, launch the pgAdmin4 tool and open the inventory database.Second, open the query tool by following the menu pathTools Query tool:Third, copy and paste the PL/pgSQL block into the query tool:Finally, click theExecute Scriptbutton (or press theF5keyboard shortcut).PL/pgSQL Block with Declaration ExampleThe following example shows how to define a block with a declaration section.DO $$ DECLARE name VARCHAR Joe; BEGIN RAISE NOTICE Hello %, name; END; $$;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)OutputNOTICE: Hello JoeCode language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)In this example, we add the declaration section and declare a variablenamewithVARCHARtype and valueJoe:DECLARE name VARCHAR Joe;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)To declare a variable, you provide the name, data type, and initial value:name VARCHAR Joe;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)We substitute thenamevariable with the placeholder%in theHello %string in theRAISE NOTICEstatement:RAISE NOTICE Hello %, name;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)The output will be like this:NOTICE: Hello JoeCode language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)PL/pgSQL SubblocksIn PL/pgSQL, blocks can be nested within the body of other blocks. Blocks nested inside other blocks are called subblocks. Blocks that contain nested blocks are called outer blocks:Block nesting allows you to organize the blocks into smaller, more manageable trunks. For example:DO $$ DECLARE total_quantity INT 0; BEGIN DECLARE safety_stock INT 10; on_hand_qty INT 100; BEGIN total_quantity safety_stock on_hand_qty; END; RAISE NOTICE The total quantity is %, total_quantity; END; $$;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)OutputNOTICE: The total quantity is 110Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)How it works:In the main block, we declare and initializetotal_quantityto 0:DECLARE total_quantity INT 0;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)In the subblock, we declare and initializesafety_stockto 10 andon_hand_qtyto 100:DECLARE safety_stock INT 10; on_hand_qty INT 100;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)Additionally, we calculate the total quantity by addingsafety_stockandon_hand_qtyand assigning the result tototal_quantity:total_quantity safety_stock on_hand_qty;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)After the subblock, we display the total quantity in the main block:RAISE NOTICE The total quantity is %, total_quantity;Code language: PostgreSQL SQL dialect and PL/pgSQL (pgsql)SummaryPL/pgSQL is a blocked structure programming language.A block includes two main sections: declaration and body.The declaration section is optional, while the body section is required.Blocks can be nested within the body section of other blocks.

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询