title: Trigger - insert into @table select from inserted and execute a procedure if @@rowcount > 0 from @table
body: <p>I have a trigger on TableA on after insert, update.  On insert the information is inserted in a table variable when the condition is right, if something is recovered from the select, a procedure is executed.  </p>
  
  <p>If <code>@ds</code> is null, the procedure is not executed. Any suggestions?</p>
  
  <p>I tried this, but it didn't work:</p>
  
  <pre><code>....
  SET @dif = (SELECT ISNULL(COUNT(*), 0) FROM @ds); 
  
  IF @dif &gt; 0...
  </code></pre>
  
  <p>Thanks!</p>
  
  <pre><code>CREATE TRIGGER t_tablaA
  ON tablaA
  AFTER INSERT, UPDATE
  AS
  BEGIN
      DECLARE @ds TABLE (d VARCHAR(50));
      @who VARCHAR(MAX);
  
      INSERT INTO @ds
          SELECT p.email
          FROM inserted
          INNER JOIN personel p ON inserted.user = p.user
  
      IF @@rowcount &gt; 0
         SELECT @who = COALESCE(@who , '')  + d + '; ' 
         FROM @ds
  
      EXECUTE sendEmailTo(@who);
  END;
  </code></pre>