什么是“存储过程”,它们是如何工作的?

存储过程是由什么组成的(每个东西都必须是存储过程)?


当前回答

想想这样的情况,

You have a database with data. There are a number of different applications needed to access that central database, and in the future some new applications too. If you are going to insert the inline database queries to access the central database, inside each application's code individually, then probably you have to duplicate the same query again and again inside different applications' code. In that kind of a situation, you can use stored procedures (SPs). With stored procedures, you are writing number of common queries (procedures) and store them with the central database. Now the duplication of work will never happen as before and the data access and the maintenance will be done centrally.

注意:

In the above situation, you may wonder "Why cannot we introduce a central data access server to interact with all the applications? Yes. That will be a possible alternative. But, The main advantage with SPs over that approach is, unlike your data-access-code with inline queries, SPs are pre-compiled statements, so they will execute faster. And communication costs (over networks) will be at a minimum. Opposite to that, SPs will add some more load to the database server. If that would be a concern according to the situation, a centralized data access server with inline queries will be a better choice.

其他回答

存储过程是可以以多种方式执行的一批SQL语句。大多数主流DBMs都支持存储过程;然而,并非所有国家都这样做。您需要查看特定的DBMS帮助文档以了解细节。因为我最熟悉的SQL Server,我将使用它作为我的样本。

要创建一个存储过程,语法相当简单:

CREATE PROCEDURE <owner>.<procedure name>

     <Param> <datatype>

AS

     <Body>

例如:

CREATE PROCEDURE Users_GetUserInfo

    @login nvarchar(30)=null

AS

    SELECT * from [Users]
    WHERE ISNULL(@login,login)=login

存储过程的一个好处是,您可以将数据访问逻辑集中到一个地方,这样DBA就可以很容易地进行优化。存储过程还有安全方面的好处,可以将执行权限授予存储过程,但用户不需要对底层表具有读/写权限。这是反对SQL注入的良好的第一步。

存储过程也有缺点,主要是与基本CRUD操作相关的维护。假设对于每个表,您有一个插入、更新、删除和至少一个基于主键的选择,这意味着每个表将有4个过程。现在有一个400个表的数据库,其中有1600个过程!这还是在你没有副本的前提下你可能会有。

这就是使用ORM或其他方法自动生成基本CRUD操作的优点所在。

一般来说,存储过程是一个“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注入攻击。

存储过程是一组预编译的SQL语句,用于执行特殊任务。

示例:如果我有一个Employee表

Employee ID  Name       Age  Mobile
---------------------------------------
001          Sidheswar  25   9938885469
002          Pritish    32   9178542436

首先,我正在检索Employee表:

Create Procedure Employee details
As
Begin
    Select * from Employee
End

在SQL Server上运行此过程:

Execute   Employee details

--- (Employee details is a user defined name, give a name as you want)

然后,我将值插入到Employee表中

Create Procedure employee_insert
    (@EmployeeID int, @Name Varchar(30), @Age int, @Mobile int)
As
Begin
    Insert Into Employee
    Values (@EmployeeID, @Name, @Age, @Mobile)
End

在SQL Server上运行参数化过程:

Execute employee_insert 003,’xyz’,27,1234567890

  --(Parameter size must be same as declared column size)

示例:@Name Varchar(30)

在Employee表中,Name列的大小必须是varchar(30)。

存储过程将帮助您在服务器中编写代码。您可以传递参数并查找输出。

create procedure_name (para1 int,para2 decimal)
as
select * from TableName

存储过程是一个预编译的SQL语句集,它执行一些特定的任务。 存储过程应该使用EXEC单独执行 存储过程可以返回多个参数 存储过程可用于实现事务