帮助调试sql查询:我在哪里有额外的(或)?

时间:2010-10-06 17:24:19

标签: sql sql-server select

此查询有效:

SELECT     u.UserId, u.GroupId, u.username,
            gp.PreferenceId, gp.Properties, gp.Override
        FROM          dbo.UserTable u 
        Inner JOIN
            dbo.GroupPreferences gp ON gp.GroupID = u.GroupId
        WHERE      (u.UserName = 'myUserId')

但是另一个查询中的相同查询却没有。我收到错误“无效的列名'GroupId'”和“无效的列名'UserId'”。我想有一个额外的(或)某个地方;请帮帮我找到它!

外部查询:

SELECT     coalesce(t1.PreferenceId,up.preferenceId) 
        as preferenceId, CASE t1.override WHEN 1 THEN COALESCE 
        (up.properties, t1.properties) 
        When 0 then t1.properties   
        ELSE up.properties END AS Properties
    FROM         
        (SELECT     u.UserId, u.GroupId, u.username,
            gp.PreferenceId, gp.Properties, gp.Override
        FROM          dbo.UserTable u 
        Inner JOIN
            dbo.GroupPreferences gp ON gp.GroupID = u.GroupId
        WHERE      (u.UserName = 'myUserId')) t1 
    full OUTER JOIN
        (select preferenceId, properties from UserPreferences u
        inner join userTable t on u.userId = t.userId and u.GroupId = 
        t.GroupId where userName = 'myUserId') up 
    ON t1.GroupId = up.GroupID AND t1.UserId = up.UserId
        AND t1.PreferenceId = up.PreferenceId

2 个答案:

答案 0 :(得分:5)

您从内联视图别名GroupId引用了列UserIdup,但它的选择列表中不包含这些列。

更改第14行以包含这些列:

(select t.GroupId, t.userId, preferenceId, properties from UserPreferences u

答案 1 :(得分:3)

(select preferenceId, properties from UserPreferences u
        inner join userTable t on u.userId = t.userId and u.GroupId = 
        t.GroupId where userName = 'myUserId') up 

没有up.GroupID或up.UserId

ON t1.GroupId = up.GroupID AND t1.UserId = up.UserId