SQL Trigger to derive a custom number and then insert that derived number into another table.
My question is: How to derive a number one table1 and to insert it into another table. This number is also an auto increment number. My trigger is: create trigger insertQNO on NN after insert as begin update NN set QNumber ='Q150' + CAST(
inserted.ID AS VARCHAR(4)) + '/' + RIGHT(CONVERT(VARCHAR(10),GETDATE(),103),7) from NN join inserted on
nn.ID; -- go /* Commented out the line, go does not belong here */ end go My result puts the number into Table NN. if i include the second table as follows: alter trigger insertQNO on NN after insert as begin update NN set QNumber ='Q150' + CAST(
inserted.Id AS VARCHAR(4)) + '/' + RIGHT(CONVERT(VARCHAR(10),GETDATE(),103),7) from NN join inserted on
NN.ID; SET IDENTITY_INSERT dbo.table2 ON; insert into Table2 (ID,Qnumber) (select ID, Qnumber from NN) end; go It does the right thing by duplicating the value into the two tables. But now, i want the trigger to fetch the number from table two, and insert the value into Table 3 (this table has different columns) and update table 2 with the next derived no? I have no idea how to do the last step. Any help would be gladly appreciated. I am using SQL Server 2014