Passing multiple value parameters in SSRS to stored procedure

后端 未结 2 2094
南笙
南笙 2021-02-08 13:02

I am trying to pass a multiple value string parameter to a table type parameter in a SQL Server 2012 stored procedure. I paste this code in the dataset of SSRS:

         


        
相关标签:
2条回答
  • 2021-02-08 13:25

    Right I need to give you some back ground 1st.

    When you allow SSRS parameter to select multiple values, The selection of multiple values creates a comma deliminated string of value as one string

      'value1,value2,value3'
    

    To check values in a string using IN operator we need strings concatenated with commas something like this ....

      'value1','value2','value3'
    

    Your Proc

    Now in your proc when you insert values explicitly it inserts multiple values into your table.

      INSERT INTO Table_Value_Param
      VALUES ('value1'),       --<-- 1st value/Row
             ('value2'),       --<-- 2nd Value/Row
             ('value3')        --<-- 3rd Value/Row
    

    and this gives you back the expected results as when inside your procedure you execute a statement like

    SELECT * 
    FROM Table_Name
    WHERE ColumnName IN (SELECT ColumnName 
                         FROM Table_Value_Param)
    

    On the other hand when you try to insert into table using SSRS report Parameter you table inserts value like

      INSERT INTO Table_Value_Param
      VALUES ('value1,value2,value3')   --<-- One Row/Value containing all the values comma separated
    

    Solution

    Creating TVP in this situation doesnt really help, What I do is make use of dbo.Split() function inside my procedure.

    You can find many definitions for split function online, for a some cool ones have a look here Split Function equivalent in tsql?

    Once you have created this split function just use this function inside your procedure definition you dont even need the Table valued parameters then.

    Something like this...

      SELECT * 
      FROM Table_Name 
      WHERE ColumnName IN ( 
                           SELECT Value
                           FROM dbo.Split(@Report_Param, ',')
                           ) 
    
    0 讨论(0)
  • 2021-02-08 13:31
    declare @Vendors_Filter nvarchar(max) = 'a,b,c'
    declare @Vendors nvarchar(max) 
    set @Vendors =''''+replace(@Vendors_Filter,',',''',''')+'''' 
    select @Vendors
    
    0 讨论(0)
提交回复
热议问题