我正面临问题:
子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。
if和else如果条件正常,但else条件不正常。 并在SQL Server中显示问题:
alter proc spSearch 'Cook','205'
(
@SearchName Nvarchar(Max),
@AdminStatus nvarchar(max)
)
as
begin
Declare @SearchRecipeKey AS Nvarchar(Max)
Declare @SearchChefKey AS Nvarchar(Max)
SET @SearchRecipeKey = (SELECT Recipes.RecipeName FROM Recipes WHERE Recipes.RecipeName LIKE '%'+@SearchName+'%')
SET @SearchChefKey = (SELECT Chef.FirstName+' '+Chef.LastName as FullName FROM Chef WHERE (Chef.FirstName+' '+Chef.LastName) LIKE '%'+@SearchName+'%')
if((@SearchRecipeKey IS NOT NULL) and (@SearchChefKey IS NULL))
Begin
select Recipes.RecipeId, Recipes.RecipeName, 1 as Flag from Recipes
where Recipes.RecipeName LIKE '%'+@SearchName+'%'
and Recipes.AdminStatus=@AdminStatus ORDER BY Recipes.RecipeName ASC;
End
else if ((@SearchRecipeKey IS NULL) and (@SearchChefKey IS NOT NULL))
Begin
select Chef.ChefId,Chef.FirstName+' '+Chef.LastName as FullName, uinfo.UserType,Chef.EmailId, 2 as Flag from Recipes
inner join Chef on Recipes.ChefId = Chef.ChefId
full join Users uinfo on Chef.EmailId = uinfo.EmailId
where (Chef.FirstName+' '+Chef.LastName) LIKE '%'+@SearchName+'%'
and Recipes.AdminStatus=@AdminStatus ORDER BY FullName ASC;
End
else
Begin
select Recipes.RecipeId, Recipes.RecipeName,Chef.ChefId,Chef.FirstName+' '+Chef.LastName as FullName, uinfo.UserType,Chef.EmailId, 3 as Flag from Recipes
inner join Chef on Recipes.ChefId = Chef.ChefId
inner join Users uinfo on Chef.EmailId = uinfo.EmailId
where (Chef.FirstName+' '+Chef.LastName) LIKE '%'+@SearchName+'%' or Recipes.RecipeName LIKE '%'+@SearchName+'%'
and Recipes.AdminStatus=@AdminStatus ORDER BY Recipes.RecipeName, FullName ASC;
End
End
答案 0 :(得分:2)
您在查询中使用Select TOP 1
。
顶部1在查询中仅选择第一个值。
alter proc spSearch 'Cook','205'
(
@SearchName Nvarchar(Max),
@AdminStatus nvarchar(max)
)
as
begin
Declare @SearchRecipeKey AS Nvarchar(Max)
Declare @SearchChefKey AS Nvarchar(Max)
SET @SearchRecipeKey = (SELECT TOP 1 Recipes.RecipeName FROM Recipes WHERE Recipes.RecipeName LIKE '%'+@SearchName+'%')
SET @SearchChefKey = (SELECT TOP 1 Chef.FirstName+' '+Chef.LastName as FullName FROM Chef WHERE (Chef.FirstName+' '+Chef.LastName) LIKE '%'+@SearchName+'%')
if((@SearchRecipeKey IS NOT NULL) and (@SearchChefKey IS NULL))
Begin
select Recipes.RecipeId, Recipes.RecipeName, 1 as Flag from Recipes
where Recipes.RecipeName LIKE '%'+@SearchName+'%'
and Recipes.AdminStatus=@AdminStatus ORDER BY Recipes.RecipeName ASC;
End
else if ((@SearchRecipeKey IS NULL) and (@SearchChefKey IS NOT NULL))
Begin
select Chef.ChefId,Chef.FirstName+' '+Chef.LastName as FullName, uinfo.UserType,Chef.EmailId, 2 as Flag from Recipes
inner join Chef on Recipes.ChefId = Chef.ChefId
full join Users uinfo on Chef.EmailId = uinfo.EmailId
where (Chef.FirstName+' '+Chef.LastName) LIKE '%'+@SearchName+'%'
and Recipes.AdminStatus=@AdminStatus ORDER BY FullName ASC;
End
else
Begin
select Recipes.RecipeId, Recipes.RecipeName,Chef.ChefId,Chef.FirstName+' '+Chef.LastName as FullName, uinfo.UserType,Chef.EmailId, 3 as Flag from Recipes
inner join Chef on Recipes.ChefId = Chef.ChefId
inner join Users uinfo on Chef.EmailId = uinfo.EmailId
where (Chef.FirstName+' '+Chef.LastName) LIKE '%'+@SearchName+'%' or Recipes.RecipeName LIKE '%'+@SearchName+'%'
and Recipes.AdminStatus=@AdminStatus ORDER BY Recipes.RecipeName, FullName ASC;
End
End