I need to compare two tables in each row. the tables are as follows:-
Table a:
Code:
ID First_Name Last_name Birthdate
1 Shradha Deshkmukh 1981-12-25 00:00:00
2 Shradha Verma 1981-05-11 00:00:00
3 Divya Dutta 1982-07-21 00:00:00
4 Matthew Palmer 1983-12-28 00:00:00
table d:-
Code:
id fn ln dob
1 Shradha Tiwari 1981-12-25 00:00:00
2 Divya Dutta 1983-07-21 00:00:00
3 Sulabh Manesar 1975-09-11 00:00:00
4 Matthew Palmer 1983-12-28 00:00:00
5 Samuel Maxwell 1984-05-22 00:00:00
Now my original table is having about 17 columns and this is just a simpler version of it. The tables 'A' and 'D' are generated by a query. Table 'A' will be populated and Table D will be like a temporary table that gets its values from the query, compares all the First Names of each table and if it encounters any change , it needs to update the log table By the first name and also mentions all the columns that are different.
For this I have created a temporary table, viz. '#TMP_COMPARE' which takes all the columns of table 'a'. and then compares those columns against that of table 'd' and it has the columns PLN, PDOB, Pmatch which have values 0 by default and are set to one in case all columns match for that row(Pmatch=1), Last name matches (PLN=1), Dob matches (Pdob=1).
Once this '#TMP_COMPARE' compares the two tables I will then update the log table with the columns that dont match for a first name.
Code:
USE Testing
GO
IF OBJECT_ID('TEMPDB..#TMP_COMPARE') is not null
BEGIN
DROP TABLE #TMP_COMPARE
END
CREATE TABLE #TMP_COMPARE(
FN varchar(20),
LN varchar(20),
dob smalldatetime,
PLN int default 0,
Pdob int default 0,
Pmatch int default 0)
BEGIN
INSERT INTO #TMP_COMPARE
SELECT a.fn, a.ln, a.dob,
case when a.ln = d.Last_name AND a.dob = d.Birthdate
THEN 1
END AS #TMP_COMPARE.PMATCH,--Error in this line ..COMPARE.PM..
CASE WHEN a.dob <> d.Birthdate
THEN 0
WHEN a.dob = d.Birthdate then 1
END AS #TMP_COMPARE.Pdob,
CASE WHEN a.ln <> d.Last_name
THEN 0
WHEN a.ln = d.Last_name
then 1
END AS #TMP_COMPARE.PLN
FROM dbo.a2 as a
JoIN d ON a.fn = d.First_Name
END
SELECT * FROM #TMP_COMPARE
Error I am getting is :-
Code:
Msg 102, Level 15, State 1, Line 24
Incorrect syntax near '.'
What is wrong in my query, and should I do this any other way please advice. Now this is something very basic that is wrong in my query but I am a newbie and any help is very much appreciated.
Thanks in advance, DCS