title: Create trigger to Audit data changes SQL Server
body: <p>Good night folks,</p>
  
  <p>I need to create a stored Procedure that giving the name of a table by parameter create a trigger to Audit data changes in the table.</p>
  
  <p>I need to do something very similar to the code of this article, but for performance, reasons I need to have a Form in my program that allows me to choose the tables and the main fields to audit.</p>
  
  <p><a href="http://weblogs.asp.net/jongalloway/adding-simple-trigger-based-auditing-to-your-sql-server-database" rel="nofollow">http://weblogs.asp.net/jongalloway/adding-simple-trigger-based-auditing-to-your-sql-server-database</a></p>
  
  <p>I have tried changing the code in various ways with cursors etc unsuccessful because I have not much experience with T-SQL.</p>
  
  <p>My main doubt is how to give the table name and the fields that I want (which can be 2 or 3 or more) by parameter in my stored procedure.</p>
  
  <pre><code>Select field = 0,maxfield = max (ORDINAL_POSITION) 
  from INFORMATION_SCHEMA.COLUMNS 
  where TABLE_NAME =tablename 
  
  while field &lt;maxfield 
  begin 
      select field = min (ORDINAL_POSITION) 
      from INFORMATION_SCHEMA.COLUMNS 
      where TABLE_NAME =tablename 
        and ORDINAL_POSITION&gt; @ field 
  
      select bit = (field - 1)% 8 + 1 =bit select power (2, @ bit - 1) 
      select char = ((field - 1) / 8) + 1 if substring (COLUMNS_UPDATED () , @ char, 1) &amp;bit&gt; 0 or type in ('I', 'D')
  </code></pre>
  
  <p>I need something like:</p>
  
  <pre><code>select * 
  from INFORMATION_SCHEMA.COLUMNS 
  where TABLE_NAME = tablename 
    and COLUMN_NAME = 'My FIELD'
  </code></pre>
  
  <p>I will be very grateful if you can help me, because I am trying to change the code almost for two days without much success.</p>