I want to compare the data between two tables (article1) and (article2). Remember both the tables have the same column name. I want to insert a difference data into a differ table. Please check the screenshots. I am troubling in writing SQL queries for generating difference data.Here is the SQL query. id field is a primary key.
CREATE DATABASE dbtest01
GO
USE dbtest01
GO
CREATE TABLE [dbo].[article1] ([id] [nchar](10) NOT NULL, [type] [nchar](10) NULL, [cost] [nchar](10) NULL,[name] [varchar](500) NULL,
CONSTRAINT [PK_article1] PRIMARY KEY CLUSTERED
(
[id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO [dbo].[article1]
VALUES ('001', '1', '40','Rahul'),
('002', '2', '80','abc'),
('003', '3', '120','John'),
('004', '40', '160','Mary')
GO
USE dbtest01
GO
CREATE TABLE [dbo].[article2] ([id] [nchar](10) NOT NULL, [type] [nchar](10) NULL, [cost] [nchar](10) NULL,[name] [varchar](500) NULL,
CONSTRAINT [PK_article2] PRIMARY KEY CLUSTERED
(
[id] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT INTO [dbo].[article2]
VALUES ('001', '1', '40','Rahul'),
('002', '2', '80','abc'),
('003', '3', '120','Jo'),
('004', '4', '160','Marie')
GO
CREATE TABLE [dbo].[differ](
[id] [nchar](10) NOT NULL,
[columnname] [nchar](10) NOT NULL,
[valuearticle1] [varchar](1000) NOT NULL,
[valuearticle2] [varchar](1000) NOT NULL
) ON [PRIMARY]
GO
Can anyone help me to write sql queries to generate differ table? Differ table contains data only for those columns where value has been updated or changed.