Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

trankilis

Comparar os dados com outra tabela

Recommended Posts

Pessoal estou com uma duvida.

 

Vou explicar meu problema, tenho uma tabela com os dados e outra tabela para preencher esses dados.

 

CODE

Tabela 1 (Dados que possuo)

Nome Telefone

---------------------- ----------------------

Joao 12345678

Joao 87654321

Joao 56781234

Joao 43215678

Joao 12121212

 

 

Tabela2 que preciso preencher os dados:

 

 

Nome Telefone1 Telefone2 Telefone3 Telefone4 TelAntigo1 TelAntigo2 TelAntigo3 TelAntigo4

---------------------- --------- --------- --------- --------- ---------- ---------- ---------- ----------

Joao 12345678 33334455

 

 

 

Eu preciso preecher os telefones 1, 2, 3 e 4 desde que os mesmos sejam diferente dos telefones antigos.

 

O que eu fiz foi o seguinte, criei um cursor para a tabela 2, A qual preciso preencher, e que sempre terá menos registros, depois no fim do SP me saiu uma biblia, vou postar mais ou menos como ficou:

 

CODE
declare tabela2 cursor for

select nome, tel1, tel2, tel3, tel4, histtel1, histtel2, histtel3, histtel4

from tabela2

 

open tabela2

fetch next from tabela2

into @nome, @tel1, @tel2, @tel3, @tel4, @telantigo1, @telantigo2, @telantigo3, @telantigo4

 

while @@fetch_status = 0

begin

 

if @tel1 is null

begin

update b set

b.tel1 = a.telelefone

from tabela2 b inner join tabela1 a

on a.nome = b.nome

where nome = @nome and @telantigo1 <> a.telefone and @telantigo2 <> a.telefone and @telantigo3 <> a.telefone and @telantigo4 <> a.telefone

 

end

if @tel1 is not null and @tel2 is null

begin

update b set

b.tel2 = a.telefone, @tel2 = a.telefone

from tabela2 b inner join tabela1 a

on a.nome = b.nome

where nome = @nome and b.tel1 <> a.telefone and @telantigo1 <> a.telefone and @telantigo2 <> a.telefone and @telantigo3 <> a.telefone and @telantigo4 <> a.telefone

 

end

if @tel1 is not null and @tel2 is not null @tel3 is null

begin

update b set

b.tel3 = a.telefone, @tel3 = a.telefone

from tabela2 b inner join tabela1 a

on a.nome = b.nome

where nome = @nome and b.tel2 <> a.telefone and b.tel1 <> a.telefone and @telantigo1 <> a.telefone and @telantigo2 <> a.telefone and @telantigo3 <> a.telefone and @telantigo4 <> a.telefone

 

end

if l@tel1 is not null and @tel2 is not null @tel3 is not null and @tel4 is null

begin

update b set

b.tel4 = a.telefone, @tel4 = a.telefone

from tabela2 b inner join tabela1 a

on a.nome = b.nome

where nome = @nome and b.tel3 <> a.telefone and b.tel2 <> a.telefone and b.tel1 <> a.telefone and @telantigo1 <> a.telefone and @telantigo2 <> a.telefone and @telantigo3 <> a.telefone and @telantigo4 <> a.telefone

 

end

fetch next from tabela2

into @nome, @tel1, @tel2, @tel3, @tel4, @telantigo1, @telantigo2, @telantigo3, @telantigo4

end

 

O que eu queria saber é, existe algo mais compacto e simples?

Existe uma maneira de eu "matar" essas variaveis, @tel2, @tel3.... @telantigo2... E fazer com que a variavel @tel1 e @telantigo1

Compartilhar este post


Link para o post
Compartilhar em outros sites

Qdo se trata deste tipo de atualizacao, nem sempre o mais compacto eh o mais correto ou ateh msm o correto.

Tem q verificar mais a performance e o impacto sobre sua aplicacao. Qdo você lida com cursor, select e update tem q ser bem pensado para nao jogar o processamento do server em 100%. Nao sou contra o uso de cursor, algumas pessoas gostam outras preferem usar tabelas temporarias.

Neste topico o eriva colocou uma dica bacana de usar cursor e tabela temporaria para atualizacao de dados. De uma lida e ve se te ajuda:

 

http://forum.imasters.com.br/index.php?showtopic=224865

 

Abçs

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.