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 は dynamic query を指定しました。それ以外の場合は、それを使用して次のロジックを実装してみてください。複数の列がある場合は、変更を行いました。

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
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

int にキャストすると、パラメーターに 1,2 を渡すときにエラーが発生します。キャストしないでください。

于 2013-08-19T09:24:05.557 に答える