-1

我有以下代码 -

declare @userType int
declare @custIdOfUser int

select 
    @userType = UserTypeId, @custIdOfUser = CustomerId 
from 
    customers c with (nolock)
left outer join 
    Sites s with (nolock) on c.CustomerId = s.CustomerId 
left outer join 
    customers rc on rc.CustomerId = c.ResellerId 
where 
    c.[name] is not null 
    and (c.customerId is null or rc.CustomerId = c.CustomerId) 
    and c.IsActive != ''
    and (@userType = 1 OR @userType = 2 OR 
           (c.customerId in ((SELECT u.CustomerId, ur.RoleId 
                              FROM Users u
                              INNER JOIN UserRoles ur ON ur.UserId = u.UserId AND ur.RoleId = 39
                              INNER JOIN Customers c ON ur.CustomerId = c.CustomerID 
                              WHERE u.UserId = 28 or u.UserTypeId = 3 or u.UserTypeId = 4 
                                and c.customerId in 
                                    (SELECT c.CustomerId FROM Customers c 
                                     WHERE c.resellerId = u.CustomerId )))))

它给了我以下错误消息-

Msg 116, Level 16, State 1, Line 10
当子查询没有用 EXISTS 引入时,只能在选择列表中指定一个表达式。
消息 207,级别 16,状态 1,第 3 行
无效的列名称“UserTypeId”。
消息 209,级别 16,状态 1,行 3
不明确的列名称“客户 ID”。

4

1 回答 1

2
  1. 不明确的列名称“CustomerId”

    你得到这个是因为你在选择CustomerId时没有指定从哪个表中获取它。此列名称存在于您的 3 个表(UserRolesSitesCustomers上,其中一个您要加入两次(Customers cCustomers rc)。您应该使您的选择语句更明确,例如

    select 
      @custIdOfUser = c.CustomerId
    
  2. 列名“UserTypeId”无效

    您的任何表上都不存在此列。

  3. 不使用 EXISTS 引入子查询时,选择列表中只能指定一个表达式

    - 因为你有这个:

    c.customerId in ((SELECT u.CustomerId, ur.RoleId 
    

    应该是这样的:

    c.customerId in ((SELECT u.CustomerId
    
于 2013-11-08T17:16:35.310 回答