查询出了什么问题?
select *
from ERP.dbo.Table
where storeID in (case
when @Designation = 80
then
(select storeID
from ERP.dbo.Store
where companyID = @CompanyID
and isMonitoringAvailable = 1
and storeID = ISNULL(@StoreId, storeID))
else
(select storeID
from ERP.dbo.EmployeeRole
where employeeID = @EmployeeId
and storeID = ISNULL(@StoreId,storeID) )
end)
我收到此错误:
Msg 512,第16级,状态1,第46行
子查询返回的值超过1。子查询时不允许这样做 遵循=,!=,<,<=,>,> =或将子查询用作表达式时。
答案 0 :(得分:0)
您可以通过以下方式重新编写条件
select * from ERP.dbo.EmployeeRole where
( storeID in (
(select storeID
from ERP.dbo.Store
where companyID=@CompanyID and isMonitoringAvailable=1
and storeID=ISNULL(@StoreId,storeID)
) and @Designation=80
) Or
storeID in (Select storeID
from ERP.dbo.EmployeeRole
where employeeID=@EmployeeId
and storeID=ISNULL(@StoreId,storeID)
)
您的查询返回错误,因为然后,否则结果生成多个storeid
答案 1 :(得分:0)
UNION ALL
期望expressions,因此出现错误。从性能角度考虑,替代方法是使用select * from ERP.dbo.EmployeeRole
where @Designation=80 AND storeID in (
select storeID
from ERP.dbo.Store
where companyID=@CompanyID and isMonitoringAvailable=1
and storeID=ISNULL(@StoreId,storeID)
)
UNION ALL
select * from ERP.dbo.EmployeeRole
where @Designation<>80 AND storeID in (
select storeID
from ERP.dbo.EmployeeRole
where employeeID=@EmployeeId
and storeID=ISNULL(@StoreId,storeID)
)
运算符,如下所示:
IF-ELSE
另一种方法是使用UNION ALL
,但是if (context.Users.Where(u => u.Username.Equals((string) value, StringComparison.OrdinalIgnoreCase)).Count() >= 1)
{
return new ValidationResult("Username is already taken", new [] { "Username" });
}
会更好。