我正在尝试创建一个存储过程,该过程返回Northwind数据库中最近n个月雇佣的员工数。这就是我得到的:
CREATE PROC spGetEmployeeCountByDateHire
@num1 int
,@num2 int
,@EmployeeCount int output
WITH Encryption
AS
BEGIN
select @EmployeeCount = COUNT(*),
DATEDIFF(DAY, HireDate, getdate()) AS Diff
FROM Employees where DATEDIFF(DAY, HireDate, getdate()) between @num1 and @num2
ORDER BY HireDate Desc END
我得到了这个错误:消息141,级别15,状态1,过程spGetEmployeeCountByDateHire,第8行为变量赋值的SELECT语句不能与数据检索操作结合使用。
有人可以帮我理解我做错了吗?
答案 0 :(得分:0)
错误正是告诉您不能将count(*)
分配给变量并同时选择datediff操作的原因。通过从选择列表
DATEDIFF(DAY, HireDate, getdate()) AS Diff
部分,更改您的选择语句,如下所示
select @EmployeeCount = COUNT(*)
FROM Employees
where DATEDIFF(DAY, HireDate, getdate()) between @num1 and @num2