Share via

Update Trigger

SPWGUT 121 Reputation points
2022-04-15T08:52:29.977+00:00

Hello team.
I would like to ask a question, after writing a trigger, if all records are updated every time they are inserted.
My opinion is
CREATE TRIGGER trigger
ON SP_SW
AFTER INSERT
AS
UPDATE SP_SW
SET SP_SW = Id
Can someone help me?
Thanks in advance.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Bert Zhou-msft 3,521 Reputation points
2022-04-15T09:02:50.113+00:00

Hi,@SPWGUT

Welcome to Microsoft T-SQL Q&A Forum!

Your code will update all columns equal to ID. If you want to update the specified column without affecting other columns, it is recommended to add a where condition directly:
Please try it:

CREATE TRIGGER trigger  
ON SP_SW  
AFTER INSERT  
AS  
update SP_SW  
set table_column =SP_SW.id  
from inserted   
where id=inserted.id  

As a reminder, please check whether your table has fields with the same name as the table. This is not very good. It is recommended to add a special symbol to mark it.For the use of triggers, you'd better refer to the official documentation.

Best regards,
Bert Zhou


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.