Why: Most of the time, when you want a flexibility of your SQL query you can use parameterization. However there might be a situation when you'd need to build a dynamic query. In my case I used SQL query within an expression to feed it to multiple data sources targeting different servers with the exact same query.
DataSet creation: Creation of a simple dataset.
I've created a sample dataset with a sample query:
-- That is a sample query
select top 10 *
from sys.messages
After we created the dataset we can create a Tablix in our report and test it:
Creation of an Expression:
1. Create a new parameter called "SQLExpression"
2. Go to the "Default Values" tab select "Specify values" and choose "fx" box
Assign Expression to the DataSet: Return to the Sample DataSet and Specify newly created parameter as the source for DataSet Expression.
Lessons Learned: When we use SQL Query Expressions we have to follow these rules:
/* That is the Fixed SQL Query
refurbished for SSRS expression usage*/
DECLARE @MessageID INT = 101;
SELECT *
FROM sys.messages
WHERE message_id = @MessageID;
Enjoy the result of your SQL Query Expression:
Conclusion: Use of SQL Expressions is very easy if you fallow those 4 rules.
Please let me know if you hit any other situation in question and I'll add it for others to avoid.