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.

相关新闻

我与 IT 这三十年:2003,Linux 遇到法律阴影

我与 IT 这三十年:2003,Linux 遇到法律阴影

2003 年,Linux 的上升感还在,但空气里多了一点阴影。 前一年,Linux 正在学会做生意。Red Hat 推出面向企业的 Advanced Server,UnitedLinux 试图联合几家发行版公司,OpenOffice.org、Mozilla、GNOME、KDE 又让桌面 Li…

2026/9/29 4:26:12 阅读更多 →
3种实用方法:Magpie窗口超分辨率工具完整指南

3种实用方法:Magpie窗口超分辨率工具完整指南

3种实用方法:Magpie窗口超分辨率工具完整指南 【免费下载链接】Magpie A general-purpose window upscaler for Windows 10/11. 项目地址: https://gitcode.com/gh_mirrors/mag/Magpie 还在为老旧游戏在4K屏幕上模糊不清而烦恼吗?或者想要让那些不…

2026/10/11 16:28:37 阅读更多 →
三分钟掌握高效OFD转PDF的完整解决方案

三分钟掌握高效OFD转PDF的完整解决方案

三分钟掌握高效OFD转PDF的完整解决方案 【免费下载链接】Ofd2Pdf Convert OFD files to PDF files. 项目地址: https://gitcode.com/gh_mirrors/ofd/Ofd2Pdf 在数字化办公环境中,OFD转PDF成为许多用户面临的常见挑战。Ofd2Pdf作为一款开源免费的文档格式转换…

2026/10/9 10:09:29 阅读更多 →

最新新闻

AI编程教程为何爆火2万Star:从零基础到生产落地的完整路径

AI编程教程为何爆火2万Star:从零基础到生产落地的完整路径

1. 这个“2万Star”到底意味着什么:从GitHub数字看AI编程教程的真实分量很多人看到标题里“2万Star”第一反应是:哇,火了。但作为在开发者教育领域摸爬滚打十多年的老手,我得先泼一盆冷静的水——Star数从来不是教学内容质量的直接…

2026/10/14 7:19:37 阅读更多 →
DNN多分类任务核心设计:Softmax、交叉熵与PyTorch实战

DNN多分类任务核心设计:Softmax、交叉熵与PyTorch实战

2. 多分类任务的三个核心设计问题先说结论:DNN跑多分类,真正需要想清楚的只有三件事——输出层的神经元数量、输出层的激活函数、损失函数的选择。这三件事定下来,模型框架基本就定死了,后面做的都是调参和优化。2.1 输出层神经元…

2026/10/14 7:19:37 阅读更多 →
离线环境部署MySQL 5.7:tar.gz包安装与避坑指南

离线环境部署MySQL 5.7:tar.gz包安装与避坑指南

简介:本资源为 MySQL 5.7.40 在 Linux-glibc2.12 环境下的 x86_64 离线安装包,面向需要在无外网或内网环境中部署关系型数据库的运维与后端开发人员。压缩包共 379 个文件,约 646.59MB,以 h 头文件、so 动态库、xml 配置、sql 脚本…

2026/10/14 7:19:37 阅读更多 →
华为HCIP-AI测试卷高效复习指南:从背答案到动手验证

华为HCIP-AI测试卷高效复习指南:从背答案到动手验证

简介:这份华为认证HCIP-AI测试卷与答案文档,面向备考华为人工智能高级认证的考生及希望系统检验AI基础知识的开发者,帮助梳理图像处理、语音合成、HiAI生态与快应用开发等核心考点。资源包共1个docx文件,约14KB,内容以…

2026/10/14 7:19:37 阅读更多 →
基于rSVD与软阈值的Matlab高效信号去噪方法

基于rSVD与软阈值的Matlab高效信号去噪方法

