我有兩個沒有關鍵的表和我需要連接所有列比需要檢查三個條件,因爲表中缺少關鍵列。兩個表中沒有關鍵列的區別
- 如果在另一本記錄沒有任何變化,然後在自定義列「無變化」
- 如果記錄存在於表「老」,但不存在於表新則在自定義的列「記錄刪除」
- 如果出現在表中記錄的「新」,但不存在於表老然後在自定義列「新紀錄」
Declare @old table
(
BaseVehicle VARCHAR (50),
BaseVehicleID VARCHAR (50),
Years VARCHAR (50),
Make VARCHAR (50),
MakeID VARCHAR (50)
)
INSERT @old
SELECT '2002 Freightliner FL60', '127640', '2002', 'Freightliner', '497' UNION ALL
SELECT '2002 Freightliner FL70', '127641', '2002', 'Freightliner', '497' UNION ALL
SELECT '2014 Check', '127979', '2014', 'BMW', '31' UNION ALL-- Record Deleted
SELECT '2003 Freightliner FL50', '127655', '2003', 'Freightliner', '497' UNION ALL
SELECT '2003 check', 'test', '2003', 'Freightliner', '497' UNION ALL-- Record Deleted
SELECT '2003 Freightliner FL70', '127657', '2003', 'Freightliner', '497'
Declare @new table
(
BaseVehicle VARCHAR (50),
BaseVehicleID VARCHAR (50),
Years VARCHAR (50),
Make VARCHAR (50),
MakeID VARCHAR (50)
)
INSERT @new
SELECT '2014 Jeep Cherokee', '127768', '2014', 'Jeep', '42' UNION ALL-- New Records
SELECT '2011 Ford E-250' , '96774', '2011', 'Ford', '54' UNION ALL-- New Records
SELECT '2014 BMW Alpina B7L', '127979', '2014', 'BMW', '31' UNION ALL-- New Records
SELECT '2002 Freightliner FL60', '127640', '2002', 'Freightliner', '497' UNION ALL -- No Changes
SELECT '2002 Freightliner FL70', '127641', '2002', 'Freightliner', '497' UNION ALL-- No Changes
SELECT '2003 Freightliner FL50', '127655', '2003', 'Freightliner', '497' UNION ALL-- No Changes
SELECT '2003 Freightliner FL70', '127657', '2003', 'Freightliner', '497' -- No Changes
和輸出應該像
BaseVehicle, BaseVehicleID, Years, Make, MakeID ,Message
2014 Check, 127979, 2014, BMW, 31, Deleted Row
2003 check, test, 2003, Freightliner, 497, Deleted Row
'2014 Jeep Cherokee', '127768','2014','Jeep', '42' , New Records
'2011 Ford E-250' , '96774','2011','Ford', '54' , New Records
'2014 BMW Alpina B7L', '127979', '2014','BMW', '31', New Records
'2002 Freightliner FL60', '127640','2002','Freightliner', '497' , No Changes
'2002 Freightliner FL70', '127641','2002','Freightliner', '497', No Changes
'2003 Freightliner FL50', '127655','2003','Freightliner', '497' , No Changes
'2003 Freightliner FL70', '127657','2003','Freightliner', '497' , No Changes
我使用的SQL服務器2008 – Shan
代替concat使用SET操作..intersect,minus等 –