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.
| Author |
Topic |
|
KunalPawar
Starting Member
1 Post |
Posted - 2008-02-28 : 10:18:51
|
| Hello all,I have following tables asListOptionID Int PKName VarcharUserEducation Id int PKEducationLevel Int FKEducationArea Int FkCenter VarcharHere EducationLevel and EducationArea are foreign keys from ListOption table.now while selecting data from UserEducation table i want that FK Id's should be replaced with the Name from ListOption table.Here i had created one function for returning the value of the Name from ListOption table.CREATE FUNCTION [dbo].[GetListOptionDetails](@fListOptionID int)RETURNS nvarchar(255)ASBEGINRETURN (SELECT Name from ListOption where Id = @fListOptionID)ENDSELECT id, starSuiteUserId, EducationLevel, dbo.[GetListOption](EducationLevel) as EducationAreaDetails, EducationArea, dbo.[GetListOption](EducationArea) as EducationAreaDetails,CenterFROM UserEducationWHERE id = @id;i had return this query but i don't feel this is an right way as i had this case in most of the tables in my DB.Regards,Kunal Pawar |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2008-02-28 : 10:32:03
|
| Table design seems to be wrong.You should have two tables - one for EducationLevel and another for EducationArea:EducationLevelEduLevelID Int PKEduLevelName VarcharEducationArea EduAreaID Int PKEduAreaName Varcharand then you can have UserEducation like this:UserEducation Id int PKEduLevelId Int FKEduAreaID Int FkCenter Varcharand then you can simply join these three tables based on IDs.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-28 : 10:48:29
|
Or you can take query twice with table and retrieve results:-SELECT ue.ID, l1.Name AS EducationLevel, l2.Name AS EducationArea, ue.CenterFROM UserEducation ueINNER JOIN ListOption l1ON l1.ID=ue.EducationLevelINNER JOIN ListOption l2ON l2.ID=ue.EducationArea |
 |
|
|
|
|
|