我有一个名为'tasks'的表,'task id'
是标识列,对于那个表我必须编写保存存储过程,其中当没有给出'task id'时它应该插入值,当给出'task id'它应该更新表。
当任务ID是标识列时,如何通过示例解释任何人都可以实现这一点。 这是代码
Alter PROCEDURE TaskSave
(
@taskid int,
@ProjectId int,
@EmployeeId int,
@TaskName nvarchar(50),
@Duration_Hrs int,
@StartDate nvarchar(20),
@FinishDate nvarchar(20),
@CreateUserId int,
@CreatedDate nvarchar(20),
@ModifiedUserID int,
@ModifiedDate nvarchar(20),
@Is_CommonTask bit
) AS
BEGIN
IF Exists( select null from TblTasks where TaskId=@TaskId)
BEGIN
INSERT TblTasks
VALUES (@ProjectId,@EmployeeId,@TaskName,@Duration_Hrs,
@StartDate,@FinishDate,@CreateUserId,@CreatedDate,
@ModifiedUserID,@ModifiedDate,@Is_CommonTask)
END
ELSE
BEGIN
UPDATE TblTasks SET
StartDate=@StartDate,FinishDate=@FinishDate,
Duration_Hrs=@Duration_Hrs
WHERE TaskId=@TaskId
END
END
GO
答案 0 :(得分:0)
您已关闭,请检查记录是否不存在并执行插入,否则更新。您还可以将@TaskId
参数声明为OUTPUT
,并在插入时使用SCOPE_IDENTITY()
函数返回:
ALTER PROCEDURE TaskSave(
@TaskId INT = NULL OUTPUT
, @ProjectId INT
, @EmployeeId INT
, @TaskName NVARCHAR(50)
, @Duration_Hrs INT
, @StartDate NVARCHAR(20)
, @FinishDate NVARCHAR(20)
, @CreateUserId INT
, @CreatedDate NVARCHAR(20)
, @ModifiedUserID INT
, @ModifiedDate NVARCHAR(20)
, @Is_CommonTask BIT
)
AS
BEGIN
IF NOT(EXISTS(SELECT * FROM TblTasks WHERE TaskId = @TaskId))
BEGIN
INSERT INTO TblTasks(
ProjectId
, EmployeeId
, TaskName
, Duration_Hrs
, StartDate
, FinishDate
, CreateUserId
, CreatedDate
, ModifiedUserID
, ModifiedDate
, Is_CommonTask
)
VALUES(
@ProjectId
, @EmployeeId
, @TaskName
, @Duration_Hrs
, @StartDate
, @FinishDate
, @CreateUserId
, @CreatedDate
, @ModifiedUserID
, @ModifiedDate
, @Is_CommonTask
)
SET @TaskId = SCOPE_IDENTITY()
END
ELSE
BEGIN
UPDATE TblTasks SET
StartDate = @StartDate
, FinishDate = @FinishDate
, Duration_Hrs = @Duration_Hrs
WHERE TaskId=@TaskId
END
END
GO
答案 1 :(得分:0)
首先,给你的输入变量TaskID一个默认值,如下所示,然后只需检查变量是否为NULL,如果是,则插入一个新行
Alter PROCEDURE TaskSave
(
@taskid int = NULL,
@ProjectId int,
@EmployeeId int,
@TaskName nvarchar(50),
@Duration_Hrs int,
@StartDate nvarchar(20),
@FinishDate nvarchar(20),
@CreateUserId int,
@CreatedDate nvarchar(20),
@ModifiedUserID int,
@ModifiedDate nvarchar(20),
@Is_CommonTask bit
) AS
BEGIN
IF @taskid IS NULL
BEGIN
INSERT TblTasks
VALUES (@ProjectId,@EmployeeId,@TaskName,@Duration_Hrs,
@StartDate,@FinishDate,@CreateUserId,@CreatedDate,
@ModifiedUserID,@ModifiedDate,@Is_CommonTask)
END
ELSE
BEGIN
UPDATE TblTasks SET
StartDate=@StartDate,FinishDate=@FinishDate,
Duration_Hrs=@Duration_Hrs
WHERE TaskId=@TaskId
END
END
GO
答案 2 :(得分:0)
将@taskid
参数声明为NULL。如果它为null则插入else更新。见下文。
Alter PROCEDURE TaskSave
(
@taskid int =NULL,
@ProjectId int,
@EmployeeId int,
@TaskName nvarchar(50),
@Duration_Hrs int,
@StartDate nvarchar(20),
@FinishDate nvarchar(20),
@CreateUserId int,
@CreatedDate nvarchar(20),
@ModifiedUserID int,
@ModifiedDate nvarchar(20),
@Is_CommonTask bit
) AS
BEGIN
if @taskid is null
BEGIN
INSERT TblTasks
VALUES (@ProjectId,@EmployeeId,@TaskName,@Duration_Hrs,
@StartDate,@FinishDate,@CreateUserId,@CreatedDate,
@ModifiedUserID,@ModifiedDate,@Is_CommonTask)
END
ELSE
BEGIN
UPDATE TblTasks SET
StartDate=@StartDate,FinishDate=@FinishDate,
Duration_Hrs=@Duration_Hrs
WHERE TaskId=@taskid
END
END
GO
答案 3 :(得分:0)
在这些情况下,我曾经创建了一个能够插入,更新和删除项目的通用存储过程。一般模式看起来像这样
create procedure Modify_MyTable
@Action char(1),
@Data int,
@PK int output
as
if @Action = 'I' begin -- Insert
insert into MyTable (Data) values (@Data)
set @PK = Scope_Identity()
end
if @Action = 'M' begin -- Modify
update MyTable set Data = @Data where PKID = @PK
end
if @Action = 'D' begin -- Delete
delete MyTable where PKID = @PK
end
很久以前,当我使用它时,我发现它非常方便,因为我在一个SP中拥有所有操作代码(当然我可以使用三个)并且还可以添加日志记录功能和其他基本逻辑这个程序。
我并不是说这仍然是最好的方法,但它应该显示基本逻辑。