0

我正在处理一个使用包含一些参数的存储过程的 SSRS 报告。

我遇到问题的参数之一是因为我可以选择多个项目(guid 值)并且它们超过 4000+ 逗号分隔值。

存储过程:proc_GetCompanyRecord

=========================================

CREATE PROCEDURE [dbo].[proc_GetCompanyRecord]
    (
    @Year AS VARCHAR(MAX) = NULL,
    @CoreCompanyId AS VARCHAR(MAX) = NULL,
    @CompanyId AS VARCHAR(MAX) = NULL,
    @OwnerId VARCHAR(MAX) = NULL
    )

--THE REST OF MY QUERY HERE WHICH PULLS ALL OF THE NEEDED COLUMNS

WHERE CoreCompany.CoreCompanyId in (SELECT * FROM ufnSplit (@CoreCompanyId, ','))
AND Company.CompanyId in (SELECT * FROM ufnSplit (@CompanyId, ','))

=========================================

功能:[dbo].[ufnSplit]

CREATE FUNCTION [dbo].[ufnSplit]
(
    @RepParam varchar(MAX),
    @Delim char(1)
)
RETURNS @Values TABLE (Item UNIQUEIDENTIFIER)
AS
BEGIN
  DECLARE @chrind INT
  DECLARE @Piece varchar(MAX)
  SELECT @chrind = 1 
  WHILE @chrind > 0
    BEGIN
      SELECT @chrind = CHARINDEX(@Delim,@RepParam)
      IF @chrind  > 0
        SELECT @Piece = LEFT(@RepParam,@chrind - 1)
      ELSE
        SELECT @Piece = @RepParam
      INSERT  @Values(Item) VALUES(@Piece)
      SELECT @RepParam = RIGHT(@RepParam,LEN(@RepParam) - @chrind)
      IF LEN(@RepParam) = 0 BREAK
    END
  RETURN
  END

当我尝试通过向 @CompanyId 参数提供 4000 多个值来运行存储过程时,它可以正常工作并返回所有可能的记录。

但是在我在 CRM 中部署报告后,运行它并单击查看报告它显示以下错误,“发生错误”

我对其进行了诊断,发现 4000+ guid 逗号分隔值对于存储过程和 IN 语句来说太多了:

消息 8152、级别 16、状态 10、第 7 行字符串或二进制数据将被截断。

请帮忙!

4

1 回答 1

1

当您进行 @CompanyID 参数查询时。为“全选”添加一个选项。

SELECT 'Select All' AS CompanyID, 1 as myOrder
 UNION
 Select CAST(CompanyID AS VARCHAR), 2 as myOrder
 FROM CompanyTable
 ORDER BY myOrder

有了这个,用户不必选择所有 4000 个值,他们只需选择“全选”选项,它将为他们选择所有内容。并更新您的 proc_getCompanyRecord 以处理 CompanyID 的新“全选”值。

CREATE PROCEDURE [dbo].[proc_GetCompanyRecord]
    (
    @Year AS VARCHAR(MAX) = NULL,
    @CoreCompanyId AS VARCHAR(MAX) = NULL,
    @CompanyId AS VARCHAR(MAX) = NULL,
    @OwnerId VARCHAR(MAX) = NULL
    )

--THE REST OF MY QUERY HERE WHICH PULLS ALL OF THE NEEDED COLUMNS

WHERE CoreCompany.CoreCompanyId in (SELECT * FROM ufnSplit (@CoreCompanyId, ','))
AND (Company.CompanyId in (SELECT * FROM ufnSplit (@CompanyId, ',')) OR Company.CompanyID = 'Select All')
于 2013-08-12T10:42:39.227 回答