ALTER PROCEDURE dbo.CompareTables
(
@table1 VARCHAR(100),
@table2 VARCHAR(100),
@T1ColumnList VARCHAR(1000),
@T2ColumnList VARCHAR(1000) = ''
)
AS
/*
Table1, Table2 are the tables or views to compare.
T1ColumnList is the list of columns to compare, from table1.
Just list them comma-separated, like in a GROUP BY clause.
If T2ColumnList is not specified, it is assumed to be the same
as T1ColumnList. Otherwise, list the columns of Table2 in
the same order as the columns in table1 that you wish to compare.</p>
<pre><code>The result is all rows from either table that do NOT match
the other table in all columns specified, along with which table that
row is from.
* /
ОБЪЯВИТЬ @SQL VARCHAR (8000)
IF @ t2ColumnList = '' SET @ T2ColumnList = @ T1ColumnList
SET @SQL = 'SELECT' '' + @ table1 + '' 'AS TableName,' + @ t1ColumnList +
'FROM' + @ Table1 + 'UNION ALL SELECT' '' + @ table2 + '' 'TableName,' +
@ t2ColumnList + 'FROM' + @ Table2
SET @SQL = 'SELECT Max (TableName) как TableName,' + @ t1ColumnList +
'FROM (' + @SQL + ') GROUP BY' + @ t1ColumnList +
'HAVING COUNT (*) = 1'
EXEC (@SQL)