查询在用作表达式

时间:2015-06-16 05:39:35

标签: c# mysql sql asp.net stored-procedures

我有一张表

tblTimeSlotInformation (用户名,EmpName,日期(日期),日期,开始时间(时间(7)),结束时间(时间(7)),TimeSlot,主题,课堂无)

如果StartingTime位于已存储的StartingTime和EndingTime之间,则无法插入数据。例如:13.30(StartingTime)到14.30(EndingTime)时间存储在数据库中然后用户尝试将13:45(StartingTime)插入到15:45(EndingTIme)但是消息显示“这些时间段已经存在”

存储过程

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 错误提示时 “子查询返回的值超过1。当子查询跟随=,!=,&lt;,&lt; =,&gt;,&gt; =或子查询用作表达式时,不允许这样做

我该如何撰写查询?

2 个答案:

答案 0 :(得分:1)

  

&#34;错误:子查询返回的值超过1。

导致错误的可疑陈述

set @NewTime=(select StartingTime from tblTimeSlotDetails)  

由于查询返回的多个结果无法分配给单个变量,即@NewTime您可能需要获取maximum时间的行,而这可能需要使用max( )功能。

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

答案 1 :(得分:0)

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

只需更新以下行代码,然后根据您的要求获得结果。

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