Suppose I have a table with five columns: (in the real scenario, say, 50 or more columns)
CREATE
TABLE
Test_dynamicColumns
(Col1
VARCHAR
(10),
Col2
Col3
Col4
Col5
(10))
INSERT
SELECT
'col1'
,
'col2'
'col3'
'col4'
'col5'
Now below query is my table dataset query:
*
FROM
Below query is my parameter dataset query: (columns list of table)
name
sys.columns
WHERE
Object_name(Object_id) =
'Test_dynamicColumns'
Below is the column visibility expression - when the parameter value matches the column name, show that column :
=IIF(InStr(
Join
(Parameters!ColumnSelection.Value,
","
),
"Col1"
)>0 ,
False
True
)
To form this expression for all columns try like this:
DECLARE
@i
INT
= 1,@Cnt
,@string NVARCHAR(2000),@xmlstring XML,@Cname
(200)
@Tmp
(id
IDENTITY(1,1),ColumnName
(200))
@Cnt =
COUNT
(Id)
@MasterXML XML =
'<TablixColumnHierarchy>
<TablixMembers>
</TablixMembers>
</TablixColumnHierarchy>'
WHILE @i <= @Cnt
BEGIN
@Cname = ColumnName
id = @i
SET
@string =
'<TablixMember>
<Visibility>
<Hidden>=IIF(InStr(Join(Parameters!ColumnSelection.Value, ","),"'
+@Cname+
'")>0 ,False,True)</Hidden>
</Visibility>
</TablixMember>'
--PRINT @string
@xmlstring =
CONVERT
(XML,@string)
@MasterXML.
modify
(
'
insert sql:variable("@xmlstring")
as last
into (/TablixColumnHierarchy/TablixMembers)[1] '
@i = @i + 1
END
@MasterXML
Go to your report RDL file location: for example: C:\Users\Sathya\Documents\Visual Studio 2010\Projects\SSRS_Demo\SSRS_Demo\report.rdl
Open the rdl file in notepad - > search for <TablixColumnHierarchy> node and replace that with XML segment formed using the query above - > save the rdl file and then check the report.