当查询用作表达式时包含多个值

Query contain more then one value when it used as a expression

我有一个Table

tblTimeSlotInformation(用户名, EmpName, 日期(date), Day, StartingTime(time(7)), EndingTime(time(7)), TimeSlot, Topic, ClassroomNo)

如果StartingTime 介于已存储的StartingTime 和EndingTime 之间,则无法插入数据。例如:13.30(StartingTime) 到 14.30(EndingTime) 时间存储在数据库中然后用户尝试将 13:45(StartingTime) 插入到 15:45(EndingTIme) 但消息显示 "These time slot is already exist"

存储过程

CREATE Proc spStoreTimeSlotDetails        
@Username nvarchar(50),        
@EmpName nvarchar(50),        
@Date date,        
@Day nvarchar(50),        
@ST time(7),        
@ET time(7),        
@TimeSlot nvarchar(50),        
@Topic nvarchar(50),        
@ClassroomNo int         
as          
begin          
--Declare @count int          
Declare @ReturnCode int        
Declare @MinuteDiff int       
DECLARE @i int = 1      
Declare @NewTime time(7)      
Declare @CRN int    
Declare @D date    

set @ReturnCode=0  
set @MinuteDiff=(select DATEDIFF(MINUTE,StartingTime,EndingTime) from tblTimeSlotDetails)      
set @NewTime=(select StartingTime from tblTimeSlotDetails)  
set @CRN=(Select ClassroomNo from tblTimeSlotDetails)    
set @D=(select [Date] from tblTimeSlotDetails)    

WHILE @i <= @MinuteDiff   
BEGIN      
 set @NewTime=(select DATEADD(MINUTE,1,@NewTime) from tblTimeSlotDetails)    
 if(@CRN=@ClassroomNo and @D=@Date and @NewTime=@ST and @ST=)          
 Begin          
  set @ReturnCode=-1   
  break         
 end          
 else          
 Begin          
  set @ReturnCode=1         
 end          
 SET @i = @i + 1      
End      
if(@ReturnCode=1 or @ReturnCode=0)    
begin   
 Insert into tblTimeSlotDetails values(@Username,@EmpName,@Date,@Day,@ST,@ET,@TimeSlot,@Topic,@ClassroomNo)          
end    
select @ReturnCode as ReturnValue   
end 

但是每次当我运行 我的Asp.net c# application 错误提示 "Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression"

我应该如何编写查询?

"Error: Subquery returned more than 1 value.

疑似语句导致错误

set @NewTime=(select StartingTime from tblTimeSlotDetails)  

您遇到问题是因为查询的多个结果 return 无法分配给单个变量,即 @NewTime 您可能需要获取具有 maximum 时间的行可以使用 max() 函数。

set @NewTime=(select Max(StartingTime) from tblTimeSlotDetails)  

if(@CRN=@ClassroomNo and @D=@Date and @NewTime=@ST)

只需更新以下行代码即可获得您希望的结果。

if(@CRN=@ClassroomNo and @D=@Date and @NewTime=@ST and @ST=)