How do i represent an unknown number of columns in SSRS?

你。 提交于 2019-12-17 11:15:12

问题


I'm working on a rather complex report in Sql Server Reporting Services. My SP returns a dynamic number of columns each of which are dynamically named.

Basically think of a time keeping application. Each column that is dynamic represents a time bucket that time was charged to for that team. If no time was charged to that bucket for the period of time the report covers it doesn't show. Each bucket has its own identifier which i need to be the column headers.

I have an SP that returns this all. It does it by doing a bit of dynamic SQL with an exec statement (ugly i know but I'm on SQL 2000 so a PIVOT option wouldn't work)

I can have an indefinite number of buckets and any or all might show.

I found this - http://www.codeproject.com/KB/reporting-services/DynamicReport.aspx - which is helpful but in the example he has a finite number of columns and he just hides or shows them according to which ones have values. In my case i have a variable number of columns so somehow i need the report to add columns.

Any thoughts?


回答1:


As long as you know a maximum number of columns, it's possible to do this after a fashion.

First, name the columns with a result from your query, so you can either pass it in to the query or derive it there. Second, just build out the report as if it had the maximum number of columns, and hide them if they are empty.

For example, I had to build a report that would report monthly sales numbers for up to a year, but the months weren't necessarily starting in January. I passed back the month name in one column, followed by the numbers for my report. On the .rdl, I built out 12 sets of columns, one for each possible month, and just used an expression to hide the column if it were empty. The result is the report appears to expand out to the number of columns needed.

Of course, it's not really dynamic in the sense that it can expand out as far as you need without knowing the upper bound.




回答2:


This can be done. I did this and it works fine. You don't have to know the maximum number of columns or show and hide columns in my approach. Use a matrix and modify your sp to return dynamic data to the structure mentioned in this blog post http://sonalimendis.blogspot.com/2011/07/dynamic-column-rdls.html




回答3:


Build 2 related Datasets, first one for the report content, and the second one for the list of its column labels.

The Dataset of the report content must have a fixed number of columns and name. You can allocate some maximum number of columns.

In this example I have the first 2 columns as fixed, or always visible, and a maximum of 4 columns to be displayed by choice through a multivalued parameter, or depends on the query conditions. And as usual, we may have a total as well. So, it may look like this:

Fixed01, Fixed02, Dyna01, Dyna02, Dyna03, Dyna04, Total

The second Dataset with its values will look like this:

Name    Label
----    -----
Dyna01  Label01
Dyna02  Label02
Dyna03  Label03

I have omitted the 4th Label to demonstrate that not all columns are being used by a certain query condition. Remember that both Datasets are meant to be related to the same query.

Now create a parameter named, say, @columns; populate its Available Values and Default Values with the second Dataset.

For each of those 4 dynamic columns, set the column visibility with the following expression:

=IIf(InStr(join(Parameters!columns.Value,","),"Dyna01"),false,true)

And for each of their column header Text Boxes, use the following expression:

=Lookup("Dyna01", Fields!Name.Value, Fields!Label.Value, "dsColumns")

As for the Total, here is the expression for its visibility:

=       IIf(InStr(join(Parameters!columns.Value, ","), "Dyna01"), false, true)
AndAlso IIf(InStr(join(Parameters!columns.Value, ","), "Dyna02"), false, true)
AndAlso IIf(InStr(join(Parameters!columns.Value, ","), "Dyna03"), false, true)
AndAlso IIf(InStr(join(Parameters!columns.Value, ","), "Dyna04"), false, true)

And here is for its values:

= IIf(InStr(join(Parameters!columns.Value, ","), "Dyna01"), Fields!C01.Value, 0)
+ IIf(InStr(join(Parameters!columns.Value, ","), "Dyna02"), Fields!C02.Value, 0)
+ IIf(InStr(join(Parameters!columns.Value, ","), "Dyna03"), Fields!C03.Value, 0)
+ IIf(InStr(join(Parameters!columns.Value, ","), "Dyna04"), Fields!C04.Value, 0)

That's all, hope it helps.

Bonus, that second Dataset, dsColumns, can also hold other column attributes, such as: color, width, fonts, etc.




回答4:


I think the best way to do it is add all the columns in your table and edit the visibility property of it with the help of arguments that you get from your SP..this will solve the purpose of dynamic column but when viewing the report you will get a lot of white-space which you can solve with SSRS - Keep a table the same width when hiding columns dynamically? and your report will be ready




回答5:


I've had the need to do this in the past and the conclusion I came to is "you can't", however I'm not positive about that. If you find a solution, I'd love to hear about it.

An issue that comes to mind is that you need to define the report using the names of the columns that you're going to get back from the stored proc, and if you don't know those names or how many there are, how can you define the report?

The only idea I had on how to do this is to dynamically create the report definition (.rdl file) via C#, but at the time, I wasn't able to find an MS API for doing so, and I doubt one exists now. I found an open source one, but I didn't pursue that route.



来源:https://stackoverflow.com/questions/888661/how-do-i-represent-an-unknown-number-of-columns-in-ssrs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!