update sql relating a column with external value

Asked

Viewed 801 times

1

Good morning, I’m having a hard time updating SQL Server. I have the following situation:

Table 1

ID
Codigo

Table 2

ID
Nome

And I have an excel file like this:

Excel

Codigo
Nome

I need to update the column Name of Table 2, with the data of excel, what would be querry in that situation?

I tried that:

SELECT *
FROM Tabela2 F
FULL JOIN Tabela1 M ON F.id = M.codigo
  • What is the relationship between Tabela1 and Tabela2?

  • Hi and welcome, have you tried anything? at least show some effort.

  • The ID column, I tried this select * from Table2 F full Join Table1 M on F.id = M.codigo But showed overflow error

  • Please show sample data and expected results, and you are trying to update the table of an excel file or what?

  • Gabriel, to access/import data from Excel spreadsheet I suggest reading the article "Bulk data import". So you can access/import directly, without manual activity. https://portosql.wordpress.com/2018/08/12/importacao-dados-mass/

  • (1) The relationship between the tables Tabela 1 and Tabela 2 occurs by ID columns? (2) What is the file extension that contains the spreadsheet: xls or xlsx?

Show 1 more comment

2 answers

1

I managed with the following situation

update Tabela 2
set Nome = '**NOME**'
from Tabela 2 A
INNER JOIN Tabela 1 B on B.ID = A.id
where B.Codigo = '**CODIGO**'

As the values are in an excel file I need to update a situation in an SQL table, I will need to manually make this querry for every update I need to update. The values NAME, CODE are in excel. By excel itself I can make a structure of CONCATENATE, which will kind of prepare the querrys. Thanks for the tips, and for the attention!

  • I could not see in your solution the use of Excel data. I believe that your answer is incomplete for other people who will seek a solution in the future.

  • Your question is not clear at all @Andrew Paes

  • @Sami OP says you need to update the column Table Name 2, with excel data. In his query it is not showing where Excel data is coming from. Where it comes from?

  • @Andrewpaes I am manually taking the values of excel, and doing a manual work. I updated ve if I become clearer.

  • I think it just means that the data was uploading it from the excel file @Andrew Paes

  • Well, I would have done a BULK INSERT using Excel, and later made a relationship between the tables looking in Table 2 only who needs to have their name updated, in the case of those who diverge from Excel, thinking that this could be an automated process.

Show 1 more comment

0


I think you’re looking for inner join:

SELECT T2.Nome,
       T1.Codigo
FROM Table1 T1 INNER JOIN Table2 T2 ON T1.ID = T2.ID
WHERE --outras condições

To insert the excel file data:

SELECT * INTO nome do arquivo
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
    'Excel 12.0; Database=nome do arquivo.xlsx', [página$]);

If you are looking for an update when joining the two tables:

UPDATE T2
SET Nome = 'NOME'
FROM [Tabela 2] T2
INNER JOIN [Tabela 1] T1 on T2.ID = T1.id
WHERE T2.Codigo = 'CODIGO'
  • Well, it does not solve the OP situation. It wants to know how to do an UPDATE using Excel data.

  • Thank you for your attention, I can do this Internet Join, but where would I put a Where? Because I still need to co-relate the Code column to the Column ID in table 2.

  • Look at his comment

  • I can’t think of a syntax that co-relates the two tables, and I can only update the Code I need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.