I'm using SQL Server 2008 R2 and I have 2 tables Product
and Product_Master
. I want to update only the latest version of records in Product_Master
from Product
table leaving earlier versions as it is.
CREATE TABLE [dbo].[Product]
(
[ProdId] [nvarchar](50) NOT NULL,
[ProdDesc] [nvarchar](50) NULL,
[ProdPrice] [decimal](18, 0) NULL,
[Version] [int] NOT NULL
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Product_Master]
(
[ProdId] [nvarchar](50) NOT NULL,
[ProdDesc] [nvarchar](50) NULL,
[ProdPrice] [decimal](18, 0) NULL,
[Version] [int] NOT NULL
) ON [PRIMARY]
GO
INSERT INTO [dbo].[Product] ([ProdId], [ProdDesc], [ProdPrice], [Version])
VALUES (N'1001', N'Toys', CAST(2500 AS Decimal(18, 0)), 2),
(N'1002', N'Books', CAST(1800 AS Decimal(18, 0)), 3)
GO
INSERT INTO [dbo].[Product_Master] ([ProdId], [ProdDesc], [ProdPrice], [Version])
VALUES (N'1001', N'Toys', CAST(2500 AS Decimal(18, 0)), 2),
(N'1001', N'Toys', CAST(2000 AS Decimal(18, 0)), 1),
(N'1002', N'Perfumes', CAST(1500 AS Decimal(18, 0)), 1),
(N'1002', N'Perfumes', CAST(1500 AS Decimal(18, 0)), 2),
(N'1002', N'Perfumes', CAST(1800 AS Decimal(18, 0)), 3)
GO
Image was attached for better understanding.