title: while loop inside a trigger to loop through all the columns of table in sql
body: <p>I have a trigger like below on user table to insert into the audit table with which column was updated and previous value:</p>
  
  <pre><code>ALTER TRIGGER [dbo].[trgAfterUpdate] ON [dbo].[tbl_User]
  AFTER UPDATE
  AS
  
  
      declare @fieldname varchar(128) ;
      declare @OldValue varchar(255);
      declare @CreateUser varchar(100) ;
      declare @User_Key int;
  
      select @CreateUser =i.user_name from deleted i; 
      SELECT @User_Key = i.user_key from inserted i;  
  
      if update(user_name)
        begin
            select @OldValue=j.user_name from deleted j;  
            set @fieldname = 'user_name';
  
              insert into tbl_Audit(user_key, field_name, previuos_Value, user_name)
              values(@User_Key ,@fieldname,@OldValue, @CreateUser);
  
        end
  </code></pre>
  
  <p>But my questions is I have like 100 fields on my table. I can't write 100 if conditions. And i need a suggestion how to use while loop in it, and how is it going to effect the performance.</p>
  
  <p>Thanks</p>