TSQL前1个参数分配问题

时间:2012-04-09 17:20:02

标签: tsql subquery user-defined-functions mode

我正在尝试获取一组zipcodes的MODE平均值(包含特定上传设置中大多数条目的邮政编码)。我想使它成为标量函数,而不是存储过程。

为什么这样做:

Select Top 1 LocationZip
from UploadSetZipCodeCount
where WorkOrderSet = 31
Order by ZipCount desc

但不是这样:

Select @setbasezip= Top 1 LocationZip
from UploadSetZipCodeCount
where WorkOrderSet = 31
Order by ZipCount desc

我的声明类型对于LocationZip字段是正确的。

1 个答案:

答案 0 :(得分:5)

语法问题...... select top 1应该在变量赋值之前出现:

Select Top 1 @setbasezip = LocationZip
from UploadSetZipCodeCount
where WorkOrderSet = 31
Order by ZipCount desc