NunoOliveira1977 Posted March 27, 2015 at 03:02 PM Report Share #580342 Posted March 27, 2015 at 03:02 PM (edited) Boa tarde, Tenho a seguinte dúvida: - 2 tabelas SQL Server rg_alertas rg_alertas_paises ------------------ ---------------------------- cod_alerta <-------------> cod_alerta patologia pais medida regiao como dados nas tabelas, tenho: rg_alertas ---------------------------------------------------------------- cod_alerta | patologia | medida ------------------------------------------------------------------- 1 | patologia teste 1 | medida teste 1 2 | patologia teste 2 | medida teste 2 rg_alertas_paises ---------------------------------------------------------- cod_alerta | pais | regiao ---------------------------------------------------------- 1 | Portugal | Algarve 1 | Espanha | Andaluzia 1 | Italia | Corsega 2 | Roménia | Não definida Resumindo, cada cod_alerta pode ter 1 ou mais registos associados na tabela rg_alertas_paises. O que pretendia era colocar esta informação, num gridview, da seguinte forma: gv_Alertas ------------------------------------------------------------------------------------------------------------------------- cod_alerta | Pais/Regiao | Patologia | Medida ------------------------------------------------------------------------------------------------------------------------- 1 | Portugal (Algarve), Espanha (Anda | Patologia Teste 1 | Medida Teste 1 | luzia), Italia (Corsega) | | -------------------------------------------------------------------------------------------------------------------------- 2 | Roménia (Não definida) | Patologia Teste 2 | Medida Teste 2 -------------------------------------------------------------------------------------------------------------------------- Alguém me consegue ajudar? Tenho andado às voltas e voltas e não consigo implementar. Obrigado. Edited March 27, 2015 at 03:03 PM by NunoOliveira1977 Link to comment Share on other sites More sharing options...
bioshock Posted March 27, 2015 at 07:55 PM Report Share #580362 Posted March 27, 2015 at 07:55 PM A tua dúvida é qual? Juntar valores de diferentes tabelas? SELECT alert.cod_alerta, CONCAT(paises.pais, '(', paises.regiao, ')'), alert.patologia, alert.medida FROM rg_alertas AS alert INNER JOIN rg_alertas_paises AS paises ON paises.cod_alerta = alert.cod_alerta Link to comment Share on other sites More sharing options...
Guest Posted March 27, 2015 at 08:10 PM Report Share #580363 Posted March 27, 2015 at 08:10 PM Já tens a query que o bioshock forneceu que faz o que pretendes. Agora é agarrar nos registos que a query retorna e preencheres a tua gridView, através da propriedade DataSource. Investiga na documentação de como o poderás fazer. Link to comment Share on other sites More sharing options...
NunoOliveira1977 Posted March 29, 2015 at 11:56 AM Author Report Share #580430 Posted March 29, 2015 at 11:56 AM Boa tarde, Obrigado pela respostas. Estive a implementar a query e dá o seguinte erro: "'CONCAT' is not a recognized built-in function name". O que será? Link to comment Share on other sites More sharing options...
bioshock Posted March 29, 2015 at 01:03 PM Report Share #580434 Posted March 29, 2015 at 01:03 PM (edited) Estive a ler e parece que a função CONCAT só apareceu no SQL Server 2012. Podes utilizar o sinal + para concatenar. paises.pais + '(' + paises.regiao + ')' Edited March 29, 2015 at 01:03 PM by bioshock Link to comment Share on other sites More sharing options...
NunoOliveira1977 Posted March 29, 2015 at 01:38 PM Author Report Share #580436 Posted March 29, 2015 at 01:38 PM Olá... A query já está a funcionar. Quando colocar gv.datadource = dr, vai aparecer com este aspeto: Cod_alerta | Pais/Regiao | Patologia | Medida -------------------------------------------------------------------------------------------------------------- 1 | Portugal (Algarve) | Patologia 1 | Medida 1 1 | Espanha (Andaluzia) | Patologia 1 | Medida 1 1 | Italia (Corsega) | Patologia 1 | Medida 1 Existe alguma possibilidade da gridview ficar com este aspeto? Cod_alerta | Pais/Regiao | Patologia | Medida -------------------------------------------------------------------------------------------------------------- 1 | Portugal (Algarve), Espanha (Andaluzia), | Patologia 1 | Medida 1 | Italia (Corsega) | ------------------------------------------------------------------------------------------------------------- Ou seja, ficar tudo apenas numa linha? Obrigado. Link to comment Share on other sites More sharing options...
Guest Posted March 29, 2015 at 04:02 PM Report Share #580440 Posted March 29, 2015 at 04:02 PM (edited) Ufa! A tua pergunta foi mesmo desafiante! E já aprendi algo novo hoje também. Por isso, os meus agradecimentos. 😉 Consegui replicar o que pretendias com este código SQL: set nocount on; declare @rg_alertas table (cod_alerta int, patologia varchar(50), medida varchar(50)) declare @rg_alertas_paises table (cod_alerta int, pais varchar(50), regiao varchar(50)) insert into @rg_alertas VALUES (1,'patologia teste 1','medida teste 1') insert into @rg_alertas VALUES (2,'patologia teste 2 ','medida teste 2') insert into @rg_alertas_paises VALUES (1,'Portugal','Algarve') insert into @rg_alertas_paises VALUES (1,'Espanha','Andaluzia') insert into @rg_alertas_paises VALUES (1,'Italia','Corsega') insert into @rg_alertas_paises VALUES (2,'Roménia','Não definida') set nocount off SELECT DISTINCT alert1.cod_alerta, STUFF((select ', ' + paises2.pais + '(' + paises2.regiao + ')' FROM @rg_alertas AS alert2 INNER JOIN @rg_alertas_paises AS paises2 ON paises2.cod_alerta = alert2.cod_alerta where alert1.cod_alerta = alert2.cod_alerta and paises1.cod_alerta = paises2.cod_alerta order by paises2.cod_alerta FOR XML PATH('')),1,2,''), alert1.patologia, alert1.medida FROM @rg_alertas AS alert1 INNER JOIN @rg_alertas_paises AS paises1 ON paises1.cod_alerta = alert1.cod_alerta Para ti apenas te interessa a query. Ignora o resto. Cumps! Edited April 6, 2015 at 01:02 PM by Guest geshi Link to comment Share on other sites More sharing options...
NunoOliveira1977 Posted March 30, 2015 at 09:13 AM Author Report Share #580467 Posted March 30, 2015 at 09:13 AM Bom dia, Era mesmo isto que pretendia. Muito obrigado pela ajuda. Foste 5* Abr Link to comment Share on other sites More sharing options...
Guest Posted March 30, 2015 at 09:30 AM Report Share #580469 Posted March 30, 2015 at 09:30 AM Ora essa. 😉 Abraço. Link to comment Share on other sites More sharing options...
NunoOliveira1977 Posted March 30, 2015 at 04:21 PM Author Report Share #580502 Posted March 30, 2015 at 04:21 PM Estou de volta... desta vez uma dúvida mais simples 🙂 Estou a fazer um insert into, por parâmetros, e quando passa texto com acentos, na bd aparece uma codificação esquisita. Como posso contornar esta questão? Obrigado. Link to comment Share on other sites More sharing options...
Guest Posted March 30, 2015 at 06:40 PM Report Share #580509 Posted March 30, 2015 at 06:40 PM Podes mostrar o teu código para ver como estás a fazer o Insert sff? Obrigado. Link to comment Share on other sites More sharing options...
NunoOliveira1977 Posted March 31, 2015 at 07:40 AM Author Report Share #580550 Posted March 31, 2015 at 07:40 AM (edited) Bom dia, Dim cmdRegistaPaisesAlerta As New SqlCommand cmdRegistaPaisesAlerta.CommandText = "INSERT INTO rg_alertas_paises(cod_alerta,cod_pais,pais,regiao,quem,quando) VALUES (@cod_alerta,@cod_pais,@pais,@regiao,@quem,@quando)" 'define os parameters do command cmdRegistaPaisesAlerta.Parameters.Add("@cod_alerta", SqlDbType.Int) cmdRegistaPaisesAlerta.Parameters.Add("@cod_pais", SqlDbType.Int) cmdRegistaPaisesAlerta.Parameters.Add("@pais", SqlDbType.NVarChar) cmdRegistaPaisesAlerta.Parameters.Add("@regiao", SqlDbType.NVarChar) cmdRegistaPaisesAlerta.Parameters.Add("@quem", SqlDbType.NVarChar) cmdRegistaPaisesAlerta.Parameters.Add("@quando", SqlDbType.NVarChar) cmdRegistaPaisesAlerta.Connection = con 'atribui os parameters com dados da gv_paises_regioes For i As Integer = 0 To gv_paises_regioes.Rows.Count - 1 cmdRegistaPaisesAlerta.Parameters(0).Value = gv_paises_regioes.Rows(i).Cells(0).Text cmdRegistaPaisesAlerta.Parameters(1).Value = gv_paises_regioes.Rows(i).Cells(1).Text cmdRegistaPaisesAlerta.Parameters(2).Value = gv_paises_regioes.Rows(i).Cells(2).Text cmdRegistaPaisesAlerta.Parameters(3).Value = gv_paises_regioes.Rows(i).Cells(3).Text cmdRegistaPaisesAlerta.Parameters(4).Value = gv_paises_regioes.Rows(i).Cells(4).Text cmdRegistaPaisesAlerta.Parameters(5).Value = gv_paises_regioes.Rows(i).Cells(5).Text cmdRegistaPaisesAlerta.ExecuteNonQuery() Next Edited April 6, 2015 at 01:02 PM by apocsantos geshi Link to comment Share on other sites More sharing options...
Guest Posted April 1, 2015 at 10:15 AM Report Share #580675 Posted April 1, 2015 at 10:15 AM (edited) Podes indicar que tipo de dados foram especificados quando criaste as tabelas? E já agora, mostra também que codificação "estranha" te aparece na BD sff. Edited April 1, 2015 at 10:17 AM by Guest Link to comment Share on other sites More sharing options...
NunoOliveira1977 Posted April 2, 2015 at 07:59 AM Author Report Share #580723 Posted April 2, 2015 at 07:59 AM Olá, bom dia os campos da tabela são: cod_alerta - int cod_pais - int pais - nvarchar(200) regiao - nvarchar(100) quem - nvarchar(100) quando - nvarchar(100) E os carateres esquisitos que aparecem, são por exemplo: Pais - quando faço o insert into com Austrália aparece Austrália Regiao - quando faço o insert into com Armádelè aparece Armádalè Para remediar altero manualmente na bd. Do que será? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now