title: Insert and update multiple records via same stored procedure
body: <p>I created this stored procedure to go through all the records in the table comparing the id (primary key) if exists and the records changed, make the necessary changes &amp; update the record.</p>
  
  <p>If the id is not in the table then insert the record. This stored procedure
  compiles fine, but doesn't seem to work properly. Does this need a while loop?</p>
  
  <pre><code>ALTER PROCEDURE [dbo].[SMLineUpdate]
  (
    @id [int],
    @Payroll_Id [int],
    @ProductCode nvarchar(255),
    @Description nvarchar (255),
    @Qty nvarchar(255)
  )
  AS
  IF EXISTS (SELECT Id from Smline where @id = Id) BEGIN 
    update dbo.SmLine
      Set [Payroll_Id] = @Payroll_Id
      , ProductCode = @ProductCode
      , Description = @Description
      , Qty = @Qty
  END ELSE BEGIN 
    INSERT INTO SmLine ([Payroll_Id], [ProductCode], [Description], [Qty])
      VALUES (@Payroll_Id, @ProductCode, @Description, @Qty)
  END
  </code></pre>