Usuário com melhor resposta
Atualizar o telefone dos clientes

Pergunta
-
Boa tarde, estou com a seguinte dificuldade
Tenho 2 tabelas, na de clientes eu tenho o telefone em varias colunas ex.:(casa, trabalho, celular, fax) e na tabela de telefones eu tenho 1 tipo de telefone por linha (codigo do cliente, casa / codigo do cliente,trabalho etc)
Para ilustrar, essa tabela abaixo e de telefone. Como atualizar a tabela de clientes
Tabela Telefones (Vertical)
0010000020071127000000001 021 25478096
0010000020071127000000001 021 25273333
0010000020071127000000001 021 87026971Tabela Clientes (Horizontal)
0010010020051018000000001 CLAUDIO FERREIRA GONÇALVES 2772-5646 3213-6541
Obrigado
- Editado Douglas B Brito terça-feira, 25 de junho de 2013 17:04
Respostas
-
Douglas, fiz um exemplo como se voce pudesse ter até 4 telefones, na query abaixo, criei as suas 2 tabelas e coloquei os dados conforme voce nos passou, a partir disso trabalho com variaveis para controlar os loopings, são 2 loopings, o principal que é o cliente e dentro deste ha outro de telefones de cada cliente ou valido se ainda não existe nenhum insiro no tel1, se ja existe no tel1 insiro no tel2 desde que não seja igual ao tel1 e assim por diante.
De uma olhada nesta query e tente rodar no seu ambiente são somente 2 tabelas de memoria que estao sendo utilizadas, acredito que possa não ser a melhor maneira mas vai te ajudar de inicio a voce implementar no seu modelo.
--DROP TABLE #CLIENTE --DROP TABLE #TELEFONE Create table #Cliente (ID int, tel1 varchar(10),tel2 varchar(10),tel3 varchar(10), tel4 varchar(10)) Create table #telefone (ID int, tel Varchar(10)) insert into #telefone values (1,'1111-1111'), (1,'2222-2222'), (1,'3333-3333'), (2,'1123-1254'), (2,'1235-1425'), (2,'2514-2121'), (3,'1123-1254'), (3,'1235-1425'), (3,'2514-2121'), (3,'3514-2121') -- Inserindo somente ID dos clientes distintos Insert into #Cliente select distinct ID, NULL, NULL, NULL,NULL from #telefone -- Rodar daqui para baixo Declare @ID int Declare @QtdFones int Declare @QtdClientes int Declare @cliente int Declare @Fone varchar(10) set @QtdClientes = (select COUNT(*) from #Cliente) While @QtdClientes <> 0 begin set @cliente = (select top 1 ID from #telefone order by 1) set @QtdFones = (select COUNT(*) from #telefone where ID = @cliente) While @QtdFones <> 0 begin IF (select tel1 from #Cliente where ID = @cliente) is null begin update #Cliente set tel1 = @Fone where ID = @cliente delete from #telefone where ID = @cliente and tel = @Fone set @Fone = (select top 1 tel from #telefone where ID = @cliente) set @QtdClientes = (select COUNT(*) from #Cliente) end IF (select tel1 from #Cliente where ID = @cliente) is not null and (select tel2 from #Cliente where ID = @cliente) is null begin update #Cliente set tel2 = @Fone where ID = @cliente and @Fone <> tel1 delete from #telefone where ID = @cliente and tel = @Fone set @Fone = (select top 1 tel from #telefone where ID = @cliente) set @QtdClientes = (select COUNT(*) from #Cliente) end else IF (select tel1 from #Cliente where ID = @cliente) is not null and (select tel2 from #Cliente where ID = @cliente) is not null and (select tel3 from #Cliente where ID = @cliente) is null begin update #Cliente set tel3 = @Fone where ID = @cliente and @Fone <> tel1 and @Fone <> tel2 delete from #telefone where ID = @cliente and tel = @Fone set @Fone = (select top 1 tel from #telefone where ID = @cliente) set @QtdClientes = (select COUNT(*) from #Cliente) end IF (select tel1 from #Cliente where ID = @cliente) is not null and (select tel2 from #Cliente where ID = @cliente) is not null and (select tel3 from #Cliente where ID = @cliente) is not null and (select tel4 from #Cliente where ID = @cliente) is null begin update #Cliente set tel4 = @Fone where ID = @cliente and @Fone <> tel1 and @Fone <> tel2 and @Fone <> tel3 delete from #telefone where ID = @cliente and tel = @Fone set @Fone = (select top 1 tel from #telefone where ID = @cliente) set @QtdClientes = (select COUNT(*) from #Cliente) end IF (select COUNT(*) from #telefone) = 0 return set @QtdFones = (select COUNT(*) from #telefone where ID = @cliente) set @cliente = (select top 1 ID from #telefone order by 1) set @Fone = (select top 1 tel from #telefone where ID = @cliente) end end select * from #cliente
Alexandre Matayosi Conde Mauricio.
- Marcado como Resposta Douglas B Brito quarta-feira, 26 de junho de 2013 13:11
Todas as Respostas
-
Douglas,
Sinceramente não consegui entender o que você, consegui compreender a sua Modelagem e que sinceramente ao me ver esta bem fora do comum.
Você criou diversas colunas na Tabela de Clientes para armazenar os números de telefones e depois na Tabela Telefone também armazena outros números.
Porque você não criou uma única e específica tabela de telefones e armazena o código do cliente, o tipo do telefone e número?
Pedro Antonio Galvão Junior [MVP | Microsoft Evangelist | Microsoft Partner | Engenheiro de Softwares | Especialista em Banco de Dados | SorBR.Net | Professor Universitário | MSIT.com]
-
Na verdade me parece que você precisa melhorar seu banco de dados e eliminar as informações duplicadas.
-
Boa tarde Pedro, estou tentando importar dados de um sistema de terceiros que tem a estrutura diferente da nossa. Eles possuem uma tabela que armazena o n'umero dos clientes e tem campo que faz chave com uma outra tabela "clientes", j'a no nosso 'e armazenado junto dos dados cadastrais do cliente, ou seja, tudo em uma 'unica tabela. Entao a questao 'e como eu pego esses telefones e importo para o nosso cadastro esses telefones?
Meu sistema
id_cliente | nome telefone1 | telefone2 | telefone3
1 | douglas | 1111-1111 | 2222-2222 | 3333-3333
2 | ze maria | 1123-1254 | 1235-1425 | 2514-2121
No outro sistema
id_ telefone | telefone
1 | 1111-1111
1 | 2222-2222
1 | 3333-3333
2 | 1123-1254
2 | 1235-1425
2 | 2514-2121
Obrigado pela sua atencao
-
Boa tarde Elton, nao ha dados duplicados. Acontece que sao tabelas de dois sistemas diferentes e estou querendo importar esses telefones para o meu cadastro. Mas como a inclusao do nosso cadastro fica em apenas uma tabela e a do cliente em 2 (cliente e telefone), preciso recuperar esses telefones.
Obrigado pela atencao
-
Boa tarde Douglas, no seu campo terá sempre 3 telefones ? se for isto ou tendo uma regra quanto a quantidade pode-se fazer updates simples validando se o numero ja existe nos outros campos de telefones conforme abaixo, caso contrario que não tiver uma regra acredito que voce teria que fazer alguem cursor ou looping para validar registro a registro o telefone e inserir no campo correto que ainda não exista em outro, segue exemplo sendo com 3 telefones:
Create table #Cliente (ID int, tel1 varchar(10),tel2 varchar(10),tel3 varchar(10)) Create table #telefone (ID int, tel Varchar(10)) insert into #telefone values (1,'1111-1111'), (1,'2222-2222'), (1,'3333-3333'), (2,'1123-1254'), (2,'1235-1425'), (2,'2514-2121') Insert into #Cliente select distinct ID, NULL, NULL, NULL from #telefone Update C set C.Tel1 = Case when C.Tel1 is null and C.Tel2 is null and C.Tel3 is null then T.Tel else C.Tel1 end from #Cliente as C inner join #telefone as T on C.ID = T.ID Update C set C.Tel2 = Case when C.Tel2 is null and C.Tel3 is null then T.Tel else null end from #Cliente as C inner join #telefone as T on C.ID = T.ID where C.tel1 <> T.tel Update C set C.Tel3 = Case when C.Tel3 is null and C.Tel2 is not null and C.Tel1 is not null then T.Tel else null end from #Cliente as C inner join #telefone as T on C.ID = T.ID where C.tel1 <> T.tel and C.tel2 <> T.tel select * from #Cliente
Alexandre Matayosi Conde Mauricio.
-
-
Douglas, fiz um exemplo como se voce pudesse ter até 4 telefones, na query abaixo, criei as suas 2 tabelas e coloquei os dados conforme voce nos passou, a partir disso trabalho com variaveis para controlar os loopings, são 2 loopings, o principal que é o cliente e dentro deste ha outro de telefones de cada cliente ou valido se ainda não existe nenhum insiro no tel1, se ja existe no tel1 insiro no tel2 desde que não seja igual ao tel1 e assim por diante.
De uma olhada nesta query e tente rodar no seu ambiente são somente 2 tabelas de memoria que estao sendo utilizadas, acredito que possa não ser a melhor maneira mas vai te ajudar de inicio a voce implementar no seu modelo.
--DROP TABLE #CLIENTE --DROP TABLE #TELEFONE Create table #Cliente (ID int, tel1 varchar(10),tel2 varchar(10),tel3 varchar(10), tel4 varchar(10)) Create table #telefone (ID int, tel Varchar(10)) insert into #telefone values (1,'1111-1111'), (1,'2222-2222'), (1,'3333-3333'), (2,'1123-1254'), (2,'1235-1425'), (2,'2514-2121'), (3,'1123-1254'), (3,'1235-1425'), (3,'2514-2121'), (3,'3514-2121') -- Inserindo somente ID dos clientes distintos Insert into #Cliente select distinct ID, NULL, NULL, NULL,NULL from #telefone -- Rodar daqui para baixo Declare @ID int Declare @QtdFones int Declare @QtdClientes int Declare @cliente int Declare @Fone varchar(10) set @QtdClientes = (select COUNT(*) from #Cliente) While @QtdClientes <> 0 begin set @cliente = (select top 1 ID from #telefone order by 1) set @QtdFones = (select COUNT(*) from #telefone where ID = @cliente) While @QtdFones <> 0 begin IF (select tel1 from #Cliente where ID = @cliente) is null begin update #Cliente set tel1 = @Fone where ID = @cliente delete from #telefone where ID = @cliente and tel = @Fone set @Fone = (select top 1 tel from #telefone where ID = @cliente) set @QtdClientes = (select COUNT(*) from #Cliente) end IF (select tel1 from #Cliente where ID = @cliente) is not null and (select tel2 from #Cliente where ID = @cliente) is null begin update #Cliente set tel2 = @Fone where ID = @cliente and @Fone <> tel1 delete from #telefone where ID = @cliente and tel = @Fone set @Fone = (select top 1 tel from #telefone where ID = @cliente) set @QtdClientes = (select COUNT(*) from #Cliente) end else IF (select tel1 from #Cliente where ID = @cliente) is not null and (select tel2 from #Cliente where ID = @cliente) is not null and (select tel3 from #Cliente where ID = @cliente) is null begin update #Cliente set tel3 = @Fone where ID = @cliente and @Fone <> tel1 and @Fone <> tel2 delete from #telefone where ID = @cliente and tel = @Fone set @Fone = (select top 1 tel from #telefone where ID = @cliente) set @QtdClientes = (select COUNT(*) from #Cliente) end IF (select tel1 from #Cliente where ID = @cliente) is not null and (select tel2 from #Cliente where ID = @cliente) is not null and (select tel3 from #Cliente where ID = @cliente) is not null and (select tel4 from #Cliente where ID = @cliente) is null begin update #Cliente set tel4 = @Fone where ID = @cliente and @Fone <> tel1 and @Fone <> tel2 and @Fone <> tel3 delete from #telefone where ID = @cliente and tel = @Fone set @Fone = (select top 1 tel from #telefone where ID = @cliente) set @QtdClientes = (select COUNT(*) from #Cliente) end IF (select COUNT(*) from #telefone) = 0 return set @QtdFones = (select COUNT(*) from #telefone where ID = @cliente) set @cliente = (select top 1 ID from #telefone order by 1) set @Fone = (select top 1 tel from #telefone where ID = @cliente) end end select * from #cliente
Alexandre Matayosi Conde Mauricio.
- Marcado como Resposta Douglas B Brito quarta-feira, 26 de junho de 2013 13:11
-