Skip to Content
《Head First SQL》 (Chinese Edition), 2nd Edition
book

《Head First SQL》 (Chinese Edition), 2nd Edition

by Kimberly Fessel
June 2026
740 pages
13h 7m
Chinese
O'Reilly Media, Inc.
Content preview from 《Head First SQL》 (Chinese Edition), 2nd Edition

第 13 章 函数 和存储过程:可重用代码

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

Image

不要让代码白白浪费,要加以复用。一遍又一遍地重复输入大同小异的代码,不仅很快会让人厌倦,更是一种不必要的浪费。如果您已准备好树立可持续的编程理念,本章将向您展示如何 保存 SQL 命令并在函数 为你提供了一条捷径,可以对全新的输入执行一组预先存储的计算。它们会返回计算结果,你甚至可以将其作为新列的值进行SELECT查询。多么环保又时尚!但尽管函数非常强大,它们并非万能的。若要修改表数据或执行其他复杂操作,你需要编写自己的 存储过程 。这些 参数和变量 可不会自动“升级”,所以请翻页开始学习吧。

重复劳动……

尽管编写 SQL 代码很有趣,但你可能会厌倦一遍又一遍地编写几乎相同的 SQL 代码,只是稍作修改。当艾比在寻找天气条件各异的地方时,似乎正面临着这一问题的困扰。

Image
Image
Image

利用函数实现代码复用

如果你发现自己在不断复制、粘贴并稍作修改代码,那么你很可能需要一个函数。 函数 让你只需编写一次代码, 重复使用该代码 。函数通常会接收用户输入,这些输入被称为 参数,并根据你传入的值来改变其行为。

这听起来可能很奇怪,但实际上你已经使用过几个 内置的 SQL 函数。现在是时候来“找出函数”了!

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

What Successful Project Managers Do

What Successful Project Managers Do

W. Scott Cameron, Jeffrey S. Russell, Edward J. Hoffman, Alexander Laufer
How to Overcome a Power Deficit

How to Overcome a Power Deficit

Cyril Bouquet, Jean-Louis Barsoux
Relational Power Is the New Currency of Hybrid Work

Relational Power Is the New Currency of Hybrid Work

Lebene Soga, Yemisi Bolade-Ogunfodun, Nazrul Islam, Joseph Amankwah-Amoah

Publisher Resources

ISBN: 0642572408114