1

我在存储过程中编写子查询以获取存储为 varchar 数据类型的订单之间的值。当我运行查询时,它显示:

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

我用谷歌搜索了一下,发现问题是因为在子查询中返回了超过 1 个值。

但在我的情况下,我需要位于给定输入之间的所有值。谁能告诉我我可以用什么方法来实现这一点..

代码:

SELECT ROW_NUMBER()OVER(
                        ORDER BY po.id) AS SNo ,
       pd.Copies AS Quantity,
       pd.EstUnitPrice AS UniPrice,
       pd.Copies*pd.EstUnitPrice AS Total,

  (SELECT value
   FROM BibContents
   WHERE bibid=pd.BibId
     AND sfld='a'
     AND tagno='245') AS Title,

  (SELECT value
   FROM BibContents
   WHERE bibid=pd.BibId
     AND sfld='a'
     AND tagno='020') AS 'ISSN/ISBN',

  (SELECT value
   FROM BibContents
   WHERE bibid=pd.BibId
     AND sfld='a'
     AND tagno='100')AS Author
FROM  [VibrantMas].[dbo].[PoDetails] AS pd
      INNER JOIN Porders AS po ON po.Id=pd.PoId
WHERE po.No BETWEEN '000021' AND '000024'
4

1 回答 1

2

引发错误的不是那个BETWEEN,而是 . 上的子选择之一BibContents

你有两个选择

  • 采取简单的路线并改变每SELECT value一个SELECT TOP 1 value
  • 找出其中一个子选择返回多条记录 的根本原因。
    • If it shouldn't return multiple records, you should add a unique constraint to BibContents preventing this from happening in the first place.
    • If multiple records can occur for a given PoDetails, you have to decide wich one you'd like to return.
    • If you want all of them returned, you'll have to change the subselects to proper joins.

My advice would be to save yourself from a maintenance nightmare and solve the root cause.

Working out another scheme for BibContents wouldn't hurt either. It looks like you've adopted the EAV model theme wich I think is a bad idea.

于 2013-02-20T06:55:18.687 回答