子查询返回超过 1 个值.当子查询跟在 =,!=,<,<=,>,>= 之后或当子查询用作表达式时,这是不允许的

Subquery returned more than 1 value.This is not permitted when the subquery follows =,!=,lt;,lt;=,gt;,gt;= or when the subquery is used as an expression(子查询返回超过 1 个值.当子查询跟在 =,!=,lt;,lt;=,gt;,gt;= 之后或当子查询用作表达式时,这是不允许的) - IT屋-程序员软件开发技术
本文介绍了子查询返回超过 1 个值.当子查询跟在 =,!=,<,<=,>,>= 之后或当子查询用作表达式时,这是不允许的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程 select * from book table ,使用子查询我的查询是

I have a stored procedure that select * from book table , using sub query my query is

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)

我知道子查询是向主查询返回多个查询,但我不知道如何避免这个错误,谁能帮助我?

I know sub query is return more than one query to main query , But i don't know how to avoid this error, can any one help me ?

推荐答案

问题是这两个查询都返回了不止一行:

The problem is that these two queries are each returning more than one row:

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

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

You have two choices, depending on your desired outcome. You can either replace the above queries with something that's guaranteed to return a single row (for example, by using SELECT TOP 1), OR you can switch your = to IN and return multiple rows, like this:

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

这篇关于子查询返回超过 1 个值.当子查询跟在 =,!=,<,<=,>,>= 之后或当子查询用作表达式时,这是不允许的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
SSIS: Model design issue causing duplications - can two fact tables be connected?(SSIS:模型设计问题导致重复-两个事实表可以连接吗?)
SQL Server Graph Database - shortest path using multiple edge types(SQL Server图形数据库-使用多种边类型的最短路径)
Invalid column name when using EF Core filtered includes(使用EF核心过滤包括时无效的列名)
How should make faster SQL Server filtering procedure with many parameters(如何让多参数的SQL Server过滤程序更快)
How can I generate an entity–relationship (ER) diagram of a database using Microsoft SQL Server Management Studio?(如何使用Microsoft SQL Server Management Studio生成数据库的实体关系(ER)图?)