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
 select inside a select statement

Author  Topic 

insanepaul
Posting Yak Master

178 Posts

Posted - 2009-02-03 : 06:17:42
I have a select command that should return 2 columns as below but the second column (lfieldtype) references another table 'lufieldtype' which I'm stuck on an error message. Rather than getting the ID I want to get the corresponding sDescription from that table. I can't figure out what i'm doing wrong. If I just select the first column I get the correct results.
Example of the 2 tables:
Table lufieldtype

ID sDescription
1 Readonly
2 Writeonly
3 Text

Table strtoolinfo
stext lfieldtype
sometext... 1
sometext... 2
sometext... 1
etc..


select stext,
(SELECT lfieldtype
FROM lufieldtype
WHERE (lfieldtypeid = strTool.lfieldtype)) as hhh
,
loptionid
FROM strtoolinfo
WHERE ltoolinfoid not in (select ltoolinfoid from strlnktooltoolinfo WHERE stoolguid = '{79be4f69-6081-4029-8fe7-802048030b5b}')

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2009-02-03 : 06:20:19
quote:
Originally posted by insanepaul

I have a select command that should return 2 columns as below but the second column (lfieldtype) references another table 'lufieldtype' which I'm stuck on an error message. Rather than getting the ID I want to get the corresponding sDescription from that table. I can't figure out what i'm doing wrong. If I just select the first column I get the correct results.
Example of the 2 tables:
Table lufieldtype

ID sDescription
1 Readonly
2 Writeonly
3 Text

Table strtoolinfo
stext lfieldtype
sometext... 1
sometext... 2
sometext... 1
etc..


select stext,
(SELECT lfieldtype
FROM lufieldtype
WHERE (lfieldtypeid = strtoolinfo.lfieldtypeid)) as hhh
,
loptionid
FROM strtoolinfo
WHERE ltoolinfoid not in (select ltoolinfoid from strlnktooltoolinfo WHERE stoolguid = '{79be4f69-6081-4029-8fe7-802048030b5b}')




Jai Krishna
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-03 : 06:21:24
try like this rather than subquery
select s.stext, l.lfieldtype as hhh,
s.loptionid
FROM strtoolinfo as s
inner join lufieldtype l on l.lfieldtypeid = s.lfieldtype
WHERE s.ltoolinfoid not in (select ltoolinfoid from strlnktooltoolinfo WHERE stoolguid = '{79be4f69-6081-4029-8fe7-802048030b5b}')
Go to Top of Page

insanepaul
Posting Yak Master

178 Posts

Posted - 2009-02-03 : 06:23:34
wow that was quick and correct...Thanks - was driving me insane
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-02-03 : 06:24:25
quote:
Originally posted by insanepaul

wow that was quick and correct...Thanks - was driving me insane


welcome
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-03 : 09:02:36
try this too and see if performance is any better

select s.stext, l.lfieldtype as hhh,
s.loptionid
FROM strtoolinfo as s
inner join lufieldtype l on l.lfieldtypeid = s.lfieldtype
WHERE NOT EXISTS (select 1 from strlnktooltoolinfo WHERE ltoolinfoid=s.ltoolinfoid
AND stoolguid = '{79be4f69-6081-4029-8fe7-802048030b5b}')
Go to Top of Page
   

- Advertisement -