我通常使用以下方式编写存储过程:
ALTER PROCEDURE MasterInsertUpdateDelete
(@id INTEGER,
@first_name VARCHAR(10),
@last_name VARCHAR(10),
@salary DECIMAL(10,2),
@city VARCHAR(20),
@StatementType NVARCHAR(20) = '')
AS
BEGIN
IF @StatementType = 'Insert'
BEGIN
INSERT INTO employee (id, first_name, last_name, salary, city)
VALUES (@id, @first_name, @last_name, @salary, @city)
END
IF @StatementType = 'Select'
BEGIN
SELECT * FROM employee
END
IF @StatementType = 'Update'
BEGIN
UPDATE employee
SET First_name = @first_name,
Last_name = @last_name,
salary = @salary,
city = @city
WHERE id = @id
END
ELSE IF @StatementType = 'Delete'
BEGIN
DELETE FROM employee
WHERE id = @id
END
END
第二种方法是将一个存储过程分为4个单独的存储过程,以用于插入,删除,选择和更新表。
就性能而言,推荐哪个?
答案 0 :(得分:2)
您是在问编写一个可以执行四项操作的存储过程还是单独执行一项操作的存储过程是更好的选择。
起初,我认为这是基于观点的,即将结束问题。可能是基于意见的,但有关四个存储过程的证据似乎不堪设想。
我想不出将这样的不同功能组合到一个过程中的好方法。
我还要补充一点,除非您正在审核,验证数据或使用存储过程来控制访问,否则将基本SQL函数包装到存储过程中不会带来太多好处。我没有看到SELECT
语句的好处。
答案 1 :(得分:0)
我认为,如果每个操作都在单独的存储过程中完成,那会更好,因为它使管理和更新存储过程更加容易。
MasterInsert
CREATE PROCEDURE MasterInsert
(@first_name VARCHAR(10),
@last_name VARCHAR(10),
@salary DECIMAL(10, 2),
@city VARCHAR(20))
AS
BEGIN
INSERT INTO employee (id, first_name, last_name, salary, city)
VALUES (@id, @first_name, @last_name, @salary, @city)
END
MasterSelect
CREATE PROCEDURE MasterSelect
AS
BEGIN
SELECT *
FROM employee
END
MasterUpdate
CREATE PROCEDURE MasterUpdate
(@id INTEGER,
@first_name VARCHAR(10) = NULL,
@last_name VARCHAR(10) = NULL,
@salary DECIMAL(10, 2) = NULL,
@city VARCHAR(20) = NULL, )
AS
BEGIN
UPDATE employee
SET First_name = @first_name,
last_name = @last_name,
salary = @salary,
city = @city
WHERE id = @id
END
MasterDelete
CREATE PROCEDURE MasterDelete
(@id INTEGER)
AS
BEGIN
DELETE
FROM employee
WHERE id = @id
END
MasterInsertUpdateDelete
CREATE PROCEDURE MasterInsertUpdateDelete
(@id INTEGER = NULL,
@first_name VARCHAR(10) = NULL,
@last_name VARCHAR(10) = NULL,
@salary DECIMAL(10, 2) = NULL,
@city VARCHAR(20) = NULL,
@StatementType NVARCHAR(20) = '')
AS
BEGIN
IF @StatementType = 'Insert'
BEGIN
EXEC MasterInsert @first_name,
@last_name,
@salary,
@city
END
ELSE IF @StatementType = 'Select'
BEGIN
EXEC MasterSelect
END
ELSE IF @StatementType = 'Update'
BEGIN
EXEC MasterUpdate @id,
@first_name,
@last_name,
@salary,
@city
END
ELSE IF @StatementType = 'Delete'
BEGIN
EXEC MasterDelete @id
END
END
答案 2 :(得分:0)
根据我的观点,编写存储过程如下。 对不需要分开的每个部分写评论。 还提到了诸如谁创建和谁修改了存储过程以及更改原因之类的细节。这可能会在将来帮助您跟踪信息。在编写“开始”和“结束”时,请始终使用制表符代替空格。
-- =============================================
-- Created By: Author Name
-- Create Date: Create Date
-- Modify By with Reason: Name of Person who modified the Store Procedure, Description
-- =============================================
-- Drop Proc MasterInsertUpdateDelete
-- Execute MasterInsertUpdateDelete 1,'First Name','Last Name',22500.15,'Banglore','Type'
Create PROCEDURE MasterInsertUpdateDelete
(
@id INTEGER,
@first_name VARCHAR(10),
@last_name VARCHAR(10),
@salary DECIMAL(10,2),
@city VARCHAR(20),
@StatementType nvarchar(20) = ''
)
AS
BEGIN
-- Insert Employee
IF @StatementType = 'Insert'
BEGIN
insert into employee (id,first_name,last_name,salary,city) values( @id, @first_name, @last_name,
@salary, @city)
END
-- Select Employee
IF @StatementType = 'Select'
BEGIN
select * from employee
END
-- Update Employee
IF @StatementType = 'Update'
BEGIN
UPDATE employee SET
First_name = @first_name, last_name = @last_name, salary = @salary,
city = @city
WHERE id = @id
END
-- Delete Employee
ELSE IF @StatementType = 'Delete'
BEGIN
DELETE FROM employee WHERE id = @id
END
END