什么是“存储过程”,它们是如何工作的?
存储过程是由什么组成的(每个东西都必须是存储过程)?
什么是“存储过程”,它们是如何工作的?
存储过程是由什么组成的(每个东西都必须是存储过程)?
当前回答
存储过程是一组预编译的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)。
其他回答
存储过程用于检索数据、修改数据和删除数据库表中的数据。你不需要每次在SQL数据库中插入、更新或删除数据时都写一个完整的SQL命令。
想想这样的情况,
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 Server中的存储过程可以接受输入参数并返回多个输出参数值;在SQL Server中,存储过程编写语句在数据库中执行操作,并将状态值返回给调用过程或批处理。
在SQL Server中使用存储过程的好处
它们允许模块化编程。 它们允许更快的执行。 它们可以减少网络流量。 它们可以用作一种安全机制。
下面是一个存储过程的示例,它接受参数,执行查询并返回结果。具体来说,存储过程接受BusinessEntityID作为参数,并使用它来匹配HumanResources的主键。Employee表返回所请求的员工。
> create procedure HumanResources.uspFindEmployee `*<<<---Store procedure name`*
@businessEntityID `<<<----parameter`
as
begin
SET NOCOUNT ON;
Select businessEntityId, <<<----select statement to return one employee row
NationalIdNumber,
LoginID,
JobTitle,
HireData,
From HumanResources.Employee
where businessEntityId =@businessEntityId <<<---parameter used as criteria
end
这是我从essential.com上学到的,非常有用。
存储过程是一个预编译的SQL语句集,它执行一些特定的任务。 存储过程应该使用EXEC单独执行 存储过程可以返回多个参数 存储过程可用于实现事务
存储过程是一组预编译的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)。