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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Foreign Key look up

Author  Topic 

banderbe
Starting Member

2 Posts

Posted - 2009-04-07 : 14:05:36
Let me phrase my question in a generic fashion:

Let’s say there’s TableA with three fields Field1, Field2, Field3 and those fields are all constrained by FieldID in TableB. TableB also has a field called FieldName.

I just want get all the fields in TableA but instead of the integers in Field1,2 and 3 get the FieldName the integers correspond to.

I'm a SQL noob so I don't know the best way to do this.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-07 : 14:11:03
You would use a join for this. Here's a simple example:

SELECT t1.Column1, t2.Column2, t2.Column2
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.Column1 = t2.Column1

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

banderbe
Starting Member

2 Posts

Posted - 2009-04-07 : 20:58:02
I assume you meant

SELECT t1.Column1, t1.Column2, t1.Column3

??
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-04-08 : 00:13:09
I do have a typo (t2.Column3), but it just depends on where the columns reside.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -