我将逗号分隔的数字作为字符串传递给存储过程,参数在IN
关键字中使用,但是当我执行 SP(使用“1,2”之类的参数)时,它给了我一个错误:
将 varchar 值“1,2”转换为数据类型 int 时转换失败。
存储过程是:
ALTER PROCEDURE [Reports].[LocationSummary]
@dtFrom VARCHAR(MAX),
@dtTo VARCHAR(MAX),
@passTypeId VARCHAR(MAX),
@passCategoryId VARCHAR(MAX),
@passTransId VARCHAR(MAX),
@printOption VARCHAR(MAX),
@printLocationId VARCHAR(MAX)
AS
BEGIN
SELECT Admins.PLocations.ArabicName AS PLocationA,
Admins.PLocations.EnglishName AS PLocationE,
SUM(CASE WHEN Port.Info.Printed = 1 THEN 1 ELSE 0 END) AS Printed,
SUM(CASE WHEN Port.Info.Printed = 0 THEN 0 ELSE 1 END) AS NotPrinted,
SUM(CASE WHEN Port.PortPrintLog.PortId IS NOT NULL THEN 1 ELSE 0 END) AS Reprinted
FROM Port.Info INNER JOIN
Port.PortRequests ON
Port.Info.PassRequestId = Port.PortRequests.ID LEFT JOIN
Port.PortDefinitions ON
Port.PortRequests.PassDefinitionID = Port.PortDefinitions.ID INNER JOIN
Admins.PortCategories ON
Port.PortDefinitions.PassCategoryId = Admins.PortCategories.ID INNER JOIN
Admins.PortTypes ON
Port.PortDefinitions.PassTypeId = Admins.PortTypes.ID INNER JOIN
Admins.PortTransactionTypes ON
Port.PortDefinitions.PassTransactionTypeId = Admins.PortTransactionTypes.ID INNER JOIN
Admins.PLocations ON
Port.Info.PrintLocationID = Admins.PLocations.ID LEFT JOIN
Port.PortPrintLog ON Port.PortPrintLog.PortId = Port.Info.ID
WHERE
(@dtFrom IS NOT NULL AND Port.PortRequests.IssuanceDate IS NOT NULL AND Port.PortRequests.IssuanceDate >= @dtFrom)
AND (@dtTo IS NOT NULL AND Port.PortRequests.ExpiryDate IS NOT NULL AND Port.PortRequests.ExpiryDate <= @dtTo)
AND ((Admins.PortTypes.ID IN (CAST(@passTypeId AS INT)))
AND (Admins.PortCategories.ID IN (CAST(@passCategoryId)))
AND (Admins.PortTransactionTypes.ID IN (CAST(@passTransId)))
AND (Port.Info.PrintLocationID IN (CAST(@PrintLocationId AS INT))))
GROUP BY Admins.PrintLocations.ArabicName, Admins.PrintLocations.EnglishName
我怎样才能避免这个错误?Split
我应该对字符串使用函数吗?我可以看到该功能的一些示例吗?