我需要比较表中同一行的多个列 例如 我有一个
1 |名字|姓氏|电话| nameWeb | surnameWeb | phoneWeb ...
我需要比较来自数据库的数据和来自Web [.... Web]列的数据 像这样的东西
名称| nameWeb
姓氏surnameWeb
手机| phoneWeb ...
我用临时表和多个插件做了这个,但我需要一个优化的solusition,因为我有很多列
我的代码
SELECT
-- Contatto
c.id ,
-- Ditta
d.nome 'nomeDitta' ,
d.filiale ,
d.webNome webNomeDitta ,
d.webDescrizione webDescrizione ,
-- Persona
p.nome Nome ,
p.cognome Cognome ,
p.email ,
p.telefono ,
p.fax ,
p.webNome ,
p.webCognome ,
p.webEmail ,
p.webTelefono ,
p.webFax ,
p.webNoteAggiuntive ,
p.canali ,
-- Indirizzo
i.indirizzo1 ,
i.indirizzo2 ,
i.cap ,
i.localita ,
i.webIndirizzo1 ,
i.webIndirizzo2 ,
i.webCap ,
i.webLocalita ,
i.webNome 'NomeInd' ,
-- Nazione
n.stato 'Nazione' ,
n2.stato 'webNazione' ,
-- Lingua
L.nome 'webLingua'
INTO #webCont
FROM dbo.contatto c ...
WHERE c.id = @idContatto
DECLARE @result TABLE ( ColumnName NVARCHAR(100) ,
DB NVARCHAR(100) ,
Web NVARCHAR(100) ,
hasData BIT)
INSERT INTO @result SELECT 'Nome Ditta' , nomeDitta , webNomeDitta , @hasData FROM #webCont
INSERT INTO @result SELECT 'Nome' , Nome , webNome , @hasData FROM #webCont
INSERT INTO @result SELECT 'Cognome' , Cognome , webCognome , @hasData FROM #webCont
INSERT INTO @result SELECT 'eMail' , email , webEmail , @hasData FROM #webCont
INSERT INTO @result SELECT 'Telefono' , telefono , webTelefono , @hasData FROM #webCont
INSERT INTO @result SELECT 'Fax' , fax , webFax , @hasData FROM #webCont
INSERT INTO @result SELECT 'Indirizzo 1' , indirizzo1 , webIndirizzo1 , @hasData FROM #webCont
INSERT INTO @result SELECT 'Indirizzo 2' , indirizzo2 , webIndirizzo2 , @hasData FROM #webCont
INSERT INTO @result SELECT 'Cap' , cap , webCap , @hasData FROM #webCont
INSERT INTO @result SELECT 'Localita' , localita , webLocalita , @hasData FROM #webCont
INSERT INTO @result SELECT 'Nazione' , Nazione , webNazione , @hasData FROM #webCont
谢谢你, 玛丽安
答案 0 :(得分:1)
好的,我试着为你编译一个相当详细的例子。这将做的是通过以下方式构建动态查询:
显然,此解决方案不会100%为您服务,但可能会引导您进入所需的解决方案。所以,没有任何进一步的麻烦,我提出代码:
DECLARE
@SQL nvarchar(max)
, @TableName nvarchar(max)
SET @TableName = 'Test'
SET @SQL = 'SELECT '
;WITH NonWeb AS
(
SELECT
[COLUMN_NAME] as [NonWebColumn]
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
[TABLE_NAME] = @TableName
AND [COLUMN_NAME] NOT LIKE '%Web'
),
Web AS
(
SELECT
'[' + C.[COLUMN_NAME] + ']' as [WebColumn]
, '[' + NonWeb.[NonWebColumn] + ']' as [NonWebColumn]
, 'CASE WHEN [' + C.[COLUMN_NAME] + '] = [' + NonWeb.[NonWebColumn] + '] THEN ''Equal'' ELSE ''Not Equal'' END as [' + NonWeb.[NonWebColumn] + 'Match]' as [Match]
FROM INFORMATION_SCHEMA.COLUMNS C
INNER JOIN NonWeb ON
REPLACE(C.[COLUMN_NAME],'Web','') = NonWeb.[NonWebColumn]
WHERE
[TABLE_NAME] = @TableName
AND [COLUMN_NAME] LIKE '%Web'
)
SELECT @SQL = 'SELECT ' + STUFF
(
(
SELECT
', ' + [NonWebColumn] + ', ' + [WebColumn] + ', ' + [Match]
FROM Web
FOR XML PATH('')
)
, 1
, 2
, ''
) + ' FROM ' + @TableName
EXEC sp_ExecuteSql @SQL
您只需将@TableName
变量(在开头设置)的值更改为特定表的名称并进行测试即可。这里的假设当然是有一些列以特定表格中的“Web”结尾......
答案 1 :(得分:1)
我认为您不需要@result
表,您可以使用UNION
代替。另外,您应该对样本FROM dbo.contatto c ...
Here中未提及的查询进行分析,并说明如何执行此操作。您可能也不需要#webCont
,但它确实依赖。您可能应该考虑使用Indexed Views,它们会降低操作速度,但可以用于查找表。