I am newbie in SQL Server.
I have Table A
with columns id
, name
, delete
(boolean).
I have another Table B
which has the same columns as A.
Once in Table A delete
flag becomes 1 (programmatically), I need to insert that row into Table B and remove it from Table A.
So below get the results
- Table A: Has records with delete -> 0
- Table B: Has records with delete -> 1
Is it possible to achieve this via triggers?
Referring this also : https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql?view=sql-server-ver16
1条答案
按热度按时间bprjcwpo1#
In your case, you can create an AFTER DELETE trigger on Table A that inserts the deleted row into Table B and removes it from Table A. Here's an example of how you can create such a trigger:
In this trigger, the
deleted
pseudo-table contains the rows deleted from Table A. You can use it to select the necessary columns and insert them into Table B. Then, you can perform a delete operation on Table A using a join with thedeleted
table to remove the corresponding rows.