2

我有一个名为'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
4

4 回答 4

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
于 2013-02-13T06:24:08.507 回答
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
于 2013-02-13T06:25:23.730 回答
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
于 2013-02-13T06:41:48.307 回答
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 中拥有所有操作代码(当然我可以使用三个),并且还可以在此过程中添加日志记录功能和其他基本逻辑。

我并不是说这仍然是最好的方法,但它应该显示基本逻辑。

于 2013-02-13T07:28:10.283 回答