title: Why triggers try to insert NULL value when using a field from 'inserted' table?
body: <p>I have to sync changes done in MSSQL with a remote MySQL database. The changes to be synced are adding invoices and users to the system. The remote server is not expected to be always reachable so I'm trying to set up a kind of log table for storing changes done in MSSQL.</p>
  
  <p>Here is a fully working trigger for that:</p>
  
  <pre><code>CREATE TRIGGER [dbo].[dokument_insert]
     ON [dbo].[dokument]
     AFTER INSERT
  AS 
  BEGIN
      SET NOCOUNT ON;
      INSERT INTO [bcg_ekodu].[dbo].[sync_stack] (event,sql, table_name, import_priority)
          SELECT 
              'INSERT',
              'INSERT INTO bills SET
                  date = "'+CONVERT(VARCHAR(19),dok_kuup,120)+'", 
                  total = "'+CAST(kokkusum AS nvarchar)+'",
                  number = "'+RTRIM(dok_nr)+'", 
                  created = "'+CONVERT(VARCHAR(19),savetime,120)+'", 
                  rounded = "'+CAST(ymardus AS nvarchar)+'",
                  currency = "'+CAST(valuuta AS nvarchar)+'", 
                  due_date = "'+CONVERT(VARCHAR(19),tasupaev,120)+'", 
                  pk_joosep = "'+CAST(dok_kood AS nvarchar)+'",
                  joosep_hankija = "'+CAST(hankija AS nvarchar)+'";
               UPDATE
                  bills, users, companies 
               SET 
                  bills.user_id = users.id,
                  bills.imported = NOW()
               WHERE
                  bills.imported IS NULL
                  AND companies.id = users.company_id
                  AND companies.pk_joosep = 10
                  AND bills.user_id = users.pk_joosep',
              'bills',
              '200'
          FROM inserted
  END
  </code></pre>
  
  <p>It inserts a row into 'sync_stack' table every time a row is inserted to 'dokument' table. The 'sql' column will contain an SQL to create the same kind of row in another (MySQL) database.</p>
  
  <p>But this trigger is not working:</p>
  
  <pre><code>CREATE TRIGGER [dbo].[klient_insert]
     ON [dbo].[klient]
     AFTER INSERT
  AS 
  BEGIN
      SET NOCOUNT ON;
      INSERT INTO [bcg_ekodu].[dbo].[sync_stack] (event,sql, table_name, import_priority)
          SELECT 
              'INSERT',
              'INSERT INTO users SET
                  username =10'+CAST(kl_kood as nvarchar)+',
                  password = NULL,
                  name ="'+LTRIM(RTRIM(kl_nimi))+'",  
                  email ="'+CAST(LTRIM(RTRIM(kl_email)) as nvarchar)+'",  
                  reference_no ="'+CAST(LTRIM(RTRIM(kl_viide)) as nvarchar)+'",   
                  phone ="'+CAST(LTRIM(RTRIM(kl_tel1)) as nvarchar)+'",   
                  logins ="'+CAST(0 as nvarchar)+'",  
                  last_login = NULL,  
                  created ="'+CONVERT(VARCHAR(19),savetime,120)+'",   
                  updated = NULL, 
                  deleted ="0",   
                  address ="'+CAST(LTRIM(RTRIM(kl_aadr1)) as nvarchar)+'",    
                  pk_joosep ="'+CAST(kl_kood as nvarchar)+'"',
              'users',
              '210'
          FROM inserted
  END
  </code></pre>
  
  <p>While the execution of the above SQL to create that trigger completes just fine, when I try to insert some rows to the 'triggered' table, I get the following error:</p>
  
  <pre><code>No row was updated.
  
  The data in row 175 was not committed.
  Error Source: .Net SqlClient Data Provider.
  Error Message: Cannot insert the value NULL into column 'sql', table 'mydb.dbo.sync_stack'; column does not allow nulls. INSERT fails.
  
  The statement has been terminated.
  
  Correct the errors and retry or press ESC to cancel the change(s).
  </code></pre>
  
  <ul>
  <li>If I delete this trigger, this error does not occur.</li>
  <li>If I insert just plain text for 'sql' column, it works as expected. </li>
  <li>If I use any field from the inserted row, even just a text field, it fails again.</li>
  <li>If I allow NULL values in 'sql' column, inserting rows succeeds but I get a NULL value in 'sql' column.</li>
  </ul>
  
  <p>How to make the second trigger work as expected, too?</p>