I am trying to update multiple records at once using a stored procedure, and if that record is not in the table, it has to insert this as new record. I got Id as the primary key on the table. Method I tried below is first inserting data in to a temp table and then updating the original table.I used a sample stored procedure from similar post to create this, as I am bit new to writing stored procedures, I am getting errors on the stored procedure. Please help
CREATE PROCEDURE [dbo].[xxxxxx]
DECLARE @Tbl TABLE(
Id [int] NOT NULL PRIMARY KEY,
Payroll_Id [int],
ProductCode nvarchar(255),
Description nvarchar (255),
Qty nvarchar(255))
BEGIN
INSERT INTO @Tb1 ([Payroll_Id],[ProductCode],[Description],[Qty])
Select @Paroll_Id as [Paroll_Id],@ProductCode as [ProductCode],@Description as [Description], @Qty as [Qty]
Update tps
Set [Payroll_Id]= tmp.Payroll_Id
,[ProductCode]= tmp.ProductCode
,[Description] = tmp.Description
,[Qty] = tmp.Qty
FROM dbo.SmLine tps
INNER JOIN @Tb1 tmp on tmp.Id= tps.Id
INSERT INTO SMLine ([Payroll_Id],[ProductCode],[Description],[Qty])
Select
tmp.Payroll_Id,tmp.ProductCode,tmp.Description,tmp.Qty
From @Tb1 tmp
LEFT JOIN dbo.SMLine tps ON tps.Id = tmp.Id
WHERE dbo.SmLine IS NULL
END