How to send data from one table to another with the Deleted command

Asked

Viewed 415 times

4

Guys I’m creating this trigger in the SQL Server, but I’m not getting the id to send the data to another table and delete from this table.

CREATE TRIGGER MoveComprador
ON comprador
INSTEAD OF DELETE
AS
BEGIN
  SELECT * INTO bk_comprador FROM comprador WHERE comprador.codigo = deleted;
END
GO
  • 1

    Hello. 
 See this answer can help you: http://stackoverflow.com/a/14019020/2076784

1 answer

2

You need to set the "affected" column in the DELETE you want to get.

In this case, use the instruction "Deleted.Suacoluna".

Below is a T-SQL script for you to adapt your need:

CREATE TRIGGER MoveComprador
ON comprador
INSTEAD OF DELETE
AS
BEGIN
  DECLARE @ID  int
  SELECT @ID = deleted.id FROM deleted;

  INSERT INTO TB_LOG (CD_REGISTRO, NM_ACAO) VALUES (@ID, 'EXCLUIDO');

END

GO

For more information see:

https://msdn.microsoft.com/pt-br/library/ms189799.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.