Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Comparing all columns of two tables

Author  Topic 

dcs
Starting Member

6 Posts

Posted - 2011-11-17 : 11:42:01
I need to compare two tables in each row. the tables are as follows:-

Table a:

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:-

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.

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.a
JoIN d ON a.fn = d.First_Name
END

SELECT * FROM #TMP_COMPARE



Error I am getting is :-


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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-17 : 11:56:47
it should be


...

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 PMATCH,--Error in this line ..COMPARE.PM..
CASE WHEN a.dob <> d.Birthdate
THEN 0
WHEN a.dob = d.Birthdate then 1
END AS Pdob,

CASE WHEN a.ln <> d.Last_name
THEN 0
WHEN a.ln = d.Last_name
then 1
END AS PLN

FROM dbo.a2 as a
JoIN d ON a.fn = d.First_Name
END
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dcs
Starting Member

6 Posts

Posted - 2011-11-17 : 12:19:29
Hi Vishakh. Many thanks for your response. I made the changes. It works. I just need to get the data from this table into another log table as per each first name.

Do you think this is the right approach since, as I mentioned in my original post, the original table is having 17 columns. And this table will be about a orders , item numbers, transaction number etc. This record will be scheduled to check as per a query and in case there is any change made on the side of a client it needs to fire a notification to the relevant authority.

Thanks in advance and again many thanks for ur suggestion.


DCS
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-18 : 01:37:25
quote:
Originally posted by dcs

Hi Vishakh. Many thanks for your response. I made the changes. It works. I just need to get the data from this table into another log table as per each first name.

Do you think this is the right approach since, as I mentioned in my original post, the original table is having 17 columns. And this table will be about a orders , item numbers, transaction number etc. This record will be scheduled to check as per a query and in case there is any change made on the side of a client it needs to fire a notification to the relevant authority.

Thanks in advance and again many thanks for ur suggestion.


DCS


Is this a periodic comparison? or do you want this comparison be done each time a DML operation (insert/update/delete) is fired on yourtable?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

dcs
Starting Member

6 Posts

Posted - 2011-11-18 : 04:40:08
Hi Vishakh,
MANY MANY THANKS for your time.I really appreciate it.

Its like a shipping database. All the bookings are extracted from a query and put into a temporary table. Then forwarded to the relevant authorities.
But it has to keep checking by the same query that the results are same or not. If there is any difference like Date of Departure, number of boxes etc, it should send over a notification and this change should be loaded on to the log file.

This log file will hold only upper level info about it. eg.when was there a change and in which columns, sort of for KPI purposes. The only thing is as of now, I will use the approach above, of creating 17 columns each integer and each representing one of the column comparisons.

After all comparison is done, I need to get the all columns that have value 0(not match) to put that in log file.

something like

CREATE TABLE COMPARETABLE
(BookNum varchar(20), ColumnNotMatch varchar(200) )

INSERT INTO COMPARETABLE(BookNum,ColumnNotMatch)
SELECT BookNum,
CASE p1<>1 SET 'ItemCode,'+
CASE p2<>1 SET 'Quantity,'+
....--I want this to work Like CONTINUE in C++ where it should --go and match all columns.

CASE p17<>1 SET 'NumberOfBoxes'
+'dont match'
END
FROM Table1 T1 JOIN TABLE2 AS T2
ON T1.BookNum= T2.BookNum


SELECT
INSERT INTO LogTable
(BookingNum,
TimeOfCheck,
ColumnsNotMatching)
VALUES
SELECT BookNum,getdate(),@ColumnNotMatch FROM TABLE1 AS T JOIN CompareTable CT
ON T.BookNum=CT.BookNum


I dont think this will work, is there any better way of doing this comparison and loading in log files. Please advice and Thanks again.

Regards,
DCS
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-18 : 11:35:15
why the above wont work? are you getting any error?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -