什么是“存储过程”,它们是如何工作的?
存储过程是由什么组成的(每个东西都必须是存储过程)?
什么是“存储过程”,它们是如何工作的?
存储过程是由什么组成的(每个东西都必须是存储过程)?
当前回答
在存储过程中,语句只被写入一次,从而减少了客户端和服务器之间的网络流量。 我们也可以避免Sql注入攻击。
Incase if you are using a third party program in your application for processing payments, here database should only expose the information it needed and activity that this third party has been authorized, by this we can achieve data confidentiality by setting permissions using Stored Procedures. The updation of table should only done to the table it is targeting but it shouldn't update any other table, by which we can achieve data integrity using transaction processing and error handling. If you want to return one or more items with a data type then it is better to use an output parameter. In Stored Procedures, we use an output parameter for anything that needs to be returned. If you want to return only one item with only an integer data type then better use a return value. Actually the return value is only to inform success or failure of the Stored Procedure.
其他回答
存储过程不过是编译成单个执行计划的一组SQL语句。
创建一次,调用n次 减少网络流量
示例:创建存储过程
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE GetEmployee
@EmployeeID int = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT FirstName, LastName, BirthDate, City, Country
FROM Employees
WHERE EmployeeID = @EmployeeID
END
GO
更改或修改存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE GetEmployee
@EmployeeID int = 0
AS
BEGIN
SET NOCOUNT ON;
SELECT FirstName, LastName, BirthDate, City, Country
FROM Employees
WHERE EmployeeID = @EmployeeID
END
GO
删除存储过程:
DROP PROCEDURE GetEmployee
一般来说,存储过程是一个“SQL函数”。他们有:
-- a name
CREATE PROCEDURE spGetPerson
-- parameters
CREATE PROCEDURE spGetPerson(@PersonID int)
-- a body
CREATE PROCEDURE spGetPerson(@PersonID int)
AS
SELECT FirstName, LastName ....
FROM People
WHERE PersonID = @PersonID
这是一个以T-SQL为重点的示例。存储过程可以执行大多数SQL语句,返回标量值和基于表的值,并且被认为更安全,因为它们可以防止SQL注入攻击。
序言:SQL92标准创建于1992年,并由Firebase DB推广。该标准引入了“存储过程”。
** 透传查询:一个字符串(通常通过编程连接),计算为语法正确的SQL语句,通常在服务器层生成(在PHP、Python、PERL等语言中)。然后将这些语句传递到数据库。 **
** 触发器:设计用于响应数据库事件(通常是DML事件)而触发的一段代码,通常用于强制数据完整性。 **
解释什么是存储过程的最好方法是解释执行DB逻辑的传统方式(即不使用存储过程)。
创建系统的传统方式是使用“直通查询”,并且可能在DB中有触发器。 几乎所有不使用存储过程的人都会使用一个叫做"直通查询"的东西
随着存储过程的现代约定,触发器和“透传查询”一起成为传统。
存储过程的优点是:
They can be cached as the physical text of the Stored Procedure never changes. They have built in mechanisms against malicious SQL injection. Only the parameters need be checked for malicious SQL injection saving a lot of processor overhead. Most modern database engines actually compile Stored Procedures. They increase the degree of abstraction between tiers. They occur in the same process as the database allowing for greater optimisation and throughput. The entire workflow of the back end can be tested without client side code. (for example the Execute command in Transact SQL or the CALL command in MySQL). They can be used to enhance security because they can be leveraged to disallow the database to be accessed in a way that is inconsistent with how the system is designed to work. This is done through the database user permission mechanism. For example you can give users privileges only to EXECUTE Stored Procedures rather that SELECT, UPDATE etc privileges. No need for the DML layer associated with triggers. ** Using so much as one trigger, opens up a DML layer which is very processor intensive **
总之,在创建一个新的SQL数据库系统时,没有理由使用直通查询。
值得一提的是,在已经使用触发器或直通查询的遗留系统中使用存储过程是完全安全的;这意味着从遗留过程迁移到存储过程是非常容易的,而且这样的迁移不会使系统长时间停机。
存储过程主要用于在数据库上执行某些任务。例如
从数据上的一些业务逻辑获取数据库结果集。 在一次调用中执行多个数据库操作。 用于将数据从一个表迁移到另一个表。 可用于其他编程语言,如Java。
存储过程是SQL语句和过程逻辑的命名集合,即编译、验证并存储在服务器数据库中。存储过程通常被视为其他数据库对象,并通过服务器安全机制进行控制。