How to pass multiple values to single parameter in stored procedure

后端 未结 5 783
野趣味
野趣味 2020-12-30 06:54

I\'m using SSRS for reporting and executing a stored procedure to generate the data for my reports

DECLARE @return_value int

EXEC    @return_value = [dbo].[         


        
5条回答
  •  抹茶落季
    2020-12-30 07:29

    This can not be done easily. There's no way to make an NVARCHAR parameter take "more than one value". What I've done before is - as you do already - make the parameter value like a list with comma-separated values. Then, split this string up into its parts in the stored procedure.

    Splitting up can be done using string functions. Add every part to a temporary table. Pseudo-code for this could be:

    CREATE TABLE #TempTable (ID INT)
    WHILE LEN(@PortfolioID) > 0
    BEGIN
        IF NOT <@PortfolioID contains Comma>
        BEGIN
            INSERT INTO #TempTable VALUES CAST(@PortfolioID as INT)
            SET @PortfolioID = ''
        END ELSE
        BEGIN
             INSERT INTO #Temptable VALUES CAST( AS INT)
             SET @PortfolioID = 
        END
    END
    

    Then, change your condition to

    WHERE PortfolioId IN (SELECT ID FROM #TempTable)
    

    EDIT
    You may be interested in the documentation for multi value parameters in SSRS, which states:

    You can define a multivalue parameter for any report parameter that you create. However, if you want to pass multiple parameter values back to a data source by using the query, the following requirements must be satisfied:

    The data source must be SQL Server, Oracle, Analysis Services, SAP BI NetWeaver, or Hyperion Essbase.

    The data source cannot be a stored procedure. Reporting Services does not support passing a multivalue parameter array to a stored procedure.

    The query must use an IN clause to specify the parameter.

    This I found here.

提交回复
热议问题