SQL Server - SSRS - Where to find query generating the report
This will give a listing of the reports with their xml report definition:
SELECT Name,
CAST(CAST(Content AS VARBINARY(MAX)) AS XML) AS ReportXML
FROM ReportServer.dbo.Catalog
where Type = 2
order by Name
The info you want is in the DataSets section, example below. There may be more than one query / stored procedure in the report.
<DataSets>
<DataSet Name="SalesSummary">
<Query>
<CommandType>StoredProcedure</CommandType>
<CommandText>rptSalesSummary</CommandText>
<QueryParameters>
<QueryParameter Name="@STARTDATE">
<Value>=Parameters!STARTDATE.Value</Value>
</QueryParameter>
<QueryParameter Name="@ENDDATE">
<Value>=Parameters!ENDDATE.Value</Value>
</QueryParameter>
</QueryParameters>
<DataSourceName>SampleDB</DataSourceName>
</Query>
.
.
.
</DataSet>
<DataSet Name="CompanyLocation">
<Query>
<DataSourceName>SampleDB</DataSourceName>
<CommandText>
SELECT CompanyLocationID AS ID, Name
FROM tblCompanyLocation
WHERE WareHouseOnlyInd = 0
ORDER BY Description
</CommandText>
<rd:UseGenericDesigner>true</rd:UseGenericDesigner>
</Query>
</DataSet>
.
.
.
See http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition/ReportDefinition.xsd
As jim31415 he said the data it's stored in Catalog table as XML. You can query this data with a cursor and select the data from each XML:
DECLARE @details AS TABLE
(
id INT IDENTITY(1,1) NOT NULL,
Report NVARCHAR(100),
ReportPath NVARCHAR(500),
DataSetName NVARCHAR(50),
DataSourceName NVARCHAR(100),
ComandType NVARCHAR(50),
CommandText NVARCHAR(MAX)
)
DECLARE @report xml
DECLARE @reportname NVARCHAR(100), @reportpath NVARCHAR(500)
DECLARE @query NVARCHAR(MAX)
DECLARE db_cursor CURSOR FOR
SELECT Name, Path ,CAST(cast(Content AS varbinary(max)) as XML)
FROM Catalog
WHERE Type = 2
ORDER BY Name
OPEN db_cursor
FETCH NEXT FROM db_cursor INTO @reportname, @reportpath, @report
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO @details
SELECT
@reportname AS Report,
@reportpath,
t.c.value('@Name[1]', 'nvarchar(50)') AS DataSetName,
t.c.value('(./*:Query/*:DataSourceName)[1]', 'nvarchar(50)') AS DataSourceName,
t.c.value('(./*:Query/*:CommandType)[1]', 'nvarchar(100)') AS CommandType,
ISNULL(t.c.value('(./*:Query/*:CommandText)[1]', 'nvarchar(MAX)'), 'Shared - ' + t.c.value('(./*:SharedDataSet/*:SharedDataSetReference)[1]', 'nvarchar(MAX)')) AS CommandText
FROM
@report.nodes('/*:Report/*:DataSets/*:DataSet') AS t(c)
FETCH NEXT FROM db_cursor INTO @reportname, @reportpath, @report
END
CLOSE db_cursor
DEALLOCATE db_cursor
SELECT * FROM @details ORDER BY Report, DataSetName