1

First I will mention a bit of my work to better understand my question

So, in SSRS, I have multiple parameters which all are sent to procedure using =Join(Parameter!x.value,",") then in the procedure I create a where condition by concatenating all the parameters.

Something like this:

> Set @where = 
> 'and Table.Column in(''' + replace(RTRIM(LTRIM(@Parameter1)),',', ''',''') 
> + ''')' + ' 
> and Table.Column in(''' + replace(RTRIM(LTRIM(@Parameter2)),',', ''',''')
> + ''')' + '
> and Table.Column in(''' + replace(RTRIM(LTRIM(@Parameter3)),',', ''',''') + 
> ''')' + '

My question is: How can I create this @where in SSRS as a dataset/parameter. I want to do this so that I can use the same @where in a detail report which is connected to main summary report and passed to my detail report when I click on a number in my summary report. I don't want to recreate the same where condition again in my detail rdl

Jatin Garg
  • 85
  • 10
  • Just to clarify, you want an expression in SSRS that will create a single string of this dynamic `WHERE` clause that you could append to some other dynamic SQL in your procedure? We can do that, but I have a feeling you might be over-complicating the report as dynamic SQL is rarely needed and difficult to maintain. – StevenWhite Jul 20 '18 at 22:19

1 Answers1

2

As @StevenWhite pointed out, you probably need to rethink your approach but if you really want to do this.

You need to add an additional parameter to your report (you can hide it once it's all working OK)

The dataset for this parameter would be your existing dynamic sql code, but just the WHERE clause part, so hte end of the dataset query just do something like SELECT @where.

So, this new parameter will be populated once the other parameters have been populated and it's value will be your where clause. You can then pass that as a parameter to your other datasets where applicable.

If that doesn't make sense, let me know and I'll do a more complete answer soon.

More Complete Answer

In this example I've used hte Northwind sample database

I show how to generate a WHERE clause that can be used in another dataset (or as many as you like). In this exmaple I'll just do it with one.

I will have two parmeters for the where clause selections

  • A List of ProductID
  • A List or EmployeeID

Our final dataset query will be dynamic sql that forms the statement something like this..

SELECT 
       o.*
       , d.Discount, d.ProductID, d.Quantity, d.UnitPrice
    FROM Orders o
       JOIN [Order details] d on o.OrderID = d.OrderID
    WHERE ProductID in (11,42,72) and EmployeeID IN (3,5,6)

Heres the steps I took:

Created a new blank report Added a conncetion to the Northwind database

Created a dataset called dsProd Set the query for this dataset to be SELECT ProductID, ProductName FROM Products ORDER BY ProductName

Created a dataset called dsEmployee Set the query for this dataset to be SELECT EmployeeID, FirstName FROM Employees ORDER BY FirstName

Added a parameter called pProd Set the parameter to be Mutil-value Set the available values to the dsProd dataset Set the Value field to ProductID Set the Label field to ProductName

Added a parameter called pEmp Set the parameter to be Mutil-value Set the available values to the dsEmployee dataset Set the Value field to EmployeeID Set the Label field to FirstName

Added a final parmater called pWHERE Set the default value (Specify values) for this to the following Expression

="WHERE ProductID IN (" & Join(Parameters!pProd.Value, ",") & ") " &
" AND EmployeeID IN (" & JOIN(Parameters!pEmp.Value, ",") & ")"

Next added a datset called dsResults Set the dataset Query to

DECLARE @SQL varchar (1000)

SET @SQL = 'SELECT 
       o.*
       , d.Discount, d.ProductID, d.Quantity, d.UnitPrice
    FROM Orders o
       JOIN [Order details] d on o.OrderID = d.OrderID '
       + @pWHERE

EXEC (@SQL)

Finally I added a table to the report pointing to dsResults to display the output.

Now, when you choose the employees and products, the where clause is constructed in the pWHERE parameter and passed to the final query's dataset.

NOTE: Going back to my original point, reiterating what @StevenWhite was saying, all this is probably unneccessary. In this simple case you could have simply set the final dataset query to

SELECT o.* , d.Discount, d.ProductID, d.Quantity, d.UnitPrice FROM Orders o JOIN [Order details] d on o.OrderID = d.OrderID WHERE ProductID in (@pProd) and EmployeeID IN (@pEmp)

This would do exactly the same job, it would be quicker, you would not need the pWHERE parameter at all and it would be more reliable, the example above will probably have issues after the first run as the pWHERE parameter may not refresh correctly.

Anyway, that's up to you but doing it the right way is always quicker in the long run..

Alan Schofield
  • 19,839
  • 3
  • 22
  • 35
  • Thanks Alan for your response. I am getting an error while creating the dataset. Could you please provide an example that would be really helpful. Thank you. My where condition is something like: Where x.y IN (@Param1) and x.y in (@param2 ) and x.y is not null etc. – Jatin Garg Jul 24 '18 at 18:18
  • Thanks a lot Alan. I will try this today and get back. Also, I am working on try to limit the use where conditions in my query as it is slowing down my query a lot. – Jatin Garg Jul 26 '18 at 19:04
  • Hey Alan! I tried the exact same approach. I created a new parameter @as_where with datatype as text and taking values from other parameter. But when I use the parameter in the dataset exactly how you mentioned. Its not able to pick up the parameter value and I get the error pop up as "declare the scalar variable". In the dataset, parameter tab, I have linked the parameter already. Thanks for all your help. I am really stuck in this. – Jatin Garg Jul 27 '18 at 06:04
  • 1
    There is no need to do anything in the dataset properties, when the dataset query contains an undefined parameter name it will create the parameter, if you have already defined it then it should just work. Parameter names in dataset query must match the SSRS parameter name exactly, they are case sensitive. – Alan Schofield Jul 27 '18 at 10:37
  • 1
    Hey Alan, Finally its running. Just a small correction in your solution in the where condition. Where condition should have quotes on both ends e.g x in ('a','b'.'c') in the listed objects. Like this: ="WHERE ProductID IN ('" & Join(Parameters!pProd.Value, "','") & " ') " & " AND EmployeeID IN ('" & JOIN(Parameters!pEmp.Value, "','") & " ')" – Jatin Garg Jul 30 '18 at 22:28
  • OK, that would depend on the data type I guess but the correction you posted is exactly what I originally posted as far as I can tell. Anyway, great to hear you got it working, if you feel my answer suited, please make it as a correct answer, thanks. – Alan Schofield Jul 30 '18 at 23:04
  • Its not same. I have added extra quotes so that all list items in 'IN' condition have quotes at both ends. Added space so quotes are better visible. WHERE ProductID IN (' " & Join(Parameters!pProd.Value, " ' ,' ") & " ') " & – Jatin Garg Jul 31 '18 at 20:13
  • 1
    OK, in my example the parameter values were numbers so didn't require the quotes.ANyway, glad you got it sorted. – Alan Schofield Jul 31 '18 at 21:06