Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, assuming I have a table called tabpeople and it has the fields: id (primary key auto increment), number (text) and name (text). How can I create a trigger that always after adding a person, the number field is updated to the same value as the id field with 5 leading zeros?

Thank you all.

What I have tried:

I tried to adapt some examples that I found but I could not do that.
Posted
Updated 24-Jun-18 20:46pm

1 solution

Pleas try:
SQL
CREATE TRIGGER tabpeople_i 
   ON  tabpeople
   FOR  INSERT
AS 
BEGIN
SET NOCOUNT ON;
UPDATE t
	SET name='00000'+LTRIM(rtrim(str(i.id)))
	FROM tabpeople t
	JOIN inserted I 
		ON i.id=t.id

END
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900