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

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


当前回答

在存储过程中,语句只被写入一次,从而减少了客户端和服务器之间的网络流量。 我们也可以避免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.

其他回答

“什么是存储过程”已经在这里的其他帖子中回答过了。我将要发布的是一种不太为人所知的使用存储过程的方法。它对存储过程进行分组或对存储过程进行编号。

语法参考

; 按此编号

可选整数,用于对同名过程进行分组。可以使用一个DROP PROCEDURE语句将这些分组的过程放到一起

例子

CREATE Procedure FirstTest 
(
    @InputA INT
)
AS 
BEGIN
    SELECT 'A' + CONVERT(VARCHAR(10),@InputA)
END
GO

CREATE Procedure FirstTest;2
(
    @InputA INT,
    @InputB INT
)
AS 
BEGIN
    SELECT 'A' + CONVERT(VARCHAR(10),@InputA)+ CONVERT(VARCHAR(10),@InputB)
END
GO

Use

exec FirstTest 10
exec FirstTest;2 20,30

结果

另一个尝试

CREATE Procedure SecondTest;2
(
     @InputA INT,
    @InputB INT
)
AS 
BEGIN
    SELECT 'A' + CONVERT(VARCHAR(10),@InputA)+ CONVERT(VARCHAR(10),@InputB)
END
GO

结果

Msg 2730,级别11,状态1,程序SecondTest,行1[批量启动行3] 无法创建组号为2的过程“SecondTest”,因为数据库中当前不存在同名且组号为1的过程。 必须先执行CREATE PROCEDURE 'SecondTest';

引用:

使用数字语法创建过程 在SQL Server中编号的存储过程 对存储过程进行分组- sqlmag .

谨慎

对过程进行分组后,就不能单独删除它们了。 此功能可能会在Microsoft SQL Server的未来版本中删除。

存储过程是一组预编译的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)。

存储过程主要用于在数据库上执行某些任务。例如

从数据上的一些业务逻辑获取数据库结果集。 在一次调用中执行多个数据库操作。 用于将数据从一个表迁移到另一个表。 可用于其他编程语言,如Java。

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

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

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