"Must declare the scalar variable" 在声明 table 的 where 子句中
"Must declare the scalar variable" inside where clause for declared table
我正在尝试以下:
--create temporary table
declare @tempemp TABLE(
salary int
);
--get maximum salary of employees in sbi
insert into @tempemp
select MAX(salary) as 'salary' from employee where company='sbi';
现在我想获取收入高于 sbi 所有员工的所有员工的姓名。我试过这个:
select * from employee where employee.salary > @tempemp.salary;
但这给了我错误:
Must declare the scalar variable "@tempemp".
虽然这工作得很好:
select * from @tempemp;
与前两个(声明和插入)查询一起执行时。
如果没有 selecting
,您不能在 where
子句中将 tablename
用作 alias
。你必须从 table 到 select。试试这个。
select * from employee
where employee.salary > (select salary from @tempemp)
不要使用 table,使用变量:
declare @salary int;
select @salary = MAX(salary)
from employee
where company = 'sbi';
select *
from employee
where employee.salary > @salary;
如果您确实使用 table,则需要子选择:
select *
from employee
where employee.salary > (select salary from @tempemp.salary);
我假设您出于某些原因单独存储该值,因为您可以这样做:
select *
from employee
where employee.salary > (select max(salary) from employee where company = 'sbi');
我正在尝试以下:
--create temporary table
declare @tempemp TABLE(
salary int
);
--get maximum salary of employees in sbi
insert into @tempemp
select MAX(salary) as 'salary' from employee where company='sbi';
现在我想获取收入高于 sbi 所有员工的所有员工的姓名。我试过这个:
select * from employee where employee.salary > @tempemp.salary;
但这给了我错误:
Must declare the scalar variable "@tempemp".
虽然这工作得很好:
select * from @tempemp;
与前两个(声明和插入)查询一起执行时。
如果没有 selecting
,您不能在 where
子句中将 tablename
用作 alias
。你必须从 table 到 select。试试这个。
select * from employee
where employee.salary > (select salary from @tempemp)
不要使用 table,使用变量:
declare @salary int;
select @salary = MAX(salary)
from employee
where company = 'sbi';
select *
from employee
where employee.salary > @salary;
如果您确实使用 table,则需要子选择:
select *
from employee
where employee.salary > (select salary from @tempemp.salary);
我假设您出于某些原因单独存储该值,因为您可以这样做:
select *
from employee
where employee.salary > (select max(salary) from employee where company = 'sbi');