无法绑定多部分标识符的原因

时间:2012-12-22 05:26:28

标签: sql tsql

错误 - 无法绑定多部分标识符“Grant.EmpID”。

查询 -

select emp.EmpID, 
COUNT(*) as CountRecords,
COUNT(GrantName) AS CountValues
From Employee as emp full join [Grant] as gr
on emp.EmpID = [Grant].EmpID 
-- This is the cause for the error ! Change it to gr.EmpID
group by emp.EmpID

为什么会出现此错误?我不能用真名和昵称来呼叫一个人吗?

2 个答案:

答案 0 :(得分:5)

你是别名[Grant]。换句话说,您说明从现在开始,[Grant]将被称为gr

使用ALIAS子句中的GROUP BY,而不是tableName。

SELECT emp.EmpID, 
       COUNT(*) as CountRecords,
       COUNT(GrantName) AS CountValues
FROM   Employee as emp 
       FULL JOIN [Grant] as gr
          on emp.EmpID = gr.EmpID  -- use the alias.
GROUP BY gr.EmpID                  -- use the alias.

这是SQL操作顺序

  • FROM clause
  • WHERE子句
  • GROUP BY子句
  • HAVING条款
  • SELECT条款
  • ORDER BY子句

答案 1 :(得分:0)

不,你不能,因为sql server不是人。想象一下,我们有一个引用自己的Employee表

select * 
from Employee Emp, Employee Mng
where Emp.ManagerID = Mng.EmployeeID

Mng和Emp是Employee的两个实例

所以,如果我选择

select * from Employee e, Employee

它将返回所有员工两次,因为我告诉员工一次以Employee的名义给我一个名字e(别名)

相关问题