6

我将逗号分隔的数字作为字符串传递给存储过程,参数在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我应该对字符串使用函数吗?我可以看到该功能的一些示例吗?

4

4 回答 4

5

@jyparask 提供了动态查询,您可以使用它,否则尝试实现以下逻辑。你有不止一列,否则我已经完成了更改。

Declare @var1 as varchar(2000)
Declare @var2 as varchar(2000)
Set @var1 = '31,39,41,45'
Set @var2 = ',' + @var1 + ','
select * from Table1 Where Charindex(','+cast(Col1 as varchar)+',', @var2) > 0
于 2013-08-19T10:00:17.023 回答
1

你可能有一个动态的 sql:

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
    DECLARE @SQL AS VARCHAR(MAX)
    SET @SQL = 
    '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 (' + @passTypeId + '))
         AND (Admins.PortCategories.ID IN (' + @passCategoryId + '))
         AND (Admins.PortTransactionTypes.ID IN (' + @passTransId + '))
         AND (Port.Info.PrintLocationID IN (' + @PrintLocationId + ')))

    GROUP BY Admins.PrintLocations.ArabicName, Admins.PrintLocations.EnglishName'

    EXEC (@SQL)
于 2013-08-19T09:05:08.637 回答
0

确保逗号分隔列表中有空格:

(1,2)

不一样

(1, 2)
于 2013-08-19T09:53:38.790 回答
0
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 (@passTypeId)
         AND Admins.PortCategories.ID IN (@passCategoryId)
         AND Admins.PortTransactionTypes.ID IN (@passTransId)
         AND Port.Info.PrintLocationID IN (@PrintLocationId)

    GROUP BY Admins.PrintLocations.ArabicName, Admins.PrintLocations.EnglishName

在参数中传递 1,2 时,转换为 int 会导致错误。不要施放它。

于 2013-08-19T09:24:05.557 回答