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)
 Invalid column name

Author  Topic 

Mondeo
Constraint Violating Yak Guru

287 Posts

Posted - 2009-03-18 : 10:53:15
[code]
SELECT prop.id,prop.datesubmitted,cust.forename,cust.surname,cust.tradingname,prop.quoteterms,prop.customerref,(SELECT top 1 vehicle from tblvehicles where propid=prop.id) as Vehicle FROM tblProposals prop
LEFT JOIN tblCustomers cust ON prop.customerID = cust.ID
WHERE prop.proposalstatus=2 AND prop.userid='test@test.com' AND (prop.customerref LIKE '%qash%' OR prop.id LIKE '%qash%' OR vehicle LIKE '%qash%')
[/code]

Invalid column name 'vehicle'

How can I make it work?
Thanks

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2009-03-18 : 11:12:29
Here's one way:

SELECT prop.id,prop.datesubmitted
,cust.forename,cust.surname
,cust.tradingname,prop.quoteterms
,prop.customerref
,v.vehicle
FROM tblProposals prop
LEFT JOIN tblCustomers cust ON prop.customerID = cust.ID
outer apply (
select top 1 v.vehicle
from tblVehicles v
where v.propid = prop.propid
order by v.vehicle
) v
WHERE prop.proposalstatus=2
AND prop.userid='test@test.com'
AND (prop.customerref LIKE '%qash%'
OR prop.id LIKE '%qash%'
OR v.vehicle LIKE '%qash%')


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -