第一种方法,质朴的(我不习惯 T-SQL,我做了更多的 PL/SQL),但这可能会帮助你走得更远,并在 SQL SERVER 2008 中测试。希望它在 2005 年有效......)
所以,我们创建了两个过程,一个调用另一个
提供的代码只能一次检查 - 2 个不同的 ID - 所有相关字段(Account、AccountNum、AccountNumber)
这个想法(检查 AccountNumber 列)
查找表(在表 INFORMATION_SCHEMA.columns 中,其中列出了您的数据库表),其中的列具有提供的 3 个名称之一
对于找到的每个表:创建一个动态查询:
select count(*) from <THE_TABLE> where <Account_column_name> IN (123456 654321);
如果我们有 2 个结果(意味着我们的 Id 都存在于表中),我们启动第二个过程,参数为 : @TableName = <THE_TABLE>
, @FieldName = <Account_column_name>
, @FirstId = 123456
,@SecondId = 654321
<THE_TABLE>
我们找到了(同样在 INFORMATION_SCHEMA.columns 中)的列名。对于找到的每一列:创建一个动态查询
select count(*) from <THE_TABLE> as T1
inner join <THE_TABLE> as T2 on T1.<COLUMN_NAME> = T2.<COLUMN_NAME>
where T1.<Account_column_name>= 123456
and T2.<Account_column_name>= 654321
如果count(*) = 1
,则表示对于给定的 id,相同的值存在于同一张表的同一列中。
在这种情况下,我们打印<THE_TABLE>
并<THE_COLUMN>
要启动搜索,在 sql management studio 中,只需
EXEC GetSimilarValuesForFieldAndValue 123456, 654321
在“消息”部分,您应该有一个“结果”列表。
CREATE procedure [dbo].[GetSimilarValuesForFieldAndValue](@FirstId int, @SecondId int)
AS
DECLARE @sql nvarchar(MAX);
DECLARE @params NVARCHAR(MAX);
DECLARE @Count int;
DECLARE @Name NVARCHAR(MAX);
DECLARE @FieldName NVARCHAR(MAX);
DECLARE db_cursor CURSOR for
select TABLE_NAME, COLUMN_NAME FROM INFORMATION_SCHEMA.columns
where COLUMN_NAME IN('Account', 'AccountNumber', 'AccountNum');
OPEN db_cursor
FETCH next from db_cursor into @Name, @FieldName
while @@FETCH_STATUS = 0
begin
select @sql =
N' SELECT @Count=Count(*) FROM ' + @Name +
N' WHERE ' +@FieldName+' IN (@FirstId,@SecondId)'
SELECT @params = N'@FieldName NVARCHAR(MAX), @FirstId int, @SecondId int, @Count int out'
EXEC sp_executesql @sql, @params, @FieldName, @FirstId, @SecondId, @Count OUT
if (@Count = 2)
begin
exec dbo.CompareFields @Name, @FieldName, @FirstId, @SecondId
end
FETCH NEXT FROM db_cursor INTO @Name, @FieldName
end
close db_cursor
DEALLOCATE db_cursor
GO
第二个 :
CREATE procedure [dbo].[CompareFields](@TableName NVARCHAR(MAX), @FieldName NVARCHAR(MAX), @FirstId int, @SecondId int)
as
DECLARE @ColumnName NVARCHAR(MAX)
DECLARE @Sql NVARCHAR(MAX)
DECLARE @Params NVARCHAR(MAX)
DECLARE @Count int
DECLARE cfCursor CURSOR FOR
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME = ''+@TableName+' '
AND COLUMN_NAME <> ' '+@FieldName+''
OPEN cfCursor
FETCH next from cfCursor into @ColumnName
while @@FETCH_STATUS = 0
begin
select @Sql =
N' SELECT @Count = count(*) from ' +@TableName + ' T1 '+
N' INNER JOIN ' + @TableName + ' T2 ON T1.' +@ColumnName + ' = T2.' + @ColumnName +
N' WHERE T1.' +@FieldName + ' = '+ CAST(@FirstId as varchar) +
N' AND T2.' + @FieldName + ' = '+CAST(@SecondId as varchar)
SELECT @Params =
N'@TableName VARCHAR(MAX), @ColumnName VARCHAR(MAX), '+
N'@FieldName VARCHAR(MAX), @FirstId int, @SecondId int, @Count int out'
exec sp_executesql @sql, @Params, @TableName, @ColumnName, @FieldName, @FirstId, @SecondId, @Count OUT
if @Count = 1
begin
--print tableName and column Name if value is identic
print 'Table : ' + @TableName + ' : same value for ' + @ColumnName
end
FETCH NEXT FROM cfCursor INTO @ColumnName
end
close cfCursor
DEALLOCATE cfCursor
GO