SQL Insert Into Value包含条件

时间:2023-01-19 15:43:15

I'm trying to write a stored procedure to modify a session in my Sessions table. I need to be able to insert values into a specified row i.e. with a condition included although I'm not sure how.

我正在尝试编写一个存储过程来修改Sessions表中的会话。我需要能够将值插入指定的行,即包含条件但我不确定如何。

Here is my code (I'm aware that I cannot do INSERT INTO > VALUES > WHERE but I'm trying to give you an idea of what I want to do).

这是我的代码(我知道我不能执行INSERT INTO> VALUES> WHERE,但我试图让你知道我想做什么)。

CREATE PROCEDURE [dbo].[TT_Modify_Session]
    @SessionName NVARCHAR(50),
    @TrainingName NVARCHAR(100),
    @Trainee NVARCHAR(20),
    @TrainingDate DATE,
    @SessionID INT
AS
    SET NOCOUNT ON;

    BEGIN TRY
    BEGIN TRAN
        INSERT INTO dbo.TT_Sessions (SessionName, Trainee, TrainingDate, TrainingName)
        VALUES @SessionName, @Trainee, @TrainingDate, @TrainingName
        WHERE @SessionID = [SessionID] 

        COMMIT
    END TRY
    BEGIN CATCH
        ROLLBACK
        PRINT ERROR_MESSAGE()
    END CATCH

    RETURN @sessionID

Any help is greatly appreciated!

任何帮助是极大的赞赏!

1 个解决方案

#1


1  

You describe code to "modify" values that already exist in the table. That's an UPDATE...

您将描述代码以“修改”表中已存在的值。这是一个更新......

(INSERT adds a new row to a table, and leaves all pre-existing rows as they were...)

(INSERT向表中添加一个新行,并保留所有预先存在的行...)

UPDATE
  dbo.TT_Sessions
SET
  SessionName  = @SessionName,
  Trainee      = @Trainee,
  TrainingDate = @TrainingDate,
  TrainingName = @TrainingName
WHERE
  SessionID = @SessionID

#1


1  

You describe code to "modify" values that already exist in the table. That's an UPDATE...

您将描述代码以“修改”表中已存在的值。这是一个更新......

(INSERT adds a new row to a table, and leaves all pre-existing rows as they were...)

(INSERT向表中添加一个新行,并保留所有预先存在的行...)

UPDATE
  dbo.TT_Sessions
SET
  SessionName  = @SessionName,
  Trainee      = @Trainee,
  TrainingDate = @TrainingDate,
  TrainingName = @TrainingName
WHERE
  SessionID = @SessionID