2013-02-13 81 views
2

我有一個名爲'tasks'的表,其中'task id'是identity列,對於那個表我必須寫保存存儲過程,其中當'task id'沒有給出時它應該插入值,當給出'任務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 [email protected]) 
    BEGIN 
     INSERT TblTasks 
     VALUES (@ProjectId,@EmployeeId,@TaskName,@Duration_Hrs, 
       @StartDate,@FinishDate,@CreateUserId,@CreatedDate, 
       @ModifiedUserID,@ModifiedDate,@Is_CommonTask) 
    END 
    ELSE 
    BEGIN 
     UPDATE TblTasks SET 
     [email protected],[email protected], 
     [email protected]_Hrs 
     WHERE [email protected] 
    END 
END 
GO 
+0

你的問題是什麼?如果它只是測試'@ TaskID'變量,我建議將IF語句改爲'IF @TaskID = -1'(或其他一些值,它將用作「未給定」用戶的值。你想在插入後知道什麼是任務ID,然後使用'@@ IDENTITY'。如果你還想返回'@ TaskID',使參數OUTPUT – cha 2013-02-13 06:21:19

回答

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 
     [email protected],[email protected], 
     [email protected]_Hrs 
     WHERE [email protected] 
    END 


END 
GO 
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 [email protected] 
    END 
END 
GO 
0

聲明@taskid參數爲NULL。如果它爲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 
     [email protected],[email protected], 
     [email protected]_Hrs 
     WHERE [email protected] 
    END 
END 
GO 
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所有的操作代碼(我可以使用了三個,當然),它很方便,也可以在此過程中添加日誌記錄功能和其他基本邏輯。

我不是說這仍然是最好的方法,但它應該顯示基本邏輯。