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)
 what is the best way to do this

Author  Topic 

johnstern
Yak Posting Veteran

67 Posts

Posted - 2007-11-12 : 16:53:53
I am trying to decide which way is the best/efficient way.

I have a user table and a usertypelookup table, the relationship is one to one. ( this is only a sample of my actual complex query )

in the first example, for every row I am going to the table and getting the information

in the second one I am join all the tables

what would you recommed

thank you

select UserID,
(Select UserDescription from UserTypeLookup where UserTypeId = e.UserTypeId) as UserDescription

from EmploymentItem e
where UserRecordId =2




select UserID,
UserDescription

from EmploymentItem e
join UserTypeLookup l on e.UserTypeId= l.UserTypeId
where UserRecordId =2

edit: added comma to first statement

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-11-12 : 16:57:31
I'd recommend the second option as the first option will break if ever the inner query produces more than one row!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-11-12 : 17:14:18
The first option is not even valid syntax.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Vinnie881
Master Smack Fu Yak Hacker

1231 Posts

Posted - 2007-11-13 : 01:21:33
Option 2
Go to Top of Page
   

- Advertisement -