title: SQL Server trigger double values at first insert
body: <p>I created a trigger on <code>Ames.Details</code> to fire <code>Ames.Sqlclass</code> but when I insert the data into <code>Ames.Details</code>, for example ‘Ken’, ’Math’, 20, ’In’, it shows me 40 marks in <code>Sqlclass.Syntax</code>** below</p>
  
  <pre><code>CREATE SCHEMA Ames 
  GO
  
  CREATE TABLE Ames.SqlClass
  (
      Name NVARCHAR(50),
      Subject NVARCHAR(50),
      Mark INT
  )
  GO
  
  CREATE TABLE Ames.Details
  (
      Name NVARCHAR(50),
      Subject NVARCHAR(50),
      Mark INT,
      Status NVARCHAR(20) 
  )
  GO
  
  CREATE TRIGGER SQlDetails 
  ON Ames.Details 
  AFTER INSERT 
  AS
  BEGIN
      DECLARE @st INT, @Namesql NVARCHAR(50),
              @Subject NVARCHAR(50), @pt INT, @Status NVARCHAR(20)
  
      SELECT 
          @Namesql = i.Name, @Subject = i.subject,
          @pt = i.mark, @Status = I.Status 
      FROM
          inserted AS I
  
      IF NOT EXISTS (SELECT * FROM Ames.SqlClass  
                     WHERE @Namesql = Name AND @Subject = Subject)
      BEGIN
          INSERT INTO Ames.sqlClass (Name, Subject, Mark)
          VALUES (@Namesql, @Subject, @pt)
      END
  
      SELECT @st = Mark 
      FROM ames.SqlClass
      WHERE @Namesql = Name AND @Subject = Subject
  
      BEGIN
          IF @Status = 'IN'
          BEGIN
              SET @st = @st + @pt
          END
  
          IF @Status = 'Out' 
          BEGIN
              SET @st = @st - @pt
          END
  
          UPDATE Ames.SqlClass 
          SET Mark = @st 
          WHERE Name = @Namesql
      END
  END
  </code></pre>