SQL视图查询“子查询返回的值超过1个”问题

问题描述 投票:1回答:2

我一直在使用一个查询视图,该视图也用于SSRS报告,并且每月运行2年以上没有任何问题。这是查询:

SELECT  
    DISTINCT 
    Code= D.LocalityCode,
    [District Name]=D.LocalityName,
    Status=UPPER(D.Status),
    [Company No]=F.CompanyNumber,
    [CompanyB No]=F.CompanyBNumber,
    [Incorp date]=case when F.DateIncorp = '1900-01-01 00:00:00.000' Then NULL else F.DateIncorp end,
    [CompanyB date]=case when F.DateCompanyB = '1900-01-01 00:00:00.000' Then NULL else F.DateCompanyB end,
    Salutation=CASE WHEN D.Status = 'LAPSED' THEN 'S.A. OF ' + UPPER(E.InstitutionName) WHEN D.Status = 'ACTIVE' THEN 'OF ' + UPPER(E.InstitutionName) ELSE '' END,
    [Sec Code]=dbo.SecCodeLookup(E.LocalityID),
    [Treas Code]=dbo.TreCodeLookup(E.LocalityID),
    [Sec Name]=dbo.SecNameLookup(E.LocalityID),
    [Treas Name]=dbo.TreNameLookup(E.LocalityID),
    [S Building]=(SELECT Building FROM SecAddressLookup(E.LocalityID)),
    [S Street]=(SELECT Street FROM SecAddressLookup(E.LocalityID)),
    [S Town]=(SELECT Town FROM SecAddressLookup(E.LocalityID)),
    [S Postcode]=(SELECT Postcode FROM SecAddressLookup(E.LocalityID)),
    [T Building]=(SELECT Building FROM TreAddressLookup(E.LocalityID)),
    [T Street]=(SELECT Street FROM TreAddressLookup(E.LocalityID)),
    [T Town]=(SELECT Town FROM TreAddressLookup(E.LocalityID)),
    [T Postcode]=(SELECT Postcode FROM TreAddressLookup(E.LocalityID))
FROM
    tblLocality D
    LEFT JOIN tblInstitution E ON D.LocalityID = E.LocalityID
    LEFT JOIN tblCompanyDetail F ON E.CompanyDetailID = F.CompanyDetailID
    LEFT JOIN tblInstitutionHistory G ON E.InstitutionID = G.InstitutionID
    LEFT JOIN tblStatusDetail H ON G.StatusDetailID = H.StatusDetailID

WHERE D.LocalityID NOT IN ('5150','5156','5149')    


GO

今天我尝试了不同的时间,但我一直收到此错误:

消息512,级别16,状态1,第4行

子查询返回了多个值。当子查询遵循=,!=,,> =或将子查询用作表达式时,不允许这样做。

当我使用查询生成SSRS报告时,它返回:

报告处理期间发生错误。 (rsProcessingAborted)无法读取数据集DataSet1的下一个数据行。 (rsErrorReadingNextDataRow)有关此错误的更多信息,请导航到本地服务器计算机上的报表服务器,或启用远程错误

在日志中(在\ Reporting Services \ LogFiles中):

ui!ReportManager_0-3!1280!01/07/2020-10:10:31:: i INFO: AccessibleTablix value not specified in config file.
library!ReportServer_0-4!1114!01/07/2020-10:10:31:: Call to GetSystemPropertiesAction().
library!ReportServer_0-4!1280!01/07/2020-10:10:32:: Call to GetPermissionsAction(/My Office/Bulk Exports/DistrictExport).
library!ReportServer_0-4!fd8!01/07/2020-10:10:32:: Call to GetSystemPropertiesAction().
library!ReportServer_0-4!fdc!01/07/2020-10:10:32:: Call to GetPropertiesAction(/My Office/Bulk Exports/DistrictExport, PathBased).
library!ReportServer_0-4!fdc!01/07/2020-10:10:32:: Call to GetSystemPermissionsAction().
library!ReportServer_0-4!fdc!01/07/2020-10:10:32:: Call to GetSystemPropertiesAction().
library!ReportServer_0-4!fdc!01/07/2020-10:10:33:: i INFO: RenderForNewSession('/My Office/Bulk Exports/DistrictExport')
processing!ReportServer_0-4!fdc!01/07/2020-10:13:18:: e ERROR: Throwing Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: , Microsoft.ReportingServices.ReportProcessing.ReportProcessingException: Cannot read the next data row for the dataset DataSet1. ---> System.Data.SqlClient.SqlException: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlDataReader.HasMoreRows()
   at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)
   at Microsoft.ReportingServices.DataExtensions.DataReaderWrapper.Read()
   at Microsoft.ReportingServices.DataExtensions.MappingDataReader.GetNextRow()
   --- End of inner exception stack trace ---;

我还检查了数据,并在获取第135行时出错,但这与1年前的数据完全相同。

sql sql-server ssrs-2008-r2
2个回答
0
投票

1)如果在表tblLocality中未将LocalityID字段定义为主键,则运行下面的查询可能会告诉您罪魁祸首:

select LocalityID, count(*) from tblLocality having count(*) >1

2)如果将字段LocalityID定义为表tblLocality中的主键,则表示它是唯一的,问题出在将函数E.LocalityID作为参数的一个函数中。因为我们无权访问这些功能的代码,所以我们可以看到导致它的原因和原因。

检查第一点和第二点,您将看到问题。


0
投票

不确定是否无法访问表或UDF,但不能100%确定。但是我有一个预感,地址函数返回的记录多于1条。

然后,您可以将TOP 1包裹在一个外部应用程序中。

SELECT  
    DISTINCT 
    Code= D.LocalityCode,
    [District Name]=D.LocalityName,
    Status=UPPER(D.Status),
    [Company No]=F.CompanyNumber,
    [CompanyB No]=F.CompanyBNumber,
    [Incorp date]=case when F.DateIncorp = '1900-01-01 00:00:00.000' Then NULL else F.DateIncorp end,
    [CompanyB date]=case when F.DateCompanyB = '1900-01-01 00:00:00.000' Then NULL else F.DateCompanyB end,
    Salutation=CASE WHEN D.Status = 'LAPSED' THEN 'S.A. OF ' + UPPER(E.InstitutionName) WHEN D.Status = 'ACTIVE' THEN 'OF ' + UPPER(E.InstitutionName) ELSE '' END,
    [Sec Code] = dbo.SecCodeLookup(E.LocalityID),
    [Treas Code] = dbo.TreCodeLookup(E.LocalityID),
    [Sec Name] = dbo.SecNameLookup(E.LocalityID),
    [Treas Name] = dbo.TreNameLookup(E.LocalityID),
    [S Building] = SecAddress.Building,
    [S Street] = SecAddress.Street,
    [S Town] = SecAddress.Town,
    [S Postcode] = SecAddress.Postcode,
    [T Building] = TreAddress.Building,
    [T Street] = TreAddress.Street,
    [T Town] = TreAddress.Town,
    [T Postcode] = TreAddress.Postcode
FROM tblLocality D
LEFT JOIN tblInstitution E ON D.LocalityID = E.LocalityID
LEFT JOIN tblCompanyDetail F 
  ON E.CompanyDetailID = F.CompanyDetailID
LEFT JOIN tblInstitutionHistory G 
  ON E.InstitutionID = G.InstitutionID
LEFT JOIN tblStatusDetail H 
  ON G.StatusDetailID = H.StatusDetailID
OUTER APPLY
(
    SELECT TOP 1 l.* 
    FROM SecAddressLookup(E.LocalityID) l
    ORDER BY l.Building
) AS SecAddress
OUTER APPLY
(
    SELECT TOP 1 l.* 
    FROM TreAddressLookup(E.LocalityID) l
    ORDER BY l.Building
) AS TreAddress
WHERE D.LocalityID NOT IN ('5150','5156','5149')    
© www.soinside.com 2019 - 2024. All rights reserved.