@Weeks附近的语法不正确

时间:2014-07-30 18:26:26

标签: sql sql-server

这个查询有什么问题吗?我想得到不同数量的热门记录

OpenConnection();
query.CommandText = @"SELECT TOP @weeks b.name, b.lastname, [Date] 
                      FROM [Scheduling_Employee].[dbo].[Date] AS a 
                      JOIN [Scheduling_Employee].[dbo].[Employee] AS b ON a.[Employee_Id] = b.id";
query.Parameters.Clear();
query.Parameters.AddWithValue("@weeks", (weeknumber*7));

SqlDataReader reader = query.ExecuteReader();

错误是:

@Weeks附近的语法不正确

1 个答案:

答案 0 :(得分:5)

TOP的语法是TOP x,其中x是固定数字,或者

SELECT TOP (@variable) 

如果你想指定一个变量(你需要把它放在括号中)。

所以试试这段代码:

query.CommandText = @"SELECT TOP (@weeks) b.name, b.lastname, [Date] 
                      FROM [Scheduling_Employee].[dbo].[Date] AS a 
                      JOIN [Scheduling_Employee].[dbo].[Employee] AS b ON a.[Employee_Id] = b.id";

你应该没事。