Re: Error using a derived table

From: Adam Machanic (amachanic_at_air-worldwide.nospamallowed.com)
Date: 03/29/04


Date: Mon, 29 Mar 2004 12:59:23 -0500

The problem is your CASE expression; it doesn't evaluate to anything. Try
to re-write your query without all of the nested SELECTs and the CASE
statement and instead put your filtration logic into a WHERE clause.

"James Rowland-Jones" <anonymous@discussions.microsoft.com> wrote in message
news:71BB5719-9C91-4CE4-9435-9863B17CF44B@microsoft.com...
> SQL Server is returning the following error :
>
> Server: Msg 107, Level 16, State 2, Line 4
> The column prefix 'u' does not match with a table name or alias name used
in the query.
>
> With this SQL Statement :
>
> declare @datasetuploadid int
> set @datasetuploadid = 1
>
> select
> ( case when u.otherregion is not null
> and u.resort is not null
> Then ( select d.regionid
> from ( select top 1 resort.name,max(resort.regionid)as 'regionid',count(*)
as 'counter'
> from ( select r.name as 'Resortname'
> ,r.regionid as 'RegionID'
> ,r.regiontypeid as 'RegionTypeID'
> from dbo.region r
> where name = u.resort
> and r.active = 1
> and r.isresort = 1
> union all
> select s.misspelledname as 'Resortname'
> ,s.regionid as 'RegionID'
> ,r1.regiontypeid as 'RegionTypeID'
> from dbo.similarname s
> join dbo.region r1
> on s.regionid = r1.regionid
> where misspelledname = u.Resort
> and r1.active = 1
> and r1.isresort = 1
> union all
> select a.aliasname as 'Resortname'
> ,a.regionid as 'RegionID'
> ,r2.regiontypeid as 'RegionTypeID'
> from dbo.alias a
> join dbo.region r2
> on a.regionid = r2.regionid
> where aliasname = u.Resort
> and r2.active = 1
> and r2.isresort = 1
> ) resort
> join dbo.childregion cr1
> on resort.regionid = cr1.childid
> where cr1.parentid IN ( select r.regionid as 'RegionID'
> from dbo.region r
> where name = u.OtherRegion
> and r.active = 1
> union all
> select s.regionid as 'RegionID'
> from dbo.similarname s
> join dbo.region r1
> on s.regionid = r1.regionid
> where misspelledname = u.OtherRegion
> and r1.active = 1
> union all
> select a.regionid as 'RegionID'
> from dbo.alias a
> join dbo.region r2
> on a.regionid = r2.regionid
> where aliasname = u.OtherRegion
> and r2.active = 1
> )
> and resort.active = 1
> and resort.isresort = 1
> and resort.name = u.resort
> group by resort.name,resort.regiontypeid
> order by resort.regiontypeid desc
> ) d
> where d.counter = 1
> )
> End
> )
> from dbo.uploadeditem u
> where u.datasetuploadid = @datasetuploadid
> and u.Resort_RegionID is null
> and u.statuscodeid is null
>
> I believe the error is due to the resort derived table not being able to
resolve the 'u' table alias. I have found a Q article on this subject
225492 but it was only for SQL 7. I am using SQL Server 2000 EE.
>
> So the question is am I doing something wrong or is this a bug?
>
> Any help would be greatly appreciated.
> James



Relevant Pages

  • Re: question about a query
    ... JOIN QuesNans AS B ON A.topicid = B.topicid ... CROSS JOIN (SELECT 1 AS n UNION ALL ... This is a crosstab query. ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: Indexes being improperly used when selecting data through a view
    ... I run the same query against the table directly and it looks ... What happens if you actually have the UNION ALL, ... With UNION SQL Server will have to do an operation ... Books Online for SQL Server 2005 at ...
    (comp.databases.ms-sqlserver)
  • Re: Establishing Precedence In ORDERBY Condition Causing Problems.
    ... in the query, prefix all your columns with aliases (or the table ... of the procedure to avoid duplicates it seems to have thrown off the ... Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx ...
    (comp.databases.ms-sqlserver)
  • Re: question about a query
    ... JOIN QuesNans AS B ON A.topicid = B.topicid ... CROSS JOIN (SELECT 1 AS n UNION ALL ... This is a crosstab query. ... Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books ... ...
    (comp.databases.ms-sqlserver)
  • Re: Establishing Precedence In ORDERBY Condition Causing Problems.
    ... tab4.login_date from (select distinct emailAddress from Users union ... I created the UserPrecedence table that describes the ordered ... ORDER BY CASE @sort ... The sp_ prefix is reserved for system procedures, and SQL Server first ...
    (comp.databases.ms-sqlserver)