gwydion93
gwydion93

Reputation: 1923

How to insert into a table where a column has a specified value

I am probably overthinking this, but how would you insert a value into a column for a table where another specified column is equal to a specified value? Something is not working with the below.

CREATE PROCEDURE [dbo].[bcasp_InsertDate]
@TicketNum nvarchar(250),
@DateFinal datetime

AS
BEGIN
SET NOCOUNT ON;
SELECT DateFinalEmailSent FROM T_Ticket WHERE TicketNumber = @TicketNumber
INSERT INTO T_Table(DateFinalEmailSent) VALUES (@DateFinal)
END

Upvotes: 1

Views: 43

Answers (2)

Zee786
Zee786

Reputation: 93

You need to make the below change in your code in order to make it work.

SELECT @DateFinal= DateFinalEmailSent FROM T_Ticket WHERE TicketNumber = @TicketNumber

Upvotes: 1

Eric Brandt
Eric Brandt

Reputation: 8101

The code as written isn't working because you're not assigning a value to @DateFinal in your first SELECT statement, but that statement, and the accompanying variable, are really unnecessary.

Why not just a straight INSERT?

INSERT INTO T_Table
(
  DateFinalEmailSent
)
SELECT 
  DateFinalEmailSent 
FROM 
  T_Ticket 
WHERE 
  TicketNumber = @TicketNumber;

Upvotes: 4

Related Questions