Skip to main content

储存程序 (Stored Procedure)

储存程序 (Stored Procedure),又可称预储程序或者存储过程,是一种在数据库中存储复杂程序,以便外部程序调用的一种数据库对象,它可以视为数据库中的一种函数或子程序。

优缺点

优点

预存程序具有下列的好处:
  • 预存程序可封装,并隐藏复杂的商业逻辑。
  • 预存程序可以回传值,并可以接受参数。
  • 预存程序无法使用 SELECT 指令来运行,因为它是子程序,与查看表数据表用户定义函数不同。
  • 预存程序可以用在数据检验,强制实行商业逻辑等。

缺点

  • 预存程序,往往定制化于特定的数据库上,因为支持的编程语言不同。当切换到其他厂商的数据库系统时,需要重写原有的预存程序。
  • 预存程序的性能调校与撰写,受限于各种数据库系统。

支持预存程序的数据库

数据库系统 预存程序使用的编程语言
CUBRID Java
DB2 SQL PL or Java
Firebird PSQL (Fyracle also supports portions of Oracle's PL/SQL)
Informix SPL
Microsoft SQL Server Transact-SQL and various .NET Framework languages
MySQL own stored procedures, closely adhering to SQL:2003 standard.
Oracle PL/SQL or Java
PostgreSQL PL/pgSQL, can also use own function languages such as pl/perl or pl/php
Sybase ASE Transact-SQL

示例

预存程序是数据库对象之一,必须使用数据定义语言来创建,例如:
以下示例,以Microsoft的SQL Server所以采用的T-SQL语法表示。
CREATE PROCEDURE usp_AddProduct
(
   @Barcode VARCHAR(13),
   @Caption nvarchar(50)
)
AS
BEGIN
 
   IF LEN(@Barcode) < 13 
      RAISERROR('Barcode length is too short.')
 
   INSERT INTO MyProducts (Barcode, Caption) VALUES (@Barcode, @Caption)
 
END
外部程序需要使用 EXECUTE 或 CALL 来调用预存程序。
EXEC usp_AddProduct '2293891100011', 'MyProductCaption'

Comments

Popular posts from this blog

CKA Simulator Kubernetes 1.22

  https://killer.sh Pre Setup Once you've gained access to your terminal it might be wise to spend ~1 minute to setup your environment. You could set these: alias k = kubectl                         # will already be pre-configured export do = "--dry-run=client -o yaml"     # k get pod x $do export now = "--force --grace-period 0"   # k delete pod x $now Vim To make vim use 2 spaces for a tab edit ~/.vimrc to contain: set tabstop=2 set expandtab set shiftwidth=2 More setup suggestions are in the tips section .     Question 1 | Contexts Task weight: 1%   You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts . Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh , the command should use kubectl . Finally write a second command doing the same thing into ...

OWASP Top 10 Threats and Mitigations Exam - Single Select

Last updated 4 Aug 11 Course Title: OWASP Top 10 Threats and Mitigation Exam Questions - Single Select 1) Which of the following consequences is most likely to occur due to an injection attack? Spoofing Cross-site request forgery Denial of service   Correct Insecure direct object references 2) Your application is created using a language that does not support a clear distinction between code and data. Which vulnerability is most likely to occur in your application? Injection   Correct Insecure direct object references Failure to restrict URL access Insufficient transport layer protection 3) Which of the following scenarios is most likely to cause an injection attack? Unvalidated input is embedded in an instruction stream.   Correct Unvalidated input can be distinguished from valid instructions. A Web application does not validate a client’s access to a resource. A Web action performs an operation on behalf of the user without checkin...