Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 New column with data from other table

Author  Topic 

josephbenfeld
Starting Member

5 Posts

Posted - 2011-06-27 : 09:20:17
Hi I'm new and this is the first time I'm using SQL 2008 Express.

I would like to create a new column in table 1 (I can do this) and import data from table 2 with something similar to vlookup in excel.

i.e. let's assume for simplicity that there are 2 columns in each table. In table 1 column (a)contains text, column (b) is empty and I would like to use it to import data from Column (b)in table 2 when Column (a) table 2 matches column (a) table 1.
In summary: Import data from table2.column2 into table1.column2 when table2.column1 match with table1.column1. any guess?

I have created a database in SQL 2008 express. My database is made of two tables. One contains data to be updated regularly on a monthly or weekly basis (600000 rows x 8 columns). The second table contains only descriptive informations (50 rows x 3 columns) to be used together with table 1. I do understand there might be some problems with Primary key and Foreign keys.

I would much appreciate if you could teach me how to do it, step by step and possibly in SQL Server Management Studio.

Thanks

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-27 : 09:24:35
alter table mytbl add newcol varchar(1000)

update mytbl
set newcol = t2.col
from mytbl t1
join othertbl t2
on t1.col1 = t2.col2

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

josephbenfeld
Starting Member

5 Posts

Posted - 2011-06-27 : 10:18:25
Thanks for this. It looks a quite simple command. Do I have to do so every time I update the database? or the new column (the one that imports data) will be updated automatically?
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-06-29 : 05:23:56
You need to update it everytime there are changes in the other table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-06-29 : 06:15:09
I take it you want to enhance the imported data from the lookup table?
Do you really need to do this? It might be better to leave it as two tables and use a query to get the data - you could use a view if you don't want to write the query every time.

The other option is to include the process as part of the import - either as another step or by including the join in the import.

==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -