Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 存储过程返回多个值_Sql_Sql Server_Sql Server 2008 - Fatal编程技术网

Sql 存储过程返回多个值

Sql 存储过程返回多个值,sql,sql-server,sql-server-2008,Sql,Sql Server,Sql Server 2008,在执行此过程时,它会重新调用子查询返回的值超过1。当子查询在=、!=、=或者当子查询用作表达式时。问题的直接原因是以下语句: ALTER PROCEDURE [dbo].[sp_checktime] ( @Booking_Date date , @Stime time(7) , @Etime time(7) , @Room varchar(50), @res int output ) AS BEGIN IF (@Stime=(select Start_Time

在执行此过程时,它会重新调用子查询返回的值超过1。当子查询在=、!=、=或者当子查询用作表达式时。

问题的直接原因是以下语句:

ALTER PROCEDURE [dbo].[sp_checktime]
( @Booking_Date date ,
    @Stime time(7) ,
    @Etime time(7) ,
    @Room varchar(50),
    @res int output
 )
AS
BEGIN
IF (@Stime=(select Start_Time from Booking_master where Booking_Date=@Booking_Date and Room=@Room) or @Stime>=(select Start_Time from Booking_master) and @Stime<=(select End_Time from Booking_master where Booking_Date=@Booking_Date and Room=@Room))
BEGIN
        set @res=0
END

ELSE    
    BEGIN
        set @res=1
    END

END
子查询似乎返回多行。自反修复是将其替换为in:

在你的情况下,我认为你最好修正逻辑,这样你只有一个逻辑,如果存在的话。陈述比如:

@Stime in (select Start_Time from Booking_master where Booking_Date=@Booking_Date and Room=@Room)

子查询只允许有一个结果。尝试将TOP1添加到每个子查询中,尽管问题可能与第二个子查询无关

if (exists (select 1
            from Booking_master bm
            where Booking_Date = @Booking_Date and Room = @Room and
                  (@Stime = Start_Time or
                   (@Stime >= StartTime and @Stime <= EndTime)
                  )
           )
   )

旁注:存储过程不应使用sp_uu前缀。微软已经这样做了,而且你确实有可能在将来的某个时候发生名称冲突。最好只是简单地避免使用sp_u,并使用其他东西作为前缀——或者根本不使用前缀!在子查询中添加前1。您的意思是Ifexistsy您的查询吗
if (exists (select 1
            from Booking_master bm
            where Booking_Date = @Booking_Date and Room = @Room and
                  (@Stime = Start_Time or
                   (@Stime >= StartTime and @Stime <= EndTime)
                  )
           )
   )
ALTER PROCEDURE [dbo].[sp_checktime]
( @Booking_Date date ,
    @Stime time(7) ,
    @Etime time(7) ,
    @Room varchar(50),
    @res int output
 )
AS
BEGIN
IF (
    @Stime = (select Top 1 Start_Time 
              from Booking_master 
              where Booking_Date=@Booking_Date and Room=@Room) 
 or @Stime >= (select Top 1 Start_Time 
               from Booking_master) 
and @Stime <= (select Top 1 End_Time 
               from Booking_master 
               where Booking_Date=@Booking_Date and Room=@Room))
BEGIN
        set @res=0
END

ELSE    
    BEGIN
        set @res=1
    END

END