Monday, April 27, 2015

How-to add a collection selection to almost any report.


This seems to be a common question so i desired to make a quick blog post a about it.
There are three parts to this ,first the part that you add to your query:

....AND ResourceID in (select ResourceID from dbo.v_FullCollectionMembership Where CollectionID = @CollectionID)
Notice: If your query have a where clause ,then you just tack it on with the AND operator. If your query does not have a where clause you just replace the AND with a WHERE and paste it in your query.

Next you need a query to get a list of collections ,this is needed to create a dropdown list with the collection names. Without this you just get a textbox where you need to type in the collection ID ,not very user friendly.

Create a new dataset called "Collections" using the following query:

SELECT     Name, CollectionID
FROM         v_Collection where CollectionType = '2'
Order by Name asc


TIP: Prefix the collections that you want to use in your reports with something ,then you can filter the collection list by adding a "WHERE Name like '%Something%' " to the query above.

Last you need a Parameter called "CollectionID" that gets it's available values from the query you just created.


Now you have the a working CollectionParameter in your report with a nice dropdown list.


If you're new to report building ,here is an example:

Lets say you have a simple query in your report to show all "Dell" computers in your environment.

Select A.Name0,A.User_Name0,B.Manufacturer0,B.Model0
from v_R_System A
join v_GS_COMPUTER_SYSTEM B on A.ResourceID = B.ResourceID
Where B.Manufacturer0 Like '%DELL%' 
To limit this query to only return computers from a specific collections you simply add the query snippet at the top of this post to your existing query.

Select A.Name0,A.User_Name0,B.Manufacturer0,B.Model0
from v_R_System A
join v_GS_COMPUTER_SYSTEM B on A.ResourceID = B.ResourceID
Where B.Manufacturer0 Like '%DELL%' AND ResourceID in (select ResourceID from dbo.v_FullCollectionMembership Where CollectionID = @CollectionID)
Add the parameter(CollectionID) and it's dataset and your done!
Hope this helps.





5 comments:

  1. Thank you very much for this valuable post. I've been searching for few weeks on how to add collection prompts and finally seeing your post.
    If you could please educating me of how to add start date and end date, start time and end time selection into report

    ReplyDelete
    Replies
    1. Created a quick post that hopefully answers your problem. http://larsenconfigmgr.blogspot.com/2016/03/how-to-add-date-filters-to-report.html

      Delete
  2. ---------------------------
    Microsoft SQL Server Report Builder
    ---------------------------
    Could not update a list of fields for the query. Verify that you can connect to the data source and that your query syntax is correct.

    Ambiguous column name 'ResourceID'.



    To continue without updating the fields list, click OK.
    ---------------------------
    OK Cancel
    ---------------------------

    ReplyDelete
    Replies
    1. Should be an easy fix ,just several tables that have the resourceID field.

      You need to add a table/view name or alias to the ResourceID.
      Easiest way to do this is to find the FROM keyword in the existing query. Note the name or alias for the view/table and prefix the ResourceID with it.
      Exable if the query look like this "...FROM v_R_System sys" use:
      "....AND sys.ResourceID in (select ResourceID from dbo.v_FullCollectionMembership Where CollectionID = @CollectionID)"

      Delete
  3. I have the same problem with Syed Kalif. It showed Ambiguous column name 'ResourceID'. Please help. Here is my original query:
    SELECT dbo.v_R_System_Valid.Netbios_Name0 AS [PC Name], dbo.v_GS_SYSTEM_CONSOLE_USAGE_MAXGROUP.TopConsoleUser0 AS [User Name],
    dbo.v_GS_ODBC_CONNECTIONS.TimeStamp, dbo.v_GS_ODBC_CONNECTIONS.Database0 AS [Database],
    dbo.v_GS_ODBC_CONNECTIONS.Description0 AS Description, dbo.v_GS_ODBC_CONNECTIONS.DSN0 AS DSN,
    dbo.v_GS_ODBC_CONNECTIONS.Server0 AS Server
    FROM dbo.v_R_System_Valid INNER JOIN
    dbo.v_GS_SYSTEM_CONSOLE_USAGE_MAXGROUP ON
    dbo.v_R_System_Valid.ResourceID = dbo.v_GS_SYSTEM_CONSOLE_USAGE_MAXGROUP.ResourceID INNER JOIN
    dbo.v_GS_ODBC_CONNECTIONS ON dbo.v_GS_SYSTEM_CONSOLE_USAGE_MAXGROUP.ResourceID = dbo.v_GS_ODBC_CONNECTIONS.ResourceID

    ReplyDelete