SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Lookup Table Setup?


Lookup Table Setup?

Author
Message
Nooch
Nooch
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 72
I have table1 with multiple columns, the Status column has 3 possible values (1,2,3). I created another table(2) named status_lut (below). When I query table1 I need it to display the StatusName. How would I create the lookup table?

StatusNum StatusName
1 Submitted
2 Active
3 Done

Thank You
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59848 Visits: 17947
nuchbcc (8/13/2013)
I have table1 with multiple columns, the Status column has 3 possible values (1,2,3). I created another table(2) named status_lut (below). When I query table1 I need it to display the StatusName. How would I create the lookup table?

StatusNum StatusName
1 Submitted
2 Active
3 Done

Thank You


I am a bit confused. Isn't what you posted your lookup table? Are you really trying to ask how you would display the StatusName from this table when joining to another table?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
padmavathi.hsbc
padmavathi.hsbc
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 9
I assume that you are trying to match values of StatusNum from table2 and status from table1

select
b.StatusName
from querytable.dbo.table1 a
join querytable.dbo.table2 b
on a.status = b.StatusNum
Nooch
Nooch
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 72
I tried the JOIN query separately and received the output that I needed but when I added it to the existing query, it failed.

I guess what I'm trying to ask is do I need to setup PK and FK? If yes do I create the FK from table1, set the PK table to be table2 pointing to StatusName column and FK table to be table1 pointing to StatusID column?



Thank You
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59848 Visits: 17947
nuchbcc (8/13/2013)
I tried the JOIN query separately and received the output that I needed but when I added it to the existing query, it failed.

I guess what I'm trying to ask is do I need to setup PK and FK? If yes do I create the FK from table1, set the PK table to be table2 pointing to StatusName column and FK table to be table1 pointing to StatusID column?

Thank You


No you are not required to establish a foreign key relationship between these tables. However if you do you would NOT have be on the StatusName column it would be on the value column.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Nooch
Nooch
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 72
Thank you for being patient.

To create a FK so the query output would replace the value with the StatusName, from SMS;
On table1 right-click on Keys, select New Foreign Key...
Click on Add and expand the "Tables And Columns Specification"
For "Primary key table", select the lookup table and StatusName column
For "Foreign key table", table1 is hardcoded, select the value column

Are the above steps correct?

Thank You
Sean Lange
Sean Lange
SSC Guru
SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)SSC Guru (59K reputation)

Group: General Forum Members
Points: 59848 Visits: 17947
nuchbcc (8/13/2013)
Thank you for being patient.

To create a FK so the query output would replace the value with the StatusName, from SMS;
On table1 right-click on Keys, select New Foreign Key...
Click on Add and expand the "Tables And Columns Specification"
For "Primary key table", select the lookup table and StatusName column
For "Foreign key table", table1 is hardcoded, select the value column

Are the above steps correct?

Thank You


You should probably read up on what a foreign key and why they are useful. I think you are confused about what they do. They are not some sort of magical tool to replace values or something. It is used to enforce data integrity between the two tables. The datatypes of the two columns MUST be the same.

http://www.w3schools.com/sql/sql_foreignkey.asp

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Nooch
Nooch
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 Visits: 72
I knew it couldn't be that easy. I thought I was doing something wrong in creating PK-FK. I'll do more reading. Thank you.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search