Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Column By Column Comparison
SQL Server 2000

I will to compare a normal table with a replicated audit table having
identical columns. I wish to report on the differences between the
data.

How can I loop though a "column list" in TSQL rather than explicitly
naming each column to compare ?

Thanks,
Craig


Report this thread to moderator Post Follow-up to this message
Old Post
csomberg@dwr.com
10-27-05 02:24 PM


Re: Column By Column Comparison
Do you just want to know if there IS a difference or what sort of
difference ?

--For the Whole Table
SELECT  checksum_agg(binary_
checksum(*)) from SomeTable

--For just the rows
Select binary_checksum(*) from SomeTable

If you need more granualarity, then it would be more work.

HTH, Jens Suessmeyer.


Report this thread to moderator Post Follow-up to this message
Old Post
Jens
10-27-05 02:24 PM


Re: Column By Column Comparison
Jens,

This is not 100% reliable.  If the checksum functions are
different, there must be a difference in the tables, but the
converse is not true.  The checksum functions can return the
same values on different data, like any hash function.

The only sure way is to compare column by column values.

Steve Kass
Drew University

Jens  wrote:
> Do you just want to know if there IS a difference or what sort of
> difference ?
>
> --For the Whole Table
> SELECT  checksum_agg(binary_
checksum(*)) from SomeTable
>
> --For just the rows
> Select binary_checksum(*) from SomeTable
>
> If you need more granualarity, then it would be more work.
>
> HTH, Jens Suessmeyer.
>

Report this thread to moderator Post Follow-up to this message
Old Post
Steve Kass
10-27-05 02:24 PM


Re: Column By Column Comparison
Yes I agree with you, definitely. But as I said, to have more
granularity and more reliabilty you have to use another mechanism which
causes more coding. but as I said, I am with you.

Jens Suessmeyer.


Report this thread to moderator Post Follow-up to this message
Old Post
Jens
10-27-05 02:24 PM


Re: Column By Column Comparison
Thanks for the reply.

I have to interrogate each column and determine which is different and
then report on the difference. I was thinking more in the lines of
looping through the columns collection somehow without having to know
the name of each column ...

Thanks again !


Report this thread to moderator Post Follow-up to this message
Old Post
csomberg@dwr.com
10-27-05 02:24 PM


Re: Column By Column Comparison
(csomberg@dwr.com)  writes:
> SQL Server 2000
>
> I will to compare a normal table with a replicated audit table having
> identical columns. I wish to report on the differences between the
> data.
>
> How can I loop though a "column list" in TSQL rather than explicitly
> naming each column to compare ?

You can get the columns for a table with

SELECT name FROM syscolumns WHERE id = object_name('tbl')

I would suggest that it is best to generate the SQL statement from client
code, since client languages are better apt for string manipulation. You
can build SQL strings in T-SQL as well, and exeute them with EXEC() or
sp_executesql, but this is bulkier.

If you want to be static, one option is to use a client-language to
generate a stored procedure.

Keep in mind that you cannot just say:

SELECT ...
FROM   tbl a
JOIN   audit_tbl b ON a.keycol = b.keycol
WHERE  a.col1 <> b.col1
AND a.col2 <> b.col2
..

Correct is:

WHERE  (a.col1 <> b.col1 OR
a.col1 IS NULL AND b.col1 IS NOT NULL OR
a.col1 IS NOT NULL AND b.col1 IS NULL)
AND


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp


Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
10-27-05 02:24 PM


Re: Column By Column Comparison
On Fri, 14 Oct 2005 21:51:02 +0000 (UTC), Erland Sommarskog wrote:

 (snip)
>Keep in mind that you cannot just say:
>
>   SELECT ...
>   FROM   tbl a
>   JOIN   audit_tbl b ON a.keycol = b.keycol
>   WHERE  a.col1 <> b.col1
>      AND a.col2 <> b.col2
>     ...
>
>Correct is:
>
>   WHERE  (a.col1 <> b.col1 OR
>           a.col1 IS NULL AND b.col1 IS NOT NULL OR
>           a.col1 IS NOT NULL AND b.col1 IS NULL)
>       AND

Hi Erland,

This can be (slightly) simplified to

WHERE  (NULLIF (a.col1, b.col1) IS NOT NULL OR
NULLIF (b.col1, a.col1) IS NOT NULL)
AND


Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Report this thread to moderator Post Follow-up to this message
Old Post
Hugo Kornelis
10-27-05 02:24 PM


Re: Column By Column Comparison
Hugo Kornelis (hugo@pe_NO_rFact.in_SPAM_fo)  writes:
> This can be (slightly) simplified to
>
>    WHERE  (NULLIF (a.col1, b.col1) IS NOT NULL OR
>            NULLIF (b.col1, a.col1) IS NOT NULL)
>        AND

Simplified in the sense "few characters to type, yes". Else I'm not
sure that I find it simpler. Or it is just that it's Friday night after
a hard and long working week...

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp


Report this thread to moderator Post Follow-up to this message
Old Post
Erland Sommarskog
10-27-05 02:24 PM


Re: Column By Column Comparison
On Fri, 14 Oct 2005 22:14:32 +0000 (UTC), Erland Sommarskog wrote:

>Hugo Kornelis (hugo@pe_NO_rFact.in_SPAM_fo) writes: 
>
>Simplified in the sense "few characters to type, yes". Else I'm not
>sure that I find it simpler. Or it is just that it's Friday night after
>a hard and long working week...

Hi Erland,

Well, okay. It's an acquired taste, I'll have to grant you that. :-)

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)

Report this thread to moderator Post Follow-up to this message
Old Post
Hugo Kornelis
10-27-05 02:24 PM


Re: Column By Column Comparison
SELECT X.a, X.b, ... X.z
FROM
(SELECT a, .b, ... z
FROM AuditData
UNION ALL
SELECT a, b, ... z
FROM BaseData)
AS X (a, b, ... z)
GROUP BY X.a, X.b, ... X.z
HAVING COUNT(*) < 2;

or if we had SQL-92 set operators, you could write:

SELECT *
FROM (SELECT 'B', * FROM BaseData)
EXCEPT
(SELECT 'A' , * FROM AuditData);

or
SELECT *
FROM (SELECT 'A', * FROM AuditData)
EXCEPT
(SELECT 'B' , * FROM BaseData);


Report this thread to moderator Post Follow-up to this message
Old Post
--CELKO--
10-27-05 02:24 PM


Sponsored Links





Last Thread Next Thread
Pages (2): [1] 2 »
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 01:22 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006