gpt4 book ai didi

sql-server - 子查询返回超过 1 个值。当子查询后面有 =、!=、<、<=、>、>= 或子查询用作表达式时,不允许这样做

转载 作者:行者123 更新时间:2023-12-02 07:19:16 30 4
gpt4 key购买 nike

我有一个存储过程,从图书表中选择*,使用子查询我的查询是

USE [library]
GO

/****** Object: StoredProcedure [dbo].[report_r_and_l] Script Date: 04/17/2013 12:42:39 ******/

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[report_r_and_l]
@fdate date,
@tdate date,
@key varchar(1)
as

if(@key='r')

select *
from dbo.books
where isbn =(select isbn from dbo.lending where (act between @fdate and @tdate) and (stat ='close'))

else if(@key='l')

select *
from dbo.books
where isbn =(select isbn from dbo.lending where lended_date between @fdate and @tdate)

我知道子查询会向主查询返回多个查询,但我不知道如何避免此错误,有人可以帮助我吗?

最佳答案

问题是这两个查询都返回多于一行:

select isbn from dbo.lending where (act between @fdate and @tdate) and (stat ='close')
select isbn from dbo.lending where lended_date between @fdate and @tdate

您有两种选择,具体取决于您想要的结果。您可以将上述查询替换为保证返回单行的内容(例如,使用 SELECT TOP 1),或者您可以切换 = IN 并返回多行,如下所示:

select * from dbo.books where isbn IN (select isbn from dbo.lending where (act between @fdate and @tdate) and (stat ='close'))

关于sql-server - 子查询返回超过 1 个值。当子查询后面有 =、!=、<、<=、>、>= 或子查询用作表达式时,不允许这样做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16053907/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com