1. 项目背景与核心挑战谐波噪声是信号处理领域常见的干扰类型,广泛存在于电力系统监测、机械振动分析、生物医学信号采集等场景。传统去噪方法如傅里叶滤波、小波变换在面对高维大数据集时往往面临计算效率低下、噪声鲁棒性不足的问题。这个Matlab项目提出了一种融合…

2026/10/14 7:19:37 阅读更多 →
智慧农业最后一百米:破解乡村数字化落地的设备与运维难题

智慧农业最后一百米:破解乡村数字化落地的设备与运维难题

做了快八年农业数字化项目,我最深的一个体会是:智慧农业真正难的从来不是算法有多高级、传感器有多精密,而是怎么让技术在没有网线、没有专职IT人员、甚至不少农户只用老年机的村子里,持续稳定地转起来。前段时间复盘了一个扎根基…

2026/10/14 7:18:36 阅读更多 →

日新闻

AnyPS5通用适配层设计:兼容性与性能的工程取舍

AnyPS5通用适配层设计:兼容性与性能的工程取舍

1. 从"AnyPS5"这个名字说起:它到底想解决什么问题第一次看到"AnyPS5"这个标题,我脑子里冒出来的第一个念头是:这大概率是一个围绕"跨平台运行"或者"通用适配"做文章的项目。名字里的"Any"…

2026/10/14 0:00:41 阅读更多 →
Flutter跨平台开发实战:二手交易应用与HarmonyOS适配

Flutter跨平台开发实战:二手交易应用与HarmonyOS适配

1. 项目概述:Flutter跨平台二手交易应用开发作为一名长期从事移动应用开发的工程师,我最近完成了一个基于Flutter框架的二手物品交易平台项目。这个项目最吸引我的地方在于它完美结合了Flutter的跨平台优势与HarmonyOS的生态特性,实现了"…

2026/10/14 0:00:41 阅读更多 →
DFS算法解决飞机降落调度问题实战

DFS算法解决飞机降落调度问题实战

1. 项目背景与核心思路飞机降落问题是一个经典的调度优化场景,也是算法竞赛和面试中的高频题目。这个题目要求我们合理安排多架飞机的降落顺序和时间,确保所有飞机都能安全着陆而不发生冲突。在实际航空管制中,这类问题直接关系到飞行安全和机…

2026/10/14 0:00:41 阅读更多 →

周新闻

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

流感时间序列预测实战:ARIMA/LSTM全流程拆解与避坑指南

简介:基于 ARIMA、LSTM、Transformer 等模型的流感时间序列预测 Python 源码,面向计算机相关专业课程设计与期末大作业学生,以及项目实战学习者。内容覆盖预处理、平稳性检验、定阶、残差分析、多模型对比预测的完整时序建模流程,…

2026/10/14 3:30:28 阅读更多 →
影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别

影刀RPA新手教程:键盘模拟输入实战——输入文本与模拟按键的区别 做影刀RPA自动化,十个新手有八个栽在"往输入框里填东西"这件事上:要么填不进去,要么填了一半,要么直接把原来内容追加在后面。这背后的根因&…

2026/10/13 16:22:04 阅读更多 →
影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容

影刀RPA新手教程:阅文起点小说数据采集实战——书籍信息与章节内容 1. 认识影刀:什么场景该用RPA采小说数据 起点中文网的页面结构相对稳定——分类榜单、书籍详情、章节内容三块独立页面,跳转链路清晰。这种场景非常适合影刀自动化&#x…

2026/10/14 3:29:58 阅读更多 →

月新闻

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

我发现了一个新思路:用 Remotion + Claude Code 像写代码一样自动化生成短视频

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/13 22:01:27 阅读更多 →
Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

Windows下 Codex 中 Chrome 和 Computer Use 插件不可用问题排查及解决参考方式:TaoToken 统一 Key 配置与验证

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/13 14:57:39 阅读更多 →
黑夜航拍船只数据集训练YOLOV5模型全流程解析

黑夜航拍船只数据集训练YOLOV5模型全流程解析

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/12 18:11:17 阅读更多 →