我正在处理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字符串或二进制数据将是 截断。
请帮忙!
答案 0 :(得分: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')