列出 SQL Server 2008 R2 中的所有数据源及其依赖项(报告、项目等)

Posted

技术标签:

【中文标题】列出 SQL Server 2008 R2 中的所有数据源及其依赖项(报告、项目等)【英文标题】:Listing all Data Sources and their Dependencies (reports, items, etc) in SQL Server 2008 R2 【发布时间】:2012-03-27 03:53:26 【问题描述】:

我是 SQL Server 新手,如果我的问题有明显的解决方案,但我似乎找不到,我很抱歉。

我希望在 SQL Server 2008 R2(报告服务器)上生成所有数据源及其各自依赖项的报告(或列表)。

我知道我可以访问每个单独的数据源以获取依赖于它的所有项目的列表。我以前做过,但是很费时间。

有没有办法获得一个显示所有数据源及其依赖项的报告?

提前致谢,

马旺

【问题讨论】:

您会将什么归类为依赖项?只有模型、数据集和报告,还是还有其他? @beargle - 我会将依赖项定义为使用数据源的任何报告。目前,每个数据源的菜单中都有一个“查看依赖项”。因此,将在其中列出的任何内容都符合从属项的条件。我希望这有助于澄清我的问题/要求。谢谢。 【参考方案1】:

以下内容(根据 beargle 之前发布的内容进行了修改)符合我的要求。这将按其实际名称列出所有数据源及其所有依赖项:

SELECT
    C2.Name AS Data_Source_Name,
    C.Name AS Dependent_Item_Name,
    C.Path AS Dependent_Item_Path
FROM
    ReportServer.dbo.DataSource AS DS
        INNER JOIN
    ReportServer.dbo.Catalog AS C
        ON
            DS.ItemID = C.ItemID
                AND
            DS.Link IN (SELECT ItemID FROM ReportServer.dbo.Catalog
                        WHERE Type = 5) --Type 5 identifies data sources
        FULL OUTER JOIN
    ReportServer.dbo.Catalog C2
        ON
            DS.Link = C2.ItemID
WHERE
    C2.Type = 5
ORDER BY
    C2.Name ASC,
    C.Name ASC;

【讨论】:

@beargle - 感谢您帮助我解决这个问题 (^_^) 第一次尝试。优秀的答案。我所要做的就是更改数据库名称。【参考方案2】:

此查询应针对 ReportServer 数据库运行

SELECT
    DS.Name AS DatasourceName,
    C.Name AS DependentItemName, 
    C.Path AS DependentItemPath
FROM
    ReportServer.dbo.Catalog AS C 
        INNER JOIN
    ReportServer.dbo.Users AS CU
        ON C.CreatedByID = CU.UserID
        INNER JOIN
    ReportServer.dbo.Users AS MU
        ON C.ModifiedByID = MU.UserID
        LEFT OUTER JOIN
    ReportServer.dbo.SecData AS SD
        ON C.PolicyID = SD.PolicyID AND SD.AuthType = 1
        INNER JOIN
    ReportServer.dbo.DataSource AS DS
        ON C.ItemID = DS.ItemID
WHERE
    DS.Name IS NOT NULL
ORDER BY
    DS.Name;

报表管理器中的依赖项页面执行dbo.FindItemsByDataSource 存储过程,提供以下参数:ItemID = <data source item ID>AuthType = 1。上述查询是此存储过程用于删除数据源特定 ID 的查询的破解版本。这允许为所有数据源返回依赖项。我使用DS.Name IS NOT NULL从结果中删除了数据源本身

【讨论】:

这看起来很棒。我希望我在这里听起来不是无知,但是我该如何运行它。到目前为止,我的经验是在服务器上构建和运行 s-s-rS。我不确定如何运行您的查询。提前致谢。 把上面的查询当作普通报表项目的一部分来处理;定义数据源,使用此查询构建数据集,然后将字段添加到您的报告中。或者,使用 SQL Server Management Studio、Visual Studio 或其他工具直接针对 ReportServer 数据库运行它。 非常感谢beargle,您的查询运行良好。也感谢您花时间解释它的细节以及如何运行它。非常有帮助。再次感谢。 如何将数据源名称本身(在上面的查询中过滤为 NULL)替换正在返回的数据源名称。在我正在使用的 ReportServer 中,两者并不相同。我希望我正确地表达了我的问题。谢谢。 我在下面回答了我自己的问题。【参考方案3】:

您也可以考虑使用 Powershell:

    #************************************************************************************************************************************
# FileName:     Delete-DataSources.ps1
# Date:         2015/04/23
# Author:       Hugh Scott
#
# Description:
# This script finds data sources with no dependencies in s-s-rS and removes them.
#
# Parameters:
#   $serverBase     - base URL for the server to check (ie, myserver.mydomain.com)
#   [$WhatIf]       - Option wwitch parameter to prevent actual deleting of objects (will list out reports that need to be deleted)
#***********************************************************************************************************************************
[CmdletBinding()]
Param(
    [Parameter(Mandatory=$true,Position=0)]
    [string]$serverBase,
    [Parameter(Mandatory=$false,Position=1)]
    [switch]$WhatIf
)

$url = "http://$serverBase/reportserver/ReportService2010.asmx?WSDL"
$s-s-rs = New-WebServiceProxy -uri $url -UseDefaultCredential -Namespace "ReportingWebService"

$outFile = ".\DeleteItems_$serverBase.txt"

# Connection to Web Service, grab all data sources
$items = $s-s-rs.ListChildren("/", $true) | where-object $_.typename -eq "DataSource"
foreach($item in $items) 

    $dependencies = $s-s-rs.ListDependentItems($item.Path)
    $dependentReports = $dependencies.Count

    if($dependencies.Count -eq 0)
        [string]$itemName = $item.Path
        if($WhatIf)

            Write-Host "Item $itemName would be deleted."
            Add-Content $outFile "Item $itemName would be deleted."
         else 
            try 
                $s-s-rs.DeleteItem($item.Path)
                Write-Host "Item $itemName deleted."
                Add-Content $outFile "Deleted item $itemName ."
             catch [System.Exception] 
                $Msg = $_.Exception.Message
                Write-Host $itemName $Msg
                Add-Content $itemName $msg
            
        
    

【讨论】:

以上是关于列出 SQL Server 2008 R2 中的所有数据源及其依赖项(报告、项目等)的主要内容,如果未能解决你的问题,请参考以下文章