Sometimes we need to compare tables and/or data to know what was changed.
Solution:
Script for create two table and insert value in both table:
- USE Learn
- GO
- CREATE TABLE [dbo].[product1](
- [id] [nchar](10) NOT NULL,
- [type] [nvarchar](10) NULL,
- [cost] [nchar](10) NULL,
- CONSTRAINT [PK_product1] 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].[product1] values
- ('001','Book1','40'),
- ('002','Book1','80'),
- ('003','Book13','120')
- GO
- CREATE TABLE [dbo].[product2](
- [id] [nchar](10) NOT NULL,
- [type] [nvarchar](20) NULL,
- [cost] [nchar](10) NULL,
- CONSTRAINT [PK_product2] 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].[product2] values
- ('001','Book1','40'),
- ('002','Book12','80'),
- ('003','Book13','120'),
- ('004','Book14','160')
-
- select * from [product1]
-
- select * from [product2]
Following Query show difference between table:
select * from [product2]
except
select * from [product1]