I need to compare multiple columns from then same row in a table
For example
I have on a row
1 | name | surname | phone | nameWeb | surnameWeb | phoneWeb …
and I need to compare data from DB and data from Web [….Web] columns
something like this
name | nameWeb
surname | surnameWeb
phone | phoneWeb
…
I did this with temporary tables and multiple inserts but I need an optimized solusition because I have a lot of columns
my code
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
Thank you ,
Marian
Ok, I tried to compile a fairly detailed example for you. What this will do, is build up a dynamic query by:
Obviously, this solution won’t work 100% for you, but might guide you into the solution you need. So, without any further ado, I present THE CODE:
You can just change the value of the
@TableNamevariable (which gets set at the beginning) to whatever the name of your specific table is and test it out. The assumption here is of course that there are columns that end in “Web” in that specific table